2021-11-23 16:33:01 +08:00
|
|
|
// Package json_tool...
|
|
|
|
//
|
|
|
|
// Description : json_tool...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 2021-03-10 11:44 下午
|
|
|
|
package json_tool
|
|
|
|
|
|
|
|
import (
|
2022-01-09 23:59:43 +08:00
|
|
|
"encoding/json"
|
2021-11-23 16:33:01 +08:00
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TestJSON ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 10:58 下午 2021/3/14
|
|
|
|
func TestJSON(t *testing.T) {
|
|
|
|
tree := NewDynamicJSON()
|
|
|
|
fmt.Println(tree.extraSliceIndex("[200]"))
|
|
|
|
tree.SetValue("extra.height.value", 180, false)
|
|
|
|
tree.SetValue("extra.height.unit.use", "cm", false)
|
|
|
|
tree.SetValue("extra.height.unit.open", "1", false)
|
|
|
|
tree.SetValue("name", "zhangdeman", false)
|
|
|
|
tree.SetValue("good.name", "good", false)
|
|
|
|
tree.SetValue("work", "111", false)
|
|
|
|
tree.SetValue("good.price", "180", false)
|
|
|
|
tree.SetValue("good.unit", "$", false)
|
|
|
|
tree.SetValue("slice.[0].name", "zhang", false)
|
|
|
|
tree.SetValue("slice.[0].age", 30, false)
|
|
|
|
tree.SetValue("slice.[1].name", "de", false)
|
|
|
|
tree.SetValue("slice.[2].name", "man", false)
|
|
|
|
tree.SetValue("slice.[3]", "zhangdeman", false)
|
|
|
|
fmt.Println(tree.String())
|
|
|
|
tree = NewDynamicJSON()
|
|
|
|
tree.SetValue("[0]", "zhang", false)
|
|
|
|
tree.SetValue("[1]", "de", false)
|
|
|
|
tree.SetValue("[2]", "man", false)
|
|
|
|
fmt.Println(tree.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestType 判断数据类型断言
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 10:59 下午 2021/3/14
|
|
|
|
func TestType(t *testing.T) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestSelect 测试动态选择字段
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 9:47 下午 2021/4/13
|
|
|
|
func TestSelect(t *testing.T) {
|
|
|
|
source := map[string]interface{}{
|
|
|
|
"name": "zhangdeman",
|
|
|
|
"extra": map[string]interface{}{
|
|
|
|
"age": 18,
|
|
|
|
"height": 180,
|
|
|
|
"slice": []int{1, 2, 3},
|
|
|
|
},
|
2022-01-17 18:41:59 +08:00
|
|
|
"slice_data": []int{1, 2, 3},
|
|
|
|
"map": map[string]interface{}{"a": 1, "b": 2, "c": 4},
|
2021-11-23 16:33:01 +08:00
|
|
|
"table": []map[string]interface{}{
|
|
|
|
{"name": "alex", "age": 18, "number": 1},
|
|
|
|
{"name": "bob", "age": 28, "number": 2},
|
2022-01-17 18:09:50 +08:00
|
|
|
{"name": "bob", "age": 28, "number": 2, "list": []int{1, 2, 3}},
|
2021-11-23 16:33:01 +08:00
|
|
|
},
|
|
|
|
}
|
2022-01-17 18:09:50 +08:00
|
|
|
rule := map[string]MapDataRule{
|
2022-01-17 18:41:59 +08:00
|
|
|
"name": {MapKey: "user_name", DefaultValue: "用户姓名默认值", IsComplexType: false},
|
2022-01-17 18:09:50 +08:00
|
|
|
"extra.age": {MapKey: "user_age", DefaultValue: "用户年龄默认值", IsComplexType: false},
|
|
|
|
"extra.height": {MapKey: "user_height", DefaultValue: "扩展高度默认值", IsComplexType: false},
|
|
|
|
"table.[].name": {MapKey: "slice.[].name_modify", DefaultValue: "列表姓名默认值", IsComplexType: false},
|
|
|
|
"table.[].list": {MapKey: "slice.[].data_list", DefaultValue: "[\"567\",\"678\",\"789\"]", IsComplexType: true},
|
2021-11-23 16:33:01 +08:00
|
|
|
}
|
|
|
|
filter := NewFilter(source, rule)
|
|
|
|
d, e := filter.Result()
|
|
|
|
if nil != e {
|
|
|
|
fmt.Println(e)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Println(d.String())
|
|
|
|
}
|
2022-01-09 23:59:43 +08:00
|
|
|
|
|
|
|
// TestParse 测试获取JSON数据结构
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<张德满>
|
|
|
|
//
|
|
|
|
// Date : 10:59 PM 2022/1/9
|
|
|
|
func TestParse(t *testing.T) {
|
|
|
|
source := map[string]interface{}{
|
|
|
|
"name": "zhangdeman",
|
|
|
|
"extra": map[string]interface{}{
|
|
|
|
"age": 18,
|
|
|
|
"height": 180,
|
|
|
|
"slice": []int{1, 2, 3},
|
|
|
|
"obj": map[string]interface{}{
|
|
|
|
"la": "aaaa",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"slice": []int{1, 2, 3},
|
|
|
|
"map": map[string]interface{}{"a": 1, "b": 2, "c": 4},
|
|
|
|
"table": []map[string]interface{}{
|
|
|
|
{"name": "alex", "age": 18, "number": 1, "obj": map[string]interface{}{"enen": "en"}},
|
|
|
|
{"name": "bob", "age": 28, "number": 2},
|
|
|
|
},
|
|
|
|
"two_slice": []map[string]interface{}{
|
|
|
|
{
|
|
|
|
"students": []map[string]interface{}{
|
|
|
|
{
|
|
|
|
"name": "enen",
|
|
|
|
"age": 18,
|
|
|
|
"score": []float64{1, 2, 3, 45},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"other": []interface{}{"others"},
|
|
|
|
"read_only": 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
byteData, _ := json.Marshal(source)
|
|
|
|
fmt.Println(GetJSONDataStruct(string(byteData)))
|
|
|
|
}
|
2022-01-10 23:26:50 +08:00
|
|
|
|
|
|
|
// TestParseWithType 测试获取JSON数据结构
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<张德满>
|
|
|
|
//
|
|
|
|
// Date : 10:59 PM 2022/1/9
|
|
|
|
func TestParseWithType(t *testing.T) {
|
|
|
|
source := map[string]interface{}{
|
|
|
|
"name": "zhangdeman",
|
|
|
|
"extra": map[string]interface{}{
|
|
|
|
"age": 18,
|
|
|
|
"height": 180,
|
|
|
|
"slice": []int{1, 2, 3},
|
|
|
|
"obj": map[string]interface{}{
|
|
|
|
"la": "aaaa",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"slice": []int{1, 2, 3},
|
|
|
|
"map": map[string]interface{}{"a": 1, "d": 5.5, "e": "qqq"},
|
|
|
|
"empty_obj": map[string]interface{}{},
|
|
|
|
"empty_list": make([]interface{}, 0),
|
|
|
|
"table": []map[string]interface{}{
|
|
|
|
{"name": "alex", "age": 18, "number": 1, "obj": map[string]interface{}{"enen": "en"}},
|
|
|
|
{"name": "bob", "age": 28, "number": 2},
|
|
|
|
},
|
|
|
|
"two_slice": []map[string]interface{}{
|
|
|
|
{
|
|
|
|
"students": []map[string]interface{}{
|
|
|
|
{
|
|
|
|
"name": "enen",
|
|
|
|
"age": 18,
|
|
|
|
"score": []float64{1, 2, 3, 45},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"other": []interface{}{"others"},
|
|
|
|
"read_only": 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
byteData, _ := json.Marshal(source)
|
|
|
|
fmt.Println(GetJSONDataStructWithType(string(byteData)))
|
|
|
|
}
|
2022-01-22 23:45:05 +08:00
|
|
|
|
|
|
|
// TestDataFilter 测试数据过滤
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 2022/1/22 10:19 PM
|
|
|
|
func TestDataFilter(t *testing.T) {
|
|
|
|
source := map[string]interface{}{
|
|
|
|
"name": "zhangdeman",
|
|
|
|
"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}},
|
|
|
|
},
|
|
|
|
}
|
2022-01-25 14:29:42 +08:00
|
|
|
rule := []*FilterDataRule{
|
2022-01-22 23:45:05 +08:00
|
|
|
{SourceKey: "name", MapKey: "user_name", DefaultValue: "用户姓名默认值"},
|
|
|
|
{SourceKey: "name", MapKey: "username", DefaultValue: "用户姓名默认值"},
|
|
|
|
{SourceKey: "name", MapKey: "user.name", DefaultValue: "用户姓名默认值"},
|
|
|
|
{SourceKey: "extra.age", MapKey: "user.age", DefaultValue: "用户年龄默认值"},
|
|
|
|
{SourceKey: "extra.age", MapKey: "user_age", DefaultValue: "用户年龄默认值"},
|
|
|
|
{SourceKey: "extra.height", MapKey: "user.height", DefaultValue: "扩展高度默认值"},
|
|
|
|
{SourceKey: "extra.height", MapKey: "user_height", DefaultValue: "扩展高度默认值"},
|
|
|
|
{SourceKey: "table.[].name", MapKey: "slice.[].name_modify", DefaultValue: "列表姓名默认值"},
|
|
|
|
{SourceKey: "table.[].list", MapKey: "slice.[].data_list", DefaultValue: "[\"567\",\"678\",\"789\"]"},
|
|
|
|
}
|
|
|
|
byteData, _ := json.Marshal(source)
|
|
|
|
filter := NewDataFilter(string(byteData), rule)
|
|
|
|
fmt.Println(filter.Filter())
|
|
|
|
}
|
2022-01-23 00:53:34 +08:00
|
|
|
|
|
|
|
// 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}},
|
|
|
|
},
|
|
|
|
}
|
2022-01-25 14:29:42 +08:00
|
|
|
rule := []*FilterDataRule{
|
2022-01-23 00:53:34 +08:00
|
|
|
// {SourceKey: "name", MapKey: "slice.[]", DefaultValue: "用户姓名默认值"},
|
2022-01-25 14:29:42 +08:00
|
|
|
{SourceKey: "age", MapKey: "slice", DefaultValue: "用户姓名默认值"},
|
|
|
|
{SourceKey: "height", MapKey: "slice", DefaultValue: "用户姓名默认值"},
|
2022-01-23 00:53:34 +08:00
|
|
|
}
|
|
|
|
byteData, _ := json.Marshal(source)
|
|
|
|
filter := NewDataFilter(string(byteData), rule)
|
2022-01-25 14:29:42 +08:00
|
|
|
filter.UserItemToSlice()
|
2022-01-23 00:53:34 +08:00
|
|
|
fmt.Println(filter.Filter())
|
|
|
|
}
|
2022-01-26 16:39:20 +08:00
|
|
|
|
|
|
|
// TestDataFilterDiffArr ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 12:27 下午 2022/1/26
|
|
|
|
func TestDataFilterDiffArr(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{}{
|
2022-01-26 18:36:05 +08:00
|
|
|
{"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, "list": []int{1, 2, 3}}}},
|
2022-01-26 16:39:20 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
rule := []*FilterDataRule{
|
|
|
|
// {SourceKey: "name", MapKey: "slice.[]", DefaultValue: "用户姓名默认值"},
|
|
|
|
{SourceKey: "table.[].user_list.[].name", MapKey: "user_list.[].detail.name", DefaultValue: "用户姓名默认值"},
|
2022-01-27 14:23:41 +08:00
|
|
|
{SourceKey: "table.[].user_list.[].age", MapKey: "user_list.[].detail.age", DefaultValue: "用户姓名默认值"},
|
2022-01-26 16:39:20 +08:00
|
|
|
}
|
|
|
|
byteData, _ := json.Marshal(source)
|
|
|
|
filter := NewDataFilter(string(byteData), rule)
|
|
|
|
filter.UserItemToSlice()
|
|
|
|
fmt.Println(filter.Filter())
|
|
|
|
}
|