This commit is contained in:
John Guo
2022-01-13 20:49:26 +08:00
parent 8731123030
commit 6e12aa4bf7

View File

@ -217,23 +217,25 @@ func doMapConvertForMapOrStructValue(isRoot bool, value interface{}, recursive b
return dataMap
case reflect.Struct:
var dataMap = make(map[string]interface{})
// Map converting interface check.
if v, ok := value.(iMapStrAny); ok {
m := v.MapStrAny()
if recursive {
for k, v := range m {
m[k] = doMapConvertForMapOrStructValue(false, v, recursive, tags...)
// Value copy, in case of concurrent safety.
for mapK, mapV := range v.MapStrAny() {
if recursive {
dataMap[mapK] = doMapConvertForMapOrStructValue(false, mapV, recursive, tags...)
} else {
dataMap[mapK] = mapV
}
}
return m
return dataMap
}
// Using reflect for converting.
var (
rtField reflect.StructField
rvField reflect.Value
dataMap = make(map[string]interface{}) // result map.
reflectType = reflectValue.Type() // attribute value type.
mapKey = "" // mapKey may be the tag name or the struct attribute name.
reflectType = reflectValue.Type() // attribute value type.
mapKey = "" // mapKey may be the tag name or the struct attribute name.
)
for i := 0; i < reflectValue.NumField(); i++ {
rtField = reflectType.Field(i)
@ -319,8 +321,10 @@ func doMapConvertForMapOrStructValue(isRoot bool, value interface{}, recursive b
break
}
array := make([]interface{}, length)
for i := 0; i < length; i++ {
array[i] = doMapConvertForMapOrStructValue(false, rvAttrField.Index(i), recursive, tags...)
for arrayIndex := 0; arrayIndex < length; arrayIndex++ {
array[arrayIndex] = doMapConvertForMapOrStructValue(
false, rvAttrField.Index(arrayIndex), recursive, tags...,
)
}
dataMap[mapKey] = array
case reflect.Map: