From 749abad4ada9c55ec6a4d7008037c86b3a31e4e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Sun, 23 Jan 2022 00:53:34 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0obj2slice,=E4=BB=A5=E5=8F=8A?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E7=B1=BB=E5=9E=8B=E6=A3=80=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- json_tool/gabs.go | 43 ++++++++++++++++++++++++++++++++++++++++++ json_tool/json_test.go | 33 ++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) diff --git a/json_tool/gabs.go b/json_tool/gabs.go index a4cc986..4992117 100644 --- a/json_tool/gabs.go +++ b/json_tool/gabs.go @@ -8,8 +8,11 @@ package json_tool import ( + "reflect" "strings" + "github.com/pkg/errors" + "github.com/tidwall/gjson" "github.com/Jeffail/gabs" @@ -59,11 +62,35 @@ func (df *DataFilter) Filter() (string, error) { jsonObject *gabs.Container err error ) + // 记录 obj => slice 的数据类型 + obg2slice := make(map[string]string) // 创建数据的根结点 jsonObject = gabs.New() for _, item := range df.filterRule { if strings.Contains(item.SourceKey, "[]") { // 数组, 特殊处理 + // 1. 判断数据源数组深度与目标数组深度是否一致 + continue + } + // 目标位置, 是一个数组 + if strings.HasSuffix(item.MapKey, "[]") { + realMapKey := strings.ReplaceAll(item.MapKey, ".[]", "") + if exist := jsonObject.Exists(realMapKey); !exist { + if _, err = jsonObject.ArrayP(realMapKey); nil != err { + return "", err + } + } + valueResult := gjson.Get(df.source, item.SourceKey) + dataType := df.getValueType(valueResult) + if _, exist := obg2slice[realMapKey]; !exist { + obg2slice[realMapKey] = dataType + } + if dataType != obg2slice[realMapKey] { + return "", errors.New(realMapKey + " 预期写入的字段数据类型不一致") + } + if err = jsonObject.ArrayAppend(valueResult.Value(), realMapKey); nil != err { + return "", err + } continue } sourceSearchResult := gjson.Get(df.source, item.SourceKey) @@ -81,3 +108,19 @@ func (df *DataFilter) Filter() (string, error) { } return jsonObject.String(), nil } + +// getValueType 获取数据类型 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2022/1/23 12:45 AM +func (df *DataFilter) getValueType(valueResult gjson.Result) string { + dataType := reflect.TypeOf(valueResult.Value()).String() + if strings.Contains(dataType, "int") { + return "int64" + } + if strings.Contains(dataType, "float") { + return "float64" + } + return dataType +} diff --git a/json_tool/json_test.go b/json_tool/json_test.go index 782e655..bf548c3 100644 --- a/json_tool/json_test.go +++ b/json_tool/json_test.go @@ -206,3 +206,36 @@ func TestDataFilter(t *testing.T) { filter := NewDataFilter(string(byteData), rule) fmt.Println(filter.Filter()) } + +// TestDataFilterForObiToSlice ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2022/1/23 12:06 AM +func TestDataFilterForObiToSlice(t *testing.T) { + source := map[string]interface{}{ + "name": "zhangdeman", + "age": 18, + "height": 180, + "extra": map[string]interface{}{ + "age": 18, + "height": 180, + "slice": []int{1, 2, 3}, + }, + "slice_data": []int{1, 2, 3}, + "map": map[string]interface{}{"a": 1, "b": 2, "c": 4}, + "table": []map[string]interface{}{ + {"name": "alex", "age": 18, "number": 1}, + {"name": "bob", "age": 28, "number": 2}, + {"name": "bob", "age": 28, "number": 2, "list": []int{1, 2, 3}}, + }, + } + rule := []FilterDataRule{ + // {SourceKey: "name", MapKey: "slice.[]", DefaultValue: "用户姓名默认值"}, + {SourceKey: "age", MapKey: "slice.[]", DefaultValue: "用户姓名默认值"}, + {SourceKey: "height", MapKey: "slice.[]", DefaultValue: "用户姓名默认值"}, + } + byteData, _ := json.Marshal(source) + filter := NewDataFilter(string(byteData), rule) + fmt.Println(filter.Filter()) +}