diff --git a/tool/json_test.go b/tool/json_test.go index b655dd5..3ae4f41 100644 --- a/tool/json_test.go +++ b/tool/json_test.go @@ -275,3 +275,26 @@ func TestDataFilterDiffArr(t *testing.T) { filter.Filter() //fmt.Println() } + +// TestDataFilterRootArr ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 20:45 2023/8/31 +func TestDataFilterRootArr(t *testing.T) { + source := []map[string]interface{}{ + {"user_list": []interface{}{map[string]interface{}{"name": "alex", "age": 18, "number": 1}}}, + {"user_list": []interface{}{map[string]interface{}{"name": "bob", "age": 28, "number": 2}}}, + {"user_list": []interface{}{map[string]interface{}{"name": "andy", "age": 28, "number": 2}}}, + } + rule := []*FilterDataRule{ + // {SourceKey: "name", MapKey: "slice.[]", DefaultValue: "用户姓名默认值"}, + {SourceKey: "[].user_list.[].name", MapKey: "user_list.[].detail.name", DefaultValue: "用户姓名默认值"}, + {SourceKey: "[].user_list.[].age", MapKey: "user_list.[].detail.age", DefaultValue: "用户姓名默认值"}, + } + byteData, _ := json.Marshal(source) + filter := NewDataFilter(string(byteData), rule) + filter.UserItemToSlice() + filter.Filter() + //fmt.Println() +}