增加struct => wrapper.Map的能力

This commit is contained in:
白茶清欢 2023-10-12 18:50:56 +08:00
parent 0a9175daa8
commit 91eca55f87

7
map.go
View File

@ -12,6 +12,7 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"git.zhangdeman.cn/zhangdeman/easymap" "git.zhangdeman.cn/zhangdeman/easymap"
"git.zhangdeman.cn/zhangdeman/serialize"
"reflect" "reflect"
) )
@ -37,7 +38,11 @@ func EasyMapWithError(mapData interface{}) (Map, error) {
m := easymap.NewNormal(true) m := easymap.NewNormal(true)
reflectType := reflect.TypeOf(mapData) reflectType := reflect.TypeOf(mapData)
if reflectType.Kind() != reflect.Map { if reflectType.Kind() != reflect.Map {
return m, errors.New("input data type is " + reflectType.String() + ", not map") mapFormatData := make(map[string]interface{})
if err := serialize.JSON.UnmarshalWithNumber(serialize.JSON.MarshalForByte(mapData), &mapFormatData); nil != err {
return m, errors.New("input data type is " + reflectType.String() + ", can not convert to map")
}
mapData = mapFormatData
} }
reflectValue := reflect.ValueOf(mapData).MapRange() reflectValue := reflect.ValueOf(mapData).MapRange()