mirror of https://github.com/spf13/cast.git
Merge 64deafa3ae
into 487df00934
This commit is contained in:
commit
50b4e2d2c3
|
@ -416,10 +416,14 @@ func TestToStringMapE(t *testing.T) {
|
||||||
{map[string]interface{}{"tag": "tags", "group": "groups"}, map[string]interface{}{"tag": "tags", "group": "groups"}, false},
|
{map[string]interface{}{"tag": "tags", "group": "groups"}, map[string]interface{}{"tag": "tags", "group": "groups"}, false},
|
||||||
{`{"tag": "tags", "group": "groups"}`, map[string]interface{}{"tag": "tags", "group": "groups"}, false},
|
{`{"tag": "tags", "group": "groups"}`, map[string]interface{}{"tag": "tags", "group": "groups"}, false},
|
||||||
{`{"tag": "tags", "group": true}`, map[string]interface{}{"tag": "tags", "group": true}, false},
|
{`{"tag": "tags", "group": true}`, map[string]interface{}{"tag": "tags", "group": true}, false},
|
||||||
|
{struct {
|
||||||
|
Hello string
|
||||||
|
}{
|
||||||
|
Hello: "World",
|
||||||
|
}, map[string]interface{}{"Hello": "World"}, false},
|
||||||
|
{testing.T{}, map[string]interface{}{}, false},
|
||||||
// errors
|
// errors
|
||||||
{nil, nil, true},
|
{nil, nil, true},
|
||||||
{testing.T{}, nil, true},
|
|
||||||
{"", nil, true},
|
{"", nil, true},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
9
caste.go
9
caste.go
|
@ -1135,8 +1135,17 @@ func ToStringMapE(i interface{}) (map[string]interface{}, error) {
|
||||||
err := jsonStringToObject(v, &m)
|
err := jsonStringToObject(v, &m)
|
||||||
return m, err
|
return m, err
|
||||||
default:
|
default:
|
||||||
|
vo := reflect.ValueOf(i)
|
||||||
|
if vo.Kind() == reflect.Invalid {
|
||||||
return m, fmt.Errorf("unable to cast %#v of type %T to map[string]interface{}", i, i)
|
return m, fmt.Errorf("unable to cast %#v of type %T to map[string]interface{}", i, i)
|
||||||
}
|
}
|
||||||
|
for i := 0; i < vo.NumField(); i++ {
|
||||||
|
if vo.Field(i).CanInterface() {
|
||||||
|
m[vo.Type().Field(i).Name] = vo.Field(i).Interface()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ToStringMapIntE casts an interface to a map[string]int{} type.
|
// ToStringMapIntE casts an interface to a map[string]int{} type.
|
||||||
|
|
Loading…
Reference in New Issue