From 34913edf28da4a2ac050bad697a082de867351a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Sat, 2 Sep 2023 11:38:15 +0800 Subject: [PATCH] =?UTF-8?q?=E6=98=A0=E5=B0=84=E9=9D=9E=E6=95=B0=E7=BB=84,?= =?UTF-8?q?=20=E6=95=B0=E6=8D=AE=E6=BA=90=E6=95=B0=E7=BB=84=E5=A4=84?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tool/gabs.go | 4 ++++ tool/gabs_test.go | 55 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/tool/gabs.go b/tool/gabs.go index 3d06ba1..1b3fdff 100644 --- a/tool/gabs.go +++ b/tool/gabs.go @@ -105,6 +105,10 @@ func (df *DataFilter) Filter() (string, error) { return "", fmt.Errorf("映射的层级深度大于数据源深度, source_path => %v map_path => %v", itemRule.SourceKey, itemRule.MapKey) } if !mapIsArr { + if err = df.setValue(itemRule.MapKey, df.getDataAsSlice(df.source, df.getArrPathList(itemRule.SourceKey))); nil != err { + df.logPrint(logLevelFatal, "映射非数组, 数据源为数组, 设置失败", "source_path => "+itemRule.SourceKey, "map_path => "+itemRule.MapKey, " err => "+err.Error()) + return "", fmt.Errorf("映射的层级深度大于数据源深度, source_path => %v map_path => %v", itemRule.SourceKey, itemRule.MapKey) + } } } diff --git a/tool/gabs_test.go b/tool/gabs_test.go index 494c169..0953156 100644 --- a/tool/gabs_test.go +++ b/tool/gabs_test.go @@ -85,3 +85,58 @@ func TestDataFilter_getDataAsSlice(t *testing.T) { fmt.Println(df.getDataAsSlice(string(byteData), []string{"list", "age", "list_test", "a"})) fmt.Println(df.getDataAsSlice(string(byteData), []string{"list", "age", "list_test", "b"})) } + +// TestDataFilter_sourceArrAndMapSingle map 非数组,数据源数组 +// +// Author : zhangdeman001@ke.com<张德满> +// +// Date : 11:24 2023/9/2 +func TestDataFilter_sourceArrAndMapSingle(t *testing.T) { + source := map[string]interface{}{ + "list": []interface{}{ + map[string]interface{}{ + "name": "1", + "age": "2", + "list_test": []interface{}{ + map[string]interface{}{ + "a": "a", + "b": "b", + "c": map[string]interface{}{ + "a": "1", + }, + "d": []int{1, 2, 3}, + }, + }, + }, + map[string]interface{}{ + "name": "3", + "age": "4", + "list_test": []interface{}{ + map[string]interface{}{ + "a": "a1", + "b": "b1", + "c": map[string]interface{}{ + "a": "a", + }, + "d": []int{1, 2, 3}, + }, + }, + }, + map[string]interface{}{ + "name": "5", + "age": "6", + }, + }, + } + df := &DataFilter{ + source: serialize.JSON.MarshalForString(source), + filterRule: []*FilterDataRule{ + {SourceKey: "list.[].list_test.[].a", MapKey: "a_list", DefaultValue: "[]", WithDefault: true}, + {SourceKey: "list.[].list_test.[].b", MapKey: "b_list", DefaultValue: "[]", WithDefault: true}, + {SourceKey: "list.[].list_test.[].c", MapKey: "c_list", DefaultValue: "[]", WithDefault: true}, + {SourceKey: "list.[].list_test.[].d", MapKey: "d_list", DefaultValue: "[]", WithDefault: true}, + }, + filterOption: &FilterOption{DebugModel: true}, + } + fmt.Println(df.Filter()) +}