From 911f1cb1de7cc6a37b7aa8d371232f0103e849bb Mon Sep 17 00:00:00 2001 From: wln32 <49137144+wln32@users.noreply.github.com> Date: Sun, 7 Apr 2024 10:00:04 +0800 Subject: [PATCH] enhance: use map iter to iterate the map instead of map keys and values (#3457) --- util/gconv/gconv_map.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/util/gconv/gconv_map.go b/util/gconv/gconv_map.go index 668abee34..243a8aa15 100644 --- a/util/gconv/gconv_map.go +++ b/util/gconv/gconv_map.go @@ -298,12 +298,12 @@ func doMapConvertForMapOrStructValue(in doMapConvertForMapOrStructValueInput) in switch reflectKind { case reflect.Map: var ( - mapKeys = reflectValue.MapKeys() + mapIter = reflectValue.MapRange() dataMap = make(map[string]interface{}) ) - for _, k := range mapKeys { + for mapIter.Next() { var ( - mapKeyValue = reflectValue.MapIndex(k) + mapKeyValue = mapIter.Value() mapValue interface{} ) switch { @@ -319,7 +319,7 @@ func doMapConvertForMapOrStructValue(in doMapConvertForMapOrStructValueInput) in default: mapValue = mapKeyValue.Interface() } - dataMap[String(k.Interface())] = doMapConvertForMapOrStructValue( + dataMap[String(mapIter.Key().Interface())] = doMapConvertForMapOrStructValue( doMapConvertForMapOrStructValueInput{ IsRoot: false, Value: mapValue, @@ -486,14 +486,14 @@ func doMapConvertForMapOrStructValue(in doMapConvertForMapOrStructValueInput) in dataMap[mapKey] = array case reflect.Map: var ( - mapKeys = rvAttrField.MapKeys() + mapIter = rvAttrField.MapRange() nestedMap = make(map[string]interface{}) ) - for _, k := range mapKeys { - nestedMap[String(k.Interface())] = doMapConvertForMapOrStructValue( + for mapIter.Next() { + nestedMap[String(mapIter.Key().Interface())] = doMapConvertForMapOrStructValue( doMapConvertForMapOrStructValueInput{ IsRoot: false, - Value: rvAttrField.MapIndex(k).Interface(), + Value: mapIter.Value().Interface(), RecursiveType: in.RecursiveType, RecursiveOption: in.RecursiveType == recursiveTypeTrue, Option: in.Option,