升级json数据过滤
升级json数据过滤
This commit is contained in:
@ -10,6 +10,7 @@ package json_tool
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.zhangdeman.cn/zhangdeman/serialize"
|
||||
@ -140,3 +141,145 @@ func TestDataFilter_sourceArrAndMapSingle(t *testing.T) {
|
||||
}
|
||||
fmt.Println(df.Filter())
|
||||
}
|
||||
|
||||
// TestDataFilter_unfoldSameDeepArr 测试展开同深度数组
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 15:42 2023/9/2
|
||||
func TestDataFilter_unfoldSameDeepArr(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{}{
|
||||
"a": "a2",
|
||||
"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.[].a.[].val", DefaultValue: "[]", WithDefault: true},
|
||||
{SourceKey: "list.[].list_test.[].b", MapKey: "b_list.[].b.[].val", DefaultValue: "[]", WithDefault: true},
|
||||
{SourceKey: "list.[].list_test.[].c", MapKey: "c_list.[].c.[].val", DefaultValue: "[]", WithDefault: true},
|
||||
{SourceKey: "list.[].list_test.[].d", MapKey: "d_list.[].d.[].val", DefaultValue: "[]", WithDefault: true},
|
||||
},
|
||||
filterOption: &FilterOption{DebugModel: true},
|
||||
}
|
||||
r, _ := df.unfoldSameDeepArr("list.[].list_test.[].a", "a_list.[].a_not_equal_list", "", "")
|
||||
|
||||
formatRes := make([]*FilterDataRule, 0)
|
||||
for _, item := range r {
|
||||
itemMapArr := strings.Split(item.MapKey, ".")
|
||||
if len(itemMapArr) != 3 && len(itemMapArr) != 5 {
|
||||
continue
|
||||
}
|
||||
formatRes = append(formatRes, item)
|
||||
}
|
||||
fmt.Println(serialize.JSON.MarshalForString(formatRes))
|
||||
}
|
||||
|
||||
// TestDataFilter_filterSameDeepArr ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 20:41 2023/9/2
|
||||
func TestDataFilter_filterSameDeepArr(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{}{
|
||||
"a": "a2",
|
||||
"b": "b1",
|
||||
"c": map[string]interface{}{
|
||||
"a": "a",
|
||||
},
|
||||
"d": []int{1, 2, 3},
|
||||
},
|
||||
},
|
||||
},
|
||||
map[string]interface{}{
|
||||
"name": "5",
|
||||
"age": "6",
|
||||
},
|
||||
},
|
||||
}
|
||||
filterRuleList := []*FilterDataRule{
|
||||
{SourceKey: "name", MapKey: "user_name", DefaultValue: "油猴", WithDefault: true},
|
||||
{SourceKey: "extra.age", MapKey: "user_age", DefaultValue: "18", WithDefault: true},
|
||||
{SourceKey: "slice", MapKey: "user_index", DefaultValue: "[4,5,6]", WithDefault: true},
|
||||
{SourceKey: "none", MapKey: "none_default", DefaultValue: map[string]interface{}{"a": "a"}, WithDefault: true},
|
||||
{SourceKey: "extra", MapKey: "extra_object", DefaultValue: map[string]interface{}{"a": "a"}, WithDefault: true},
|
||||
{SourceKey: "list.[].list_test.[].a", MapKey: "a_list.[].a.[].val", DefaultValue: "[]", WithDefault: true},
|
||||
{SourceKey: "list.[].list_test.[].a", MapKey: "a_list.[].a_not_equal_list", DefaultValue: "[]", WithDefault: true},
|
||||
{SourceKey: "list.[].list_test.[].a", MapKey: "a_list.[].a.[].val1", DefaultValue: "[]", WithDefault: true},
|
||||
{SourceKey: "list.[].list_test.[].b", MapKey: "b_list.[].b.[].val", DefaultValue: "[]", WithDefault: true},
|
||||
{SourceKey: "list.[].list_test.[].c", MapKey: "c_list.[].c.[].val", DefaultValue: "[]", WithDefault: true},
|
||||
{SourceKey: "list.[].list_test.[].d", MapKey: "d_list.[].d.[].val", DefaultValue: "[]", WithDefault: true},
|
||||
}
|
||||
filterOption := &FilterOption{DebugModel: true}
|
||||
|
||||
df := NewDataFilter(serialize.JSON.MarshalForString(source), filterRuleList, filterOption)
|
||||
_, _ = df.Filter()
|
||||
}
|
||||
|
Reference in New Issue
Block a user