This commit is contained in:
Sippakorn Raksakiart 2024-11-22 14:13:18 +00:00 committed by GitHub
commit 50b4e2d2c3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 3 deletions

View File

@ -416,10 +416,14 @@ func TestToStringMapE(t *testing.T) {
{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": 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
{nil, nil, true},
{testing.T{}, nil, true},
{"", nil, true},
}

View File

@ -1135,7 +1135,16 @@ func ToStringMapE(i interface{}) (map[string]interface{}, error) {
err := jsonStringToObject(v, &m)
return m, err
default:
return m, fmt.Errorf("unable to cast %#v of type %T to map[string]interface{}", i, i)
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)
}
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
}
}