Skip to content

Commit

Permalink
chore: make methods consistent in signatures (#1971)
Browse files Browse the repository at this point in the history
* chore: make methods consistent in signatures

* test: fix fails
  • Loading branch information
kevwan committed Jun 5, 2022
1 parent 3fa8c59 commit 1d95e95
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 6 deletions.
1 change: 0 additions & 1 deletion core/logx/tracelogger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions core/mapping/unmarshaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down
8 changes: 8 additions & 0 deletions core/mapping/unmarshaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
6 changes: 3 additions & 3 deletions core/mapping/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand Down

0 comments on commit 1d95e95

Please sign in to comment.