From 1d95e95cf8093d680b792182edf430c86b7e78c9 Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Sun, 5 Jun 2022 12:56:13 +0800 Subject: [PATCH] chore: make methods consistent in signatures (#1971) * chore: make methods consistent in signatures * test: fix fails --- core/logx/tracelogger_test.go | 1 - core/mapping/unmarshaler.go | 4 ++-- core/mapping/unmarshaler_test.go | 8 ++++++++ core/mapping/utils.go | 6 +++--- 4 files changed, 13 insertions(+), 6 deletions(-) diff --git a/core/logx/tracelogger_test.go b/core/logx/tracelogger_test.go index 7dac6d01ac9e..fc5964ea38e1 100644 --- a/core/logx/tracelogger_test.go +++ b/core/logx/tracelogger_test.go @@ -210,7 +210,6 @@ func validate(t *testing.T, body string, expectedTrace, expectedSpan bool) { val = doc } - assert.Nil(t, json.Unmarshal([]byte(body), &val), body) assert.Equal(t, expectedTrace, len(val.Trace) > 0, body) assert.Equal(t, expectedSpan, len(val.Span) > 0, body) } diff --git a/core/mapping/unmarshaler.go b/core/mapping/unmarshaler.go index 7b6df276139b..57e05979ad96 100644 --- a/core/mapping/unmarshaler.go +++ b/core/mapping/unmarshaler.go @@ -231,7 +231,7 @@ func (u *Unmarshaler) processFieldPrimitive(field reflect.StructField, value ref return u.processFieldPrimitiveWithJSONNumber(field, value, v, opts, fullName) default: if typeKind == valueKind { - if err := validateValueInOptions(opts.options(), mapValue); err != nil { + if err := validateValueInOptions(mapValue, opts.options()); err != nil { return err } @@ -253,7 +253,7 @@ func (u *Unmarshaler) processFieldPrimitiveWithJSONNumber(field reflect.StructFi return err } - if err := validateValueInOptions(opts.options(), v); err != nil { + if err := validateValueInOptions(v, opts.options()); err != nil { return err } diff --git a/core/mapping/unmarshaler_test.go b/core/mapping/unmarshaler_test.go index 521e574f5d68..3c113ed13e61 100644 --- a/core/mapping/unmarshaler_test.go +++ b/core/mapping/unmarshaler_test.go @@ -1184,6 +1184,14 @@ func TestUnmarshalWithJsonNumberOptionsIncorrect(t *testing.T) { assert.NotNil(t, UnmarshalKey(m, &in)) } +func TestUnmarshaler_UnmarshalIntOptions(t *testing.T) { + var val struct { + Sex int `json:"sex,options=0|1"` + } + input := []byte(`{"sex": 2}`) + assert.NotNil(t, UnmarshalJsonBytes(input, &val)) +} + func TestUnmarshalWithUintOptionsCorrect(t *testing.T) { type inner struct { Value string `key:"value,options=first|second"` diff --git a/core/mapping/utils.go b/core/mapping/utils.go index ad336cbcdcbe..fee4e99cf85b 100644 --- a/core/mapping/utils.go +++ b/core/mapping/utils.go @@ -592,16 +592,16 @@ func validateNumberRange(fv float64, nr *numberRange) error { return nil } -func validateValueInOptions(options []string, value interface{}) error { +func validateValueInOptions(val interface{}, options []string) error { if len(options) > 0 { - switch v := value.(type) { + switch v := val.(type) { case string: if !stringx.Contains(options, v) { return fmt.Errorf(`error: value "%s" is not defined in options "%v"`, v, options) } default: if !stringx.Contains(options, Repr(v)) { - return fmt.Errorf(`error: value "%v" is not defined in options "%v"`, value, options) + return fmt.Errorf(`error: value "%v" is not defined in options "%v"`, val, options) } } }