* fix: #2672 * chore: fix more cases * chore: update deps * chore: update deps * chore: refactor * chore: refactor * chore: refactor
This commit is contained in:
@@ -735,8 +735,16 @@ func (u *Unmarshaler) generateMap(keyType, elemType reflect.Type, mapValue inter
|
||||
default:
|
||||
switch v := keythData.(type) {
|
||||
case bool:
|
||||
if dereffedElemKind != reflect.Bool {
|
||||
return emptyValue, errTypeMismatch
|
||||
}
|
||||
|
||||
targetValue.SetMapIndex(key, reflect.ValueOf(v))
|
||||
case string:
|
||||
if dereffedElemKind != reflect.String {
|
||||
return emptyValue, errTypeMismatch
|
||||
}
|
||||
|
||||
targetValue.SetMapIndex(key, reflect.ValueOf(v))
|
||||
case json.Number:
|
||||
target := reflect.New(dereffedElemType)
|
||||
@@ -746,6 +754,10 @@ func (u *Unmarshaler) generateMap(keyType, elemType reflect.Type, mapValue inter
|
||||
|
||||
targetValue.SetMapIndex(key, target.Elem())
|
||||
default:
|
||||
if dereffedElemKind != keythValue.Kind() {
|
||||
return emptyValue, errTypeMismatch
|
||||
}
|
||||
|
||||
targetValue.SetMapIndex(key, keythValue)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3563,6 +3563,71 @@ func TestGoogleUUID(t *testing.T) {
|
||||
assert.Equal(t, "6ba7b810-9dad-11d1-80b4-00c04fd430c2", val.Uidp.String())
|
||||
}
|
||||
|
||||
func TestUnmarshalJsonReaderWithTypeMismatchBool(t *testing.T) {
|
||||
var req struct {
|
||||
Params map[string]bool `json:"params"`
|
||||
}
|
||||
body := `{"params":{"a":"123"}}`
|
||||
assert.Equal(t, errTypeMismatch, UnmarshalJsonReader(strings.NewReader(body), &req))
|
||||
}
|
||||
|
||||
func TestUnmarshalJsonReaderWithTypeMismatchString(t *testing.T) {
|
||||
var req struct {
|
||||
Params map[string]string `json:"params"`
|
||||
}
|
||||
body := `{"params":{"a":{"a":123}}}`
|
||||
assert.Equal(t, errTypeMismatch, UnmarshalJsonReader(strings.NewReader(body), &req))
|
||||
}
|
||||
|
||||
func TestUnmarshalJsonReaderWithMismatchType(t *testing.T) {
|
||||
type Req struct {
|
||||
Params map[string]string `json:"params"`
|
||||
}
|
||||
|
||||
var req Req
|
||||
body := `{"params":{"a":{"a":123}}}`
|
||||
assert.Equal(t, errTypeMismatch, UnmarshalJsonReader(strings.NewReader(body), &req))
|
||||
}
|
||||
|
||||
func TestUnmarshalJsonReaderWithMismatchTypeBool(t *testing.T) {
|
||||
type Req struct {
|
||||
Params map[string]bool `json:"params"`
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
}{
|
||||
{
|
||||
name: "int",
|
||||
input: `{"params":{"a":123}}`,
|
||||
},
|
||||
{
|
||||
name: "int",
|
||||
input: `{"params":{"a":"123"}}`,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
test := test
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
var req Req
|
||||
assert.Equal(t, errTypeMismatch, UnmarshalJsonReader(strings.NewReader(test.input), &req))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnmarshalJsonReaderWithMismatchTypeBoolMap(t *testing.T) {
|
||||
var req struct {
|
||||
Params map[string]string `json:"params"`
|
||||
}
|
||||
assert.Equal(t, errTypeMismatch, UnmarshalJsonMap(map[string]interface{}{
|
||||
"params": map[string]interface{}{
|
||||
"a": true,
|
||||
},
|
||||
}, &req))
|
||||
}
|
||||
|
||||
func BenchmarkDefaultValue(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
var a struct {
|
||||
|
||||
@@ -82,7 +82,14 @@ func ValidatePtr(v *reflect.Value) error {
|
||||
func convertType(kind reflect.Kind, str string) (interface{}, error) {
|
||||
switch kind {
|
||||
case reflect.Bool:
|
||||
return str == "1" || strings.ToLower(str) == "true", nil
|
||||
switch strings.ToLower(str) {
|
||||
case "1", "true":
|
||||
return true, nil
|
||||
case "0", "false":
|
||||
return false, nil
|
||||
default:
|
||||
return false, errTypeMismatch
|
||||
}
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
intValue, err := strconv.ParseInt(str, 10, 64)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user