- 改Struct2MapWithIgnore为递归
This commit is contained in:
@@ -454,8 +454,6 @@ func FlatMap(in map[string]interface{}) map[string]interface{} {
|
||||
vMap = FlatMap(vMap)
|
||||
maps = append(maps, vMap)
|
||||
keys = append(keys, k)
|
||||
} else {
|
||||
fmt.Printf("%s is %s\n", k, reflect.TypeOf(v).String())
|
||||
}
|
||||
}
|
||||
if len(maps) > 0 {
|
||||
@@ -473,14 +471,38 @@ func Struct2FlatMap(obj interface{}) map[string]interface{} {
|
||||
return FlatMap(m)
|
||||
}
|
||||
|
||||
func Struct2MapWithIgnore(obj interface{}, ignoreValues map[string]interface{}) map[string]interface{} {
|
||||
mapData := Struct2FlatMap(obj)
|
||||
for k, v := range ignoreValues {
|
||||
// todo 性能考虑?
|
||||
if fmt.Sprint(mapData[k]) == fmt.Sprint(v) {
|
||||
delete(mapData, k)
|
||||
func removeIgnoreFields(data interface{}, ignoreValues map[string]interface{}) {
|
||||
dataType := reflect.TypeOf(data).Kind()
|
||||
if dataType == reflect.Map {
|
||||
inMap := data.(map[string]interface{})
|
||||
for k, v := range inMap {
|
||||
// fmt.Printf("k:%s v:%v, ignoreValues[k]:%v\n", k, v, ignoreValues[k])
|
||||
if fmt.Sprint(ignoreValues[k]) == fmt.Sprint(v) {
|
||||
delete(inMap, k)
|
||||
} else {
|
||||
fieldType := reflect.TypeOf(v).Kind()
|
||||
if fieldType == reflect.Map || fieldType == reflect.Slice {
|
||||
removeIgnoreFields(v, ignoreValues)
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if dataType == reflect.Slice {
|
||||
dataList := Interface2Slice(data)
|
||||
for _, v := range dataList {
|
||||
removeIgnoreFields(v, ignoreValues)
|
||||
}
|
||||
}
|
||||
// mapData := data.(map[string]interface{})
|
||||
// for k, v := range ignoreValues {
|
||||
// if fmt.Sprint(mapData[k]) == fmt.Sprint(v) {
|
||||
// delete(mapData, k)
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
func Struct2MapWithIgnore(obj interface{}, ignoreValues map[string]interface{}) map[string]interface{} {
|
||||
mapData := Struct2FlatMap(obj)
|
||||
removeIgnoreFields(mapData, ignoreValues)
|
||||
return mapData
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user