chore: add tests (#2778)
This commit is contained in:
@@ -752,7 +752,7 @@ func (u *Unmarshaler) processNamedFieldWithValueFromString(fieldType reflect.Typ
|
|||||||
switch mt := mapValue.(type) {
|
switch mt := mapValue.(type) {
|
||||||
case string:
|
case string:
|
||||||
checkValue = mt
|
checkValue = mt
|
||||||
case json.Number:
|
case fmt.Stringer:
|
||||||
checkValue = mt.String()
|
checkValue = mt.String()
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf("the value in map is not string or json.Number, but %s",
|
return fmt.Errorf("the value in map is not string or json.Number, but %s",
|
||||||
|
|||||||
@@ -260,7 +260,7 @@ func TestUnmarshalIntWithString(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var in inner
|
var in inner
|
||||||
assert.Nil(t, UnmarshalKey(m, &in))
|
assert.NoError(t, UnmarshalKey(m, &in))
|
||||||
assert.Equal(t, int64(0), in.Int)
|
assert.Equal(t, int64(0), in.Int)
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -273,9 +273,37 @@ func TestUnmarshalIntWithString(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var in inner
|
var in inner
|
||||||
assert.Nil(t, UnmarshalKey(m, &in))
|
assert.NoError(t, UnmarshalKey(m, &in))
|
||||||
assert.Equal(t, int64(0), in.Int)
|
assert.Equal(t, int64(0), in.Int)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
t.Run("int with options", func(t *testing.T) {
|
||||||
|
type inner struct {
|
||||||
|
Int int64 `key:"int,string,options=[0,1]"`
|
||||||
|
}
|
||||||
|
m := map[string]interface{}{
|
||||||
|
"int": nil,
|
||||||
|
}
|
||||||
|
|
||||||
|
var in inner
|
||||||
|
assert.Error(t, UnmarshalKey(m, &in))
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("int with options", func(t *testing.T) {
|
||||||
|
type (
|
||||||
|
StrType string
|
||||||
|
|
||||||
|
inner struct {
|
||||||
|
Int int64 `key:"int,string,options=[0,1]"`
|
||||||
|
}
|
||||||
|
)
|
||||||
|
m := map[string]interface{}{
|
||||||
|
"int": StrType("0"),
|
||||||
|
}
|
||||||
|
|
||||||
|
var in inner
|
||||||
|
assert.Error(t, UnmarshalKey(m, &in))
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUnmarshalBoolSliceRequired(t *testing.T) {
|
func TestUnmarshalBoolSliceRequired(t *testing.T) {
|
||||||
|
|||||||
Reference in New Issue
Block a user