286 lines
8.0 KiB
Go
286 lines
8.0 KiB
Go
// Package json_tool ...
|
|
//
|
|
// Description : json_tool ...
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 2023-09-01 16:07
|
|
package json_tool
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.zhangdeman.cn/zhangdeman/serialize"
|
|
)
|
|
|
|
// TestDataFilter_FilterNormalData 最基础对象
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 16:13 2023/9/1
|
|
func TestDataFilter_FilterNormalData(t *testing.T) {
|
|
source := map[string]interface{}{
|
|
"name": "zhangdeman",
|
|
"extra": map[string]interface{}{
|
|
"age": 18,
|
|
"height": 180,
|
|
"slice": []int{1, 2, 3},
|
|
},
|
|
"slice": []int{1, 2, 3},
|
|
}
|
|
df := &DataFilter{
|
|
source: serialize.JSON.MarshalForString(source),
|
|
filterRule: []*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},
|
|
},
|
|
filterOption: &FilterOption{DebugModel: true},
|
|
}
|
|
fmt.Println(df.Filter())
|
|
}
|
|
|
|
// TestDataFilter_getDataAsSlice 测试循环读取数据
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 22:21 2023/9/1
|
|
func TestDataFilter_getDataAsSlice(t *testing.T) {
|
|
data := map[string]interface{}{
|
|
"list": []interface{}{
|
|
map[string]interface{}{
|
|
"name": "1",
|
|
"age": "2",
|
|
"list_test": []interface{}{
|
|
map[string]interface{}{
|
|
"a": "a",
|
|
"b": "b",
|
|
},
|
|
},
|
|
},
|
|
map[string]interface{}{
|
|
"name": "3",
|
|
"age": "4",
|
|
"list_test": []interface{}{
|
|
map[string]interface{}{
|
|
"a": "a1",
|
|
"b": "b1",
|
|
},
|
|
},
|
|
},
|
|
map[string]interface{}{
|
|
"name": "5",
|
|
"age": "6",
|
|
},
|
|
},
|
|
}
|
|
byteData, _ := json.Marshal(data)
|
|
df := &DataFilter{}
|
|
fmt.Println(df.getDataAsSlice(string(byteData), []string{"list", "name"}))
|
|
fmt.Println(df.getDataAsSlice(string(byteData), []string{"list", "age"}))
|
|
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())
|
|
}
|
|
|
|
// 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()
|
|
}
|