From 6e12aa4bf78a3bd4df342b5b6d4f569d2a8c1463 Mon Sep 17 00:00:00 2001 From: John Guo Date: Thu, 13 Jan 2022 20:49:26 +0800 Subject: [PATCH] fix issue #1568 --- util/gconv/gconv_map.go | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/util/gconv/gconv_map.go b/util/gconv/gconv_map.go index f448a8d6b..3c2093283 100644 --- a/util/gconv/gconv_map.go +++ b/util/gconv/gconv_map.go @@ -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: