From f105cfed624fd6f79061444796de01fb3883e5b5 Mon Sep 17 00:00:00 2001 From: gazebo Date: Wed, 3 Apr 2019 10:47:06 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E5=B0=86FlatMap=EF=BC=8CStruct2FlatMap?= =?UTF-8?q?=E4=BB=8Ejxutils=E7=A7=BB=E5=88=B0utils=E4=B8=AD=20+=20?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0MarshalJSONIgnoreValues,=20MustMarshalJSONIgn?= =?UTF-8?q?oreValues?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/typeconv.go | 58 +++++++++++++++++++++++++++++++++++++++++++++++ utils/utils.go | 12 ---------- 2 files changed, 58 insertions(+), 12 deletions(-) diff --git a/utils/typeconv.go b/utils/typeconv.go index 619d1140..746a2d2b 100644 --- a/utils/typeconv.go +++ b/utils/typeconv.go @@ -425,8 +425,66 @@ func Map2KeySlice(flagMap map[string]int) (keyList []string) { return keyList } +// 合并map,相同字段以前面的优先,后来相同的字段忽略 +func MergeMaps(firstMap map[string]interface{}, otherMaps ...map[string]interface{}) (retVal map[string]interface{}) { + retVal = make(map[string]interface{}) + allMaps := append([]map[string]interface{}{firstMap}, otherMaps...) + mapCount := len(allMaps) + for index := range allMaps { + oneMap := allMaps[mapCount-index-1] + for k, v := range oneMap { + retVal[k] = v + } + } + return retVal +} + func Struct2MapByJson(obj interface{}) (mapData map[string]interface{}) { structsObj := structs.New(obj) structsObj.TagName = "json" return structsObj.Map() } + +// 此函数将MAP中所有的子MAP中的数据提升到最上层,相同字段会覆盖父MAP的 +func FlatMap(in map[string]interface{}) map[string]interface{} { + keys := []string{} + maps := []map[string]interface{}{} + for k, v := range in { + if vMap, ok := v.(map[string]interface{}); ok { + vMap = FlatMap(vMap) + maps = append(maps, vMap) + keys = append(keys, k) + } + } + if len(maps) > 0 { + retVal := MergeMaps(in, maps...) + for _, v := range keys { + delete(retVal, v) + } + return retVal + } + return in +} + +func Struct2FlatMap(obj interface{}) map[string]interface{} { + m := Struct2MapByJson(obj) + return FlatMap(m) +} + +func Struct2MapWithIgnore(obj interface{}, ignoreValues map[string]interface{}) map[string]interface{} { + mapData := Struct2FlatMap(obj) + for k, v := range mapData { + if ignoreValues[k] == v { + delete(mapData, k) + } + } + return mapData +} + +func MarshalJSONIgnoreValues(obj interface{}, ignoreValues map[string]interface{}) ([]byte, error) { + return json.Marshal(Struct2MapWithIgnore(obj, ignoreValues)) +} + +func MustMarshalJSONIgnoreValues(obj interface{}, ignoreValues map[string]interface{}) []byte { + return MustMarshal(Struct2MapWithIgnore(obj, ignoreValues)) +} diff --git a/utils/utils.go b/utils/utils.go index deadbf9e..0396864d 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -108,18 +108,6 @@ func GetAPIOperator(userName string) string { return retVal } -func MergeMaps(firstMap map[string]interface{}, otherMaps ...map[string]interface{}) (retVal map[string]interface{}) { - retVal = make(map[string]interface{}) - allMaps := append(otherMaps, firstMap) - for _, oneMap := range allMaps { - for k, v := range oneMap { - retVal[k] = v - } - } - - return retVal -} - func CallFuncLogError(funcToCall func() error, msg string, params ...interface{}) error { err := funcToCall() if err != nil {