-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Support JSON string of map * Support any `map` type * Support any `struct` type
- Loading branch information
Showing
6 changed files
with
201 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package cvt | ||
|
||
import ( | ||
"encoding/json" | ||
"reflect" | ||
) | ||
|
||
// StringMapE convert an interface to `map[string]interface{}` | ||
// * Support JSON string of map | ||
// * Support any `map` type | ||
// * Support any `struct` type | ||
func StringMapE(val interface{}) (m map[string]interface{}, err error) { | ||
m = make(map[string]interface{}) | ||
if val == nil { | ||
return nil, errUnsupportedTypeNil | ||
} | ||
|
||
_, rv := indirect(val) | ||
switch rv.Kind() { | ||
case reflect.Map: | ||
for _, key := range rv.MapKeys() { | ||
m[String(key.Interface())] = rv.MapIndex(key).Interface() | ||
} | ||
case reflect.Struct: | ||
m = struct2map(rv) | ||
case reflect.String: | ||
// JSON string of map | ||
err = json.Unmarshal([]byte(rv.String()), &m) | ||
} | ||
|
||
return | ||
} | ||
|
||
func struct2map(rv reflect.Value) map[string]interface{} { | ||
var m = make(map[string]interface{}) | ||
if !rv.IsValid() { | ||
return m | ||
} | ||
|
||
for j := 0; j < rv.NumField(); j++ { | ||
f := rv.Type().Field(j) | ||
t := ptrType(f.Type) | ||
vv := ptrValue(rv.Field(j)) | ||
if f.Anonymous && t.Kind() == reflect.Struct { | ||
for k, v := range struct2map(vv) { | ||
// anonymous sub-field has a low priority | ||
if _, ok := m[k]; !ok { | ||
m[k] = v | ||
} | ||
} | ||
} else if vv.IsValid() && vv.CanInterface() { | ||
m[f.Name] = vv.Interface() | ||
} | ||
} | ||
return m | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package cvt_test | ||
|
||
import ( | ||
"fmt" | ||
"github.com/shockerli/cvt" | ||
"testing" | ||
) | ||
|
||
func TestStringMapE(t *testing.T) { | ||
tests := []struct { | ||
input interface{} | ||
expect map[string]interface{} | ||
isErr bool | ||
}{ | ||
// JSON String | ||
{`{"name":"cvt","age":3.21}`, map[string]interface{}{"name": "cvt", "age": 3.21}, false}, | ||
{`{"name":"cvt","tag":"convert"}`, map[string]interface{}{"name": "cvt", "tag": "convert"}, false}, | ||
{`{"name":"cvt","build":true}`, map[string]interface{}{"name": "cvt", "build": true}, false}, | ||
|
||
// Map | ||
{map[string]interface{}{}, map[string]interface{}{}, false}, | ||
{map[string]interface{}{"name": "cvt", "age": 3.21}, map[string]interface{}{"name": "cvt", "age": 3.21}, false}, | ||
{map[interface{}]interface{}{"name": "cvt", "age": 3.21}, map[string]interface{}{"name": "cvt", "age": 3.21}, false}, | ||
{map[interface{}]interface{}{111: "cvt", "222": 3.21}, map[string]interface{}{"111": "cvt", "222": 3.21}, false}, | ||
|
||
// Struct | ||
{struct { | ||
Name string | ||
Age int | ||
}{"cvt", 3}, map[string]interface{}{"Name": "cvt", "Age": 3}, false}, | ||
{&struct { | ||
Name string | ||
Age int | ||
}{"cvt", 3}, map[string]interface{}{"Name": "cvt", "Age": 3}, false}, | ||
{struct { | ||
A1 string | ||
TestStructC | ||
}{"a1", TestStructC{"c1"}}, map[string]interface{}{"A1": "a1", "C1": "c1"}, false}, | ||
{struct { | ||
A1 string | ||
TestStructC | ||
C1 string | ||
}{"a1", TestStructC{"c1-1"}, "c1-2"}, map[string]interface{}{"A1": "a1", "C1": "c1-2"}, false}, | ||
{struct { | ||
A1 string | ||
*TestStructC | ||
C1 string | ||
}{"a1", &TestStructC{"c1-1"}, "c1-2"}, map[string]interface{}{"A1": "a1", "C1": "c1-2"}, false}, | ||
{struct { | ||
C1 string | ||
*TestStructC | ||
A1 string | ||
}{"c1-1", &TestStructC{"c1-2"}, "a1"}, map[string]interface{}{"A1": "a1", "C1": "c1-1"}, false}, | ||
{struct { | ||
AliasTypeInt8 | ||
}{5}, map[string]interface{}{"AliasTypeInt8": AliasTypeInt8(5)}, false}, | ||
{struct { | ||
*AliasTypeInt | ||
}{&aliasTypeInt0}, map[string]interface{}{"AliasTypeInt": aliasTypeInt0}, false}, | ||
{struct { | ||
*AliasTypeInt | ||
}{}, map[string]interface{}{}, false}, | ||
{struct { | ||
*TestStructC | ||
}{}, map[string]interface{}{}, false}, | ||
|
||
// errors | ||
{nil, nil, true}, | ||
{"", nil, true}, | ||
{"hello", nil, true}, | ||
} | ||
|
||
for i, tt := range tests { | ||
msg := fmt.Sprintf( | ||
"i = %d, input[%+v], expect[%+v], isErr[%v]", | ||
i, tt.input, tt.expect, tt.isErr, | ||
) | ||
|
||
v, err := cvt.StringMapE(tt.input) | ||
if tt.isErr { | ||
assertError(t, err, "[HasErr] "+msg) | ||
continue | ||
} | ||
|
||
assertNoError(t, err, "[NoErr] "+msg) | ||
assertEqual(t, tt.expect, v, "[WithE] "+msg) | ||
} | ||
} |