增加新的构建动态JSON方式, TODO: 待完成

This commit is contained in:
白茶清欢 2022-01-22 23:45:05 +08:00
parent dfc58c7052
commit a768d758b2
4 changed files with 123 additions and 0 deletions

1
go.mod
View File

@ -32,6 +32,7 @@ require (
require (
github.com/DataDog/zstd v1.3.5 // indirect
github.com/Jeffail/gabs v1.4.0 // indirect
github.com/StackExchange/wmi v1.2.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/coreos/bbolt v1.3.4 // indirect

2
go.sum
View File

@ -40,6 +40,8 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/DataDog/zstd v1.3.5 h1:DtpNbljikUepEPD16hD4LvIcmhnhdLTiW/5pHgbmp14=
github.com/DataDog/zstd v1.3.5/go.mod h1:1jcaCB/ufaK+sKp1NBhlGmpz41jOoPQ35bpF36t7BBo=
github.com/Jeffail/gabs v1.4.0 h1://5fYRRTq1edjfIrQGvdkcd22pkYUrHZ5YC/H2GJVAo=
github.com/Jeffail/gabs v1.4.0/go.mod h1:6xMvQMK4k33lb7GUUpaAPh6nKMmemQeg5d4gn7/bOXc=
github.com/Shopify/sarama v1.21.0 h1:0GKs+e8mn1RRUzfg9oUXv3v7ZieQLmOZF/bfnmmGhM8=
github.com/Shopify/sarama v1.21.0/go.mod h1:yuqtN/pe8cXRWG5zPaO7hCfNJp5MwmkoJEoLjkm5tCQ=
github.com/Shopify/toxiproxy v2.1.4+incompatible h1:TKdv8HiTLgE5wdJuEML90aBgNWsokNbMijUGhmcoBJc=

83
json_tool/gabs.go Normal file
View File

@ -0,0 +1,83 @@
// Package json_tool ...
//
// Description : json_tool ...
//
// Author : go_developer@163.com<张德满>
//
// Date : 2022/01/22 9:19 PM
package json_tool
import (
"strings"
"github.com/tidwall/gjson"
"github.com/Jeffail/gabs"
)
// FilterDataRule 参数过滤规则
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2022/1/22 9:44 PM
type FilterDataRule struct {
SourceKey string // 原始数据路径
MapKey string // 提取后映射到的数据路径
DefaultValue interface{} // 原始数据路径不存在时的默认值
WithDefault bool // 是否使用默认值
}
// NewDataFilter 获取数据过滤方法实例
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2022/1/22 9:50 PM
func NewDataFilter(source string, filterRule []FilterDataRule) *DataFilter {
return &DataFilter{
source: source,
filterRule: filterRule,
}
}
// DataFilter 数据过滤
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2022/1/22 9:20 PM
type DataFilter struct {
source string
filterRule []FilterDataRule
}
// Filter 数据过滤
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2022/1/22 9:36 PM
func (df *DataFilter) Filter() (string, error) {
var (
jsonObject *gabs.Container
err error
)
// 创建数据的根结点
jsonObject = gabs.New()
for _, item := range df.filterRule {
if strings.Contains(item.SourceKey, "[]") {
// 数组, 特殊处理
continue
}
sourceSearchResult := gjson.Get(df.source, item.SourceKey)
if !sourceSearchResult.Exists() {
if item.WithDefault {
if _, err = jsonObject.SetP(item.DefaultValue, item.MapKey); nil != err {
return "", err
}
}
continue
}
if _, err = jsonObject.SetP(sourceSearchResult.Value(), item.MapKey); nil != err {
return "", err
}
}
return jsonObject.String(), nil
}

View File

@ -169,3 +169,40 @@ func TestParseWithType(t *testing.T) {
byteData, _ := json.Marshal(source)
fmt.Println(GetJSONDataStructWithType(string(byteData)))
}
// 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}},
},
}
rule := []FilterDataRule{
{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())
}