Skip to content

Commit

Permalink
修复 map 转换问题
Browse files Browse the repository at this point in the history
  • Loading branch information
gelin9527 committed Mar 4, 2023
1 parent 42be483 commit 5353a83
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions auto.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func analysis(fsVal reflect.Value, objMap map[string]interface{}) {

// 结构体遍历
if itemType.Kind() == reflect.Struct && !types.IsGoBasicType(itemType) && itemType.String() != "dateTime.DateTime" {
structAnalysis(fieldName, fsVal.Field(i), fsVal.Type().Field(i).Type, objMap)
structAnalysis(fieldName, fieldName, fsVal.Field(i), fsVal.Type().Field(i).Type, objMap)
} else {
// 非结构体遍历
itemValue := fsVal.Field(i).Interface()
Expand All @@ -105,7 +105,7 @@ func analysis(fsVal reflect.Value, objMap map[string]interface{}) {
}

// 结构体递归解析
func structAnalysis(fieldName string, fromStructVal reflect.Value, fromStructType reflect.Type, objMap map[string]interface{}) {
func structAnalysis(parentName string, fieldName string, fromStructVal reflect.Value, fromStructType reflect.Type, objMap map[string]interface{}) {
for i := 0; i < fromStructVal.NumField(); i++ {
fieldVal := fromStructVal.Field(i)
itemType := fieldVal.Type()
Expand All @@ -116,16 +116,16 @@ func structAnalysis(fieldName string, fromStructVal reflect.Value, fromStructTyp
objMap[itemName] = itemValue
// map
} else if itemType.Kind() == reflect.Map {
mapAnalysis(fieldName, fieldVal, objMap)
mapAnalysis(parentName, fieldName, fieldVal, objMap)
// struct
} else if itemType.Kind() == reflect.Struct {
structAnalysis(fromStructType.Field(i).Name, fieldVal, fromStructType.Field(i).Type, objMap)
structAnalysis(parentName, fromStructType.Field(i).Name, fieldVal, fromStructType.Field(i).Type, objMap)
}
}
}

// map 解析
func mapAnalysis(fieldName string, fieldVal reflect.Value, objMap map[string]interface{}) {
func mapAnalysis(parentName string, fieldName string, fieldVal reflect.Value, objMap map[string]interface{}) {
newMaps := make(map[string]string)
maps := fieldVal.MapRange()
for maps.Next() {
Expand All @@ -134,7 +134,7 @@ func mapAnalysis(fieldName string, fieldVal reflect.Value, objMap map[string]int
newMaps[array[0]] = array[1]
}
dic := collections.NewDictionaryFromMap(newMaps)
objMap[fieldName] = dic
objMap[parentName] = dic
}

// StructToMap 结构转map
Expand Down

0 comments on commit 5353a83

Please sign in to comment.