From b02230ec2dc0a11957f874c21c5ca761acd95852 Mon Sep 17 00:00:00 2001 From: gazebo Date: Tue, 24 Sep 2019 15:23:27 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E9=87=8D=E6=96=B0=E5=AE=9E=E7=8E=B0utils.S?= =?UTF-8?q?truct2FlatMap?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/typeconv.go | 31 +++++-------------------------- utils/typeconv_test.go | 37 +++++++++++++++++++++++++++++++++++++ utils/utils_test.go | 11 ----------- 3 files changed, 42 insertions(+), 37 deletions(-) diff --git a/utils/typeconv.go b/utils/typeconv.go index f528da43..c1b76479 100644 --- a/utils/typeconv.go +++ b/utils/typeconv.go @@ -13,7 +13,7 @@ import ( "time" "git.rosy.net.cn/baseapi" - "github.com/fatih/structs" + "github.com/gazeboxu/structs" "github.com/mitchellh/mapstructure" ) @@ -449,43 +449,22 @@ func MergeMaps(firstMap map[string]interface{}, otherMaps ...map[string]interfac return retVal } -func Struct2Map(obj interface{}, tagName string) (mapData map[string]interface{}) { +func Struct2Map(obj interface{}, tagName string, isFlattenAnonymous bool) (mapData map[string]interface{}) { if tagName == "" { tagName = "json" } structsObj := structs.New(obj) structsObj.TagName = tagName + structsObj.IsFlattenAnonymous = true return structsObj.Map() } func Struct2MapByJson(obj interface{}) (mapData map[string]interface{}) { - return Struct2Map(obj, "") -} - -// 此函数将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 + return Struct2Map(obj, "", false) } func Struct2FlatMap(obj interface{}) map[string]interface{} { - m := Struct2MapByJson(obj) - return FlatMap(m) + return Struct2Map(obj, "", true) } // !!! 此函数好像不支持struct是内嵌结构的 diff --git a/utils/typeconv_test.go b/utils/typeconv_test.go index d49f2122..081503e3 100644 --- a/utils/typeconv_test.go +++ b/utils/typeconv_test.go @@ -84,3 +84,40 @@ func TestTime(t *testing.T) { } } } + +func TestStruct2MapByJson(t *testing.T) { + type InnerKK struct { + IntData int + StrData string + ObjData time.Time + } + type KK struct { + IntData int + A int + B string + C time.Time + InnerKK + InnerKK2 InnerKK + } + kk := &KK{ + InnerKK: InnerKK{ + IntData: 1, + StrData: "hello", + }, + } + mapData := Struct2MapByJson(kk) + t.Log(Format4Output(mapData, false)) + // t.Log(mapData) + // t.Log(kk) +} + +// func TestStruct2MapByJson(t *testing.T) { +// mapData := Struct2MapByJson(&struct { +// IntData int `structs:"dataInt"` +// StrData string `json:"-"` +// }{ +// IntData: 1, +// StrData: "2", +// }) +// t.Log(mapData) +// } diff --git a/utils/utils_test.go b/utils/utils_test.go index ffdab708..79fb4b29 100644 --- a/utils/utils_test.go +++ b/utils/utils_test.go @@ -175,17 +175,6 @@ func TestTrimBlanChar(t *testing.T) { } } -func TestStruct2MapByJson(t *testing.T) { - mapData := Struct2MapByJson(&struct { - IntData int `structs:"dataInt"` - StrData string `json:"-"` - }{ - IntData: 1, - StrData: "2", - }) - t.Log(mapData) -} - func TestLimitUTF8StringLen(t *testing.T) { for _, v := range [][]interface{}{ []interface{}{