Compare commits
No commits in common. "master" and "feature/json_hack" have entirely different histories.
master
...
feature/js
@ -1,38 +0,0 @@
|
||||
// Package abstract ...
|
||||
//
|
||||
// Description : abstract ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2025-05-06 10:42
|
||||
package abstract
|
||||
|
||||
// IJsonRead json数据读取接口约束
|
||||
type IJsonRead interface {
|
||||
// Exist 指定路径是否存在
|
||||
Exist(dataPath string) bool
|
||||
// IsNil 指定路径是否为nil
|
||||
IsNil(dataPath string) bool
|
||||
// Type 路径数据类型
|
||||
Type(dataPath string) string
|
||||
// Int 转换为int类型
|
||||
Int(dataPath string) (int64, error)
|
||||
// Uint 转换为uint类型
|
||||
Uint(dataPath string) (uint64, error)
|
||||
// Float 转换为float类型
|
||||
Float(dataPath string) (float64, error)
|
||||
// String 转换为string类型
|
||||
String(dataPath string) (string, error)
|
||||
// Bool 转换为bool类型
|
||||
Bool(dataPath string) (bool, error)
|
||||
// Map 转换为map
|
||||
Map(dataPath string) (map[string]any, error)
|
||||
// MapWithReceiver 通过指针接收
|
||||
MapWithReceiver(dataPath string, receiver any) error
|
||||
// Array 转换为数组
|
||||
Array(dataPath string) ([]any, error)
|
||||
// ArrayWithReceiver 通过指针接收
|
||||
ArrayWithReceiver(dataPath string, receiver any) error
|
||||
// Value 自适应类型数据读取
|
||||
Value(dataPath string, dataType string, defaultValue any) (any, error)
|
||||
}
|
@ -1,26 +0,0 @@
|
||||
// Package abstract ...
|
||||
//
|
||||
// Description : abstract ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2025-05-06 10:53
|
||||
package abstract
|
||||
|
||||
// IJsonWrite json数据写入
|
||||
type IJsonWrite interface {
|
||||
// Set 设置一个路径的值
|
||||
Set(dataPath string, data any) error
|
||||
// Delete 删除一个路径
|
||||
Delete(dataPath string) error
|
||||
// Result 最终结果以字符串形式返回
|
||||
Result() string
|
||||
// Map 最终结果以map返回
|
||||
Map() (map[string]any, error)
|
||||
// MapWithReceiver 外部指针接收返回值
|
||||
MapWithReceiver(receiver any) error
|
||||
// Array 最终结果以数组返回
|
||||
Array() ([]any, error)
|
||||
// ArrayWithReceiver 外部指针接收返回值
|
||||
ArrayWithReceiver(receiver any) error
|
||||
}
|
66
filter.go
66
filter.go
@ -8,30 +8,18 @@
|
||||
package filter
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"git.zhangdeman.cn/zhangdeman/json_filter/abstract"
|
||||
"git.zhangdeman.cn/zhangdeman/json_filter/implement"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
"errors"
|
||||
"git.zhangdeman.cn/zhangdeman/json_filter/gjson_hack"
|
||||
"git.zhangdeman.cn/zhangdeman/serialize"
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
"github.com/tidwall/sjson"
|
||||
|
||||
func NewFilterWithJson(sourceData string, filterRuleList []MapRule, jsonRead abstract.IJsonRead, jsonWrite abstract.IJsonWrite) *filter {
|
||||
if nil == jsonRead {
|
||||
jsonRead = implement.NewGjsonRead(sourceData)
|
||||
}
|
||||
if nil == jsonWrite {
|
||||
jsonWrite = implement.NewSjsonWrite()
|
||||
}
|
||||
return &filter{
|
||||
jsonRaad: jsonRead,
|
||||
jsonWrite: jsonWrite,
|
||||
sourceData: sourceData,
|
||||
filterRuleList: filterRuleList,
|
||||
}
|
||||
}
|
||||
"errors"
|
||||
)
|
||||
|
||||
// NewFilter 过滤器实例
|
||||
//
|
||||
@ -41,8 +29,7 @@ func NewFilterWithJson(sourceData string, filterRuleList []MapRule, jsonRead abs
|
||||
func NewFilter(sourceData string, filterRuleList []MapRule) *filter {
|
||||
return &filter{
|
||||
sourceData: sourceData,
|
||||
jsonRaad: implement.NewGjsonRead(sourceData),
|
||||
jsonWrite: implement.NewSjsonWrite(),
|
||||
formatResult: "{}",
|
||||
filterRuleList: filterRuleList,
|
||||
}
|
||||
}
|
||||
@ -53,9 +40,8 @@ func NewFilter(sourceData string, filterRuleList []MapRule) *filter {
|
||||
//
|
||||
// Date : 11:58 2022/7/4
|
||||
type filter struct {
|
||||
jsonRaad abstract.IJsonRead
|
||||
jsonWrite abstract.IJsonWrite
|
||||
sourceData string
|
||||
formatResult string
|
||||
filterRuleList []MapRule
|
||||
}
|
||||
|
||||
@ -140,10 +126,33 @@ func (f *filter) setResult(rule MapRule) error {
|
||||
err error
|
||||
formatVal any
|
||||
)
|
||||
if formatVal, err = f.jsonRaad.Value(rule.SourcePath, rule.DataType.String(), rule.DefaultValue); nil != err {
|
||||
return fmt.Errorf("%s = %v can not convert to %s : %s", rule.SourcePath, gjson.Get(f.sourceData, rule.SourcePath).String(), rule.DataType, err.Error())
|
||||
|
||||
sourceResult := gjson.Get(f.sourceData, rule.SourcePath)
|
||||
if formatVal, err = gjson_hack.Value(rule.DataType, sourceResult, rule.DefaultValue); nil != err {
|
||||
return fmt.Errorf("%s = %v can not convert to %s : %s", rule.SourcePath, sourceResult.Value(), rule.DataType, err.Error())
|
||||
}
|
||||
if err = f.jsonWrite.Set(rule.TargetPath, formatVal); nil != err {
|
||||
if reflect.TypeOf(formatVal).Kind() == reflect.Map {
|
||||
// 获取的数据是map类型, 处理数据覆盖
|
||||
// eg : 配置如下两个规则 process.id(string) 、process(map[string]any)
|
||||
// 若输入数据的process.id为int类型, 则格式化后的process.id必为 string, 应为 process.id 规则的控制更精细
|
||||
gjsonVal := gjson.Get(f.formatResult, rule.TargetPath)
|
||||
if gjsonVal.Exists() && gjsonVal.IsObject() {
|
||||
var (
|
||||
existRes = map[string]any{}
|
||||
formatRes = map[string]any{}
|
||||
)
|
||||
// 已存在, 且是对象
|
||||
_ = serialize.JSON.UnmarshalWithNumber([]byte(gjsonVal.String()), &existRes)
|
||||
if err = serialize.JSON.Transition(formatVal, &formatRes); nil != err {
|
||||
return errors.New("conflict data path config deal fail : " + err.Error())
|
||||
}
|
||||
for k, v := range existRes {
|
||||
formatRes[k] = v
|
||||
}
|
||||
formatVal = formatRes // 重新赋值 formatVal
|
||||
}
|
||||
}
|
||||
if f.formatResult, err = sjson.Set(f.formatResult, rule.TargetPath, formatVal); nil != err {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
@ -173,7 +182,7 @@ func (f *filter) getAllFinalData(res *[]string, resultList []gjson.Result, pathA
|
||||
//
|
||||
// Date : 21:18 2022/12/31
|
||||
func (f *filter) String() string {
|
||||
return f.jsonWrite.Result()
|
||||
return f.formatResult
|
||||
}
|
||||
|
||||
// Byte 获取格式化之后的字节数组
|
||||
@ -190,10 +199,9 @@ func (f *filter) Byte() []byte {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 21:20 2022/12/31
|
||||
func (f *filter) Parse(receiver any) error {
|
||||
func (f *filter) Parse(receiver interface{}) error {
|
||||
if nil == receiver {
|
||||
return errors.New("receiver is nil")
|
||||
}
|
||||
return f.jsonWrite.MapWithReceiver(receiver)
|
||||
// return json.Unmarshal(f.Byte(), receiver)
|
||||
return json.Unmarshal(f.Byte(), receiver)
|
||||
}
|
||||
|
@ -31,7 +31,7 @@ type PathResult struct {
|
||||
}
|
||||
|
||||
const (
|
||||
ArrayIdxTpl = "[]"
|
||||
ArrayIdxTpl = "{{idx}}"
|
||||
)
|
||||
|
||||
// ExpendArrayResult 展开数组的结果
|
||||
|
@ -143,21 +143,14 @@ func Any(sourceValue gjson.Result) (any, error) {
|
||||
func getRealDataType(dataType consts.DataType, sourceValue gjson.Result) consts.DataType {
|
||||
// 指针数据类型, 转换为普通数据类型处理
|
||||
dataType = consts.DataType(strings.TrimLeft(dataType.String(), "*"))
|
||||
// 没指定数据类型或者数据类型是any
|
||||
if dataType == consts.DataTypeAny || dataType == "" {
|
||||
if dataType == consts.DataTypeAny {
|
||||
if sourceValue.IsObject() {
|
||||
dataType = consts.DataTypeMapAnyAny
|
||||
} else if sourceValue.IsArray() {
|
||||
dataType = consts.DataTypeSliceAny
|
||||
} else if sourceValue.IsBool() {
|
||||
dataType = consts.DataTypeBool
|
||||
} else {
|
||||
if sourceValue.Type == gjson.Number {
|
||||
dataType = consts.DataTypeFloat64
|
||||
} else if sourceValue.Type == gjson.String {
|
||||
dataType = consts.DataTypeString
|
||||
} else {
|
||||
dataType = consts.DataTypeAny
|
||||
if sourceValue.Num != 0 {
|
||||
dataType = consts.DataTypeFloat
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -369,11 +362,11 @@ func Value(dataType consts.DataType, sourceValue gjson.Result, defaultValue any)
|
||||
dataType = getRealDataType(dataType, sourceValue)
|
||||
strVal := wrapper.String(sourceValue.String())
|
||||
switch dataType {
|
||||
case consts.DataTypeInt, consts.DataTypeInt8, consts.DataTypeInt16, consts.DataTypeInt32, consts.DataTypeInt64:
|
||||
case consts.DataTypeInt:
|
||||
return Int(sourceValue)
|
||||
case consts.DataTypeUint, consts.DataTypeUint8, consts.DataTypeUint16, consts.DataTypeUint32, consts.DataTypeUint64:
|
||||
case consts.DataTypeUint:
|
||||
return Uint(sourceValue)
|
||||
case consts.DataTypeFloat64, consts.DataTypeFloat32:
|
||||
case consts.DataTypeFloat:
|
||||
return Float64(sourceValue)
|
||||
case consts.DataTypeBool:
|
||||
boolVal := strVal.ToBool()
|
||||
@ -381,7 +374,6 @@ func Value(dataType consts.DataType, sourceValue gjson.Result, defaultValue any)
|
||||
case consts.DataTypeString:
|
||||
return sourceValue.String(), nil
|
||||
case consts.DataTypeAny:
|
||||
// 经过getRealDataType类型处理后依旧是any,不考虑精度问题
|
||||
return sourceValue.Value(), nil
|
||||
case consts.DataTypeSliceAny:
|
||||
// 任意类型的list
|
||||
|
9
go.mod
9
go.mod
@ -5,11 +5,11 @@ go 1.21
|
||||
toolchain go1.21.5
|
||||
|
||||
require (
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250328040304-7e4a6f9f148c
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241125100843-b1b286c7a701
|
||||
git.zhangdeman.cn/zhangdeman/easylock v0.0.0-20230731062340-983985c12eda
|
||||
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241223084948-de2e49144fcd
|
||||
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241125105403-cb92be844edc
|
||||
git.zhangdeman.cn/zhangdeman/util v0.0.0-20240618042405-6ee2c904644e
|
||||
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20250321102712-1cbfbe959740
|
||||
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20241125065949-2f87fe0cd90e
|
||||
github.com/pkg/errors v0.9.1
|
||||
github.com/smartystreets/goconvey v1.8.1
|
||||
github.com/tidwall/gjson v1.18.0
|
||||
@ -18,8 +18,9 @@ require (
|
||||
)
|
||||
|
||||
require (
|
||||
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20241101082529-28a6c68e38a4 // indirect
|
||||
git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20240122104027-4928421213c0 // indirect
|
||||
github.com/BurntSushi/toml v1.5.0 // indirect
|
||||
github.com/BurntSushi/toml v1.4.0 // indirect
|
||||
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 // indirect
|
||||
github.com/go-ini/ini v1.67.0 // indirect
|
||||
github.com/gopherjs/gopherjs v1.17.2 // indirect
|
||||
|
48
go.sum
48
go.sum
@ -1,19 +1,41 @@
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250321102241-d6e86b64f7ca h1:uxjzbY5fDozjyK6jkoQtuQouVTcVfXjbe3chARYSjRM=
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250321102241-d6e86b64f7ca/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250328040304-7e4a6f9f148c h1:cl3gQGXQpJ8ugDs0C/hQLfcvF4lGBm5BeABLvROFDoM=
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250328040304-7e4a6f9f148c/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240608115700-4311f44fabf6 h1:3xXT341KFAN8AzliAUJtOkyTugNpHpVXY4wDXwE0xmw=
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240608115700-4311f44fabf6/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241104082108-0f97a870bbc3 h1:BiAlBJ+DuRs/xD7nDQD2JT8Oc+V+0Uwt36qZwdXGvzI=
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241104082108-0f97a870bbc3/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241125061350-1f5050978fc3 h1:/40XIygeSxRhPQc3/7pKGpV5hg8jwrMwh1+YiyCHdNI=
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241125061350-1f5050978fc3/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241125065114-f919222003d9 h1:TP/M3WnGsxh0Vr6YuS1i28hw1oV//YbdCoI46PUBIA0=
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241125065114-f919222003d9/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241125100843-b1b286c7a701 h1:G+lGQmjMOBWGspZfijZvenGUAKpjBBrkRXLg3+GZp0U=
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241125100843-b1b286c7a701/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
|
||||
git.zhangdeman.cn/zhangdeman/easylock v0.0.0-20230731062340-983985c12eda h1:bMD6r9gjRy7cO+T4zRQVYAesgIblBdTnhzT1vN5wjvI=
|
||||
git.zhangdeman.cn/zhangdeman/easylock v0.0.0-20230731062340-983985c12eda/go.mod h1:dT0rmHcJ9Z9IqWeMIt7YzR88nKkNV2V3dfG0j9Q6lK0=
|
||||
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20240311030808-e2a2e6a3c211 h1:I/wOsRpCSRkU9vo1u703slQsmK0wnNeZzsWQOGtIAG0=
|
||||
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20240311030808-e2a2e6a3c211/go.mod h1:SrtvrQRdzt+8KfYzvosH++gWxo2ShPTzR1m3VQ6uX7U=
|
||||
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20241101082529-28a6c68e38a4 h1:s6d4b6yY+NaK1AzoBD1pxqsuygEHQz0Oie86c45geDw=
|
||||
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20241101082529-28a6c68e38a4/go.mod h1:V4Dfg1v/JVIZGEKCm6/aehs8hK+Xow1dkL1yiQymXlQ=
|
||||
git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20240122104027-4928421213c0 h1:gUDlQMuJ4xNfP2Abl1Msmpa3fASLWYkNlqDFF/6GN0Y=
|
||||
git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20240122104027-4928421213c0/go.mod h1:VHb9qmhaPDAQDcS6vUiDCamYjZ4R5lD1XtVsh55KsMI=
|
||||
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241223084948-de2e49144fcd h1:q7GG14qgXKB4MEXQFOe7/UYebsqMfPaSX80TcPdOosI=
|
||||
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241223084948-de2e49144fcd/go.mod h1:+D6uPSljwHywjVY5WSBY4TRVMj26TN5f5cFGEYMldjs=
|
||||
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240325080031-1f58204e8687 h1:uQcGqdzi4UdpZlp4f4FUPeBqoygP58pEKJkmN3ROsE0=
|
||||
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240325080031-1f58204e8687/go.mod h1:gf7SW2TXATgux8pfdFedMkXWv2515OtIIM/5c4atkFw=
|
||||
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241108082010-42ae8fe5ebdc h1:jtdEMr/xNchJDEoCnvMr4JXT9+biYQu625Cj+dz025w=
|
||||
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241108082010-42ae8fe5ebdc/go.mod h1:XqgER4jDYwskFgj2riJ9XptIjzgYWubY+Zq8iB2WkY0=
|
||||
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241125105403-cb92be844edc h1:rYjlMH5Yy0G8OQgXA8qrV+fqObnB99v+6s8nbiLhzQs=
|
||||
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241125105403-cb92be844edc/go.mod h1:+D6uPSljwHywjVY5WSBY4TRVMj26TN5f5cFGEYMldjs=
|
||||
git.zhangdeman.cn/zhangdeman/util v0.0.0-20231227095334-7eb5cdbf9253 h1:GO3oZa5a2sqwAzGcLDJtQzmshSWRmoP7IDS8bwFqvC4=
|
||||
git.zhangdeman.cn/zhangdeman/util v0.0.0-20231227095334-7eb5cdbf9253/go.mod h1:VpPjBlwz8U+OxZuxzHQBv1aEEZ3pStH6bZvT21ADEbI=
|
||||
git.zhangdeman.cn/zhangdeman/util v0.0.0-20240618042405-6ee2c904644e h1:Q973S6CcWr1ICZhFI1STFOJ+KUImCl2BaIXm6YppBqI=
|
||||
git.zhangdeman.cn/zhangdeman/util v0.0.0-20240618042405-6ee2c904644e/go.mod h1:VpPjBlwz8U+OxZuxzHQBv1aEEZ3pStH6bZvT21ADEbI=
|
||||
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20250321102712-1cbfbe959740 h1:zPUoylfJTbc0EcxW+NEzOTBmoeFZ2I/rLFBnEzxb4Wk=
|
||||
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20250321102712-1cbfbe959740/go.mod h1:1ct92dbVc49pmXusA/iGfcQUJzcYmJ+cjAhgc3sDv1I=
|
||||
github.com/BurntSushi/toml v1.5.0 h1:W5quZX/G/csjUnuI8SUYlsHs9M38FC7znL0lIO+DvMg=
|
||||
github.com/BurntSushi/toml v1.5.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
|
||||
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240608120635-588df729e0d3 h1:IDzOawDhqBWHACsPKWVv/sALUj09NxrjfB2whWaovvY=
|
||||
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240608120635-588df729e0d3/go.mod h1:7vFN7QrHLLI/iN7ZrJSU0bw/7TyaYjVQ4+clYuIoRrY=
|
||||
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20241106102517-46cd353dd617 h1:4rwv7bYKA4+IQgQiT8yNeM92t+fGd2tR5zD2ixpP6i8=
|
||||
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20241106102517-46cd353dd617/go.mod h1:uEuMBRzTlUJ9k3H+PymZWPn1b48U9lJPQ+ZBWmP+O/c=
|
||||
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20241125062526-91423fb146e0 h1:aGQADmQKTF7c8+s3acnn569sTJwjlLhhhZfQ6f4nAH0=
|
||||
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20241125062526-91423fb146e0/go.mod h1:lvVOIrYDvCQHUzBdaOwKSrxT9ubcXQJBsafWMWu+I14=
|
||||
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20241125065949-2f87fe0cd90e h1:/N5yXmOEH7N/h4S6nv/6os8sRenh95BXogTZ2RJI950=
|
||||
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20241125065949-2f87fe0cd90e/go.mod h1:17TlhgwKTLQLIzYW/R6G39oN5FFPdsEEFDWniv+ovgA=
|
||||
github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0=
|
||||
github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
|
||||
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 h1:OYA+5W64v3OgClL+IrOD63t4i/RW7RqrAVl9LTZ9UqQ=
|
||||
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394/go.mod h1:Q8n74mJTIgjX4RBBcHnJ05h//6/k6foqmgE45jTQtxg=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
@ -38,9 +60,11 @@ github.com/smartystreets/goconvey v1.8.1 h1:qGjIddxOk4grTu9JPOU31tVfq3cNdBlNa5sS
|
||||
github.com/smartystreets/goconvey v1.8.1/go.mod h1:+/u4qLyY6x1jReYOp7GOM2FSt8aP9CzCZL03bI28W60=
|
||||
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
|
||||
github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
|
||||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
||||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
|
||||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||
github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
|
||||
github.com/tidwall/gjson v1.17.1 h1:wlYEnwqAHgzmhNUFfw7Xalt2JzQvsMx2Se4PcoFCT/U=
|
||||
github.com/tidwall/gjson v1.17.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
|
||||
github.com/tidwall/gjson v1.18.0 h1:FIDeeyB800efLX89e5a8Y0BNH+LOngJyGrIWxG2FKQY=
|
||||
github.com/tidwall/gjson v1.18.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
|
||||
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
|
||||
|
@ -1,134 +0,0 @@
|
||||
// Package implement ...
|
||||
//
|
||||
// Description : implement ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2025-05-06 11:00
|
||||
package implement
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"git.zhangdeman.cn/zhangdeman/consts"
|
||||
"git.zhangdeman.cn/zhangdeman/json_filter/abstract"
|
||||
"git.zhangdeman.cn/zhangdeman/json_filter/gjson_hack"
|
||||
"git.zhangdeman.cn/zhangdeman/serialize"
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
|
||||
// NewGjsonRead ...
|
||||
func NewGjsonRead(sourceData string) abstract.IJsonRead {
|
||||
return &gjsonRead{
|
||||
sourceData: sourceData,
|
||||
gjsonResult: gjson.Parse(sourceData),
|
||||
}
|
||||
}
|
||||
|
||||
type gjsonRead struct {
|
||||
sourceData string
|
||||
gjsonResult gjson.Result
|
||||
}
|
||||
|
||||
func (g *gjsonRead) Exist(dataPath string) bool {
|
||||
return g.gjsonResult.Get(dataPath).Exists()
|
||||
}
|
||||
|
||||
func (g *gjsonRead) IsNil(dataPath string) bool {
|
||||
return g.gjsonResult.Get(dataPath).Value() == nil
|
||||
}
|
||||
|
||||
func (g *gjsonRead) Type(dataPath string) string {
|
||||
pathRes := g.gjsonResult.Get(dataPath)
|
||||
if pathRes.IsObject() { // map
|
||||
return consts.DataTypeMapStrAny.String()
|
||||
}
|
||||
if pathRes.IsArray() { // slice
|
||||
return consts.DataTypeSliceAny.String()
|
||||
}
|
||||
if pathRes.IsBool() { // bool
|
||||
return consts.DataTypeBool.String()
|
||||
}
|
||||
dataType := pathRes.Type
|
||||
switch dataType {
|
||||
case gjson.String:
|
||||
return consts.DataTypeString.String()
|
||||
case gjson.Number:
|
||||
return consts.DataTypeFloat64.String()
|
||||
case gjson.Null:
|
||||
return consts.DataTypeAny.String()
|
||||
case gjson.True, gjson.False:
|
||||
return consts.DataTypeBool.String()
|
||||
}
|
||||
return consts.DataTypeAny.String() // any类型兜底
|
||||
}
|
||||
|
||||
func (g *gjsonRead) Int(dataPath string) (int64, error) {
|
||||
return gjson_hack.Int(g.gjsonResult.Get(dataPath))
|
||||
}
|
||||
|
||||
func (g *gjsonRead) Uint(dataPath string) (uint64, error) {
|
||||
return gjson_hack.Uint(g.gjsonResult.Get(dataPath))
|
||||
}
|
||||
|
||||
func (g *gjsonRead) Float(dataPath string) (float64, error) {
|
||||
return gjson_hack.Float64(g.gjsonResult.Get(dataPath))
|
||||
}
|
||||
|
||||
func (g *gjsonRead) Bool(dataPath string) (bool, error) {
|
||||
res := g.gjsonResult.Get(dataPath)
|
||||
if !res.IsBool() {
|
||||
return false, errors.New(dataPath + ": is not a bool")
|
||||
}
|
||||
return res.Bool(), nil
|
||||
}
|
||||
|
||||
func (g *gjsonRead) String(dataPath string) (string, error) {
|
||||
if !g.Exist(dataPath) {
|
||||
return "", errors.New(dataPath + ": not found")
|
||||
}
|
||||
return g.gjsonResult.Get(dataPath).String(), nil
|
||||
}
|
||||
|
||||
func (g *gjsonRead) Map(dataPath string) (map[string]any, error) {
|
||||
var (
|
||||
err error
|
||||
dataMap map[string]any
|
||||
)
|
||||
if err = g.MapWithReceiver(dataPath, &dataMap); nil != err {
|
||||
return map[string]any{}, err
|
||||
}
|
||||
return dataMap, nil
|
||||
}
|
||||
|
||||
func (g *gjsonRead) MapWithReceiver(dataPath string, receiver any) error {
|
||||
strVal, err := g.String(dataPath)
|
||||
if nil != err {
|
||||
return err
|
||||
}
|
||||
return serialize.JSON.UnmarshalWithNumber([]byte(strVal), receiver)
|
||||
}
|
||||
|
||||
func (g *gjsonRead) Array(dataPath string) ([]any, error) {
|
||||
var (
|
||||
err error
|
||||
dataArr []any
|
||||
)
|
||||
if err = g.ArrayWithReceiver(dataPath, &dataArr); nil != err {
|
||||
return []any{}, err
|
||||
}
|
||||
return dataArr, nil
|
||||
}
|
||||
|
||||
func (g *gjsonRead) ArrayWithReceiver(dataPath string, receiver any) error {
|
||||
strVal, err := g.String(dataPath)
|
||||
if nil != err {
|
||||
return err
|
||||
}
|
||||
return serialize.JSON.UnmarshalWithNumber([]byte(strVal), receiver)
|
||||
}
|
||||
func (g *gjsonRead) Value(dataPath string, dataType string, defaultValue any) (any, error) {
|
||||
if len(dataType) == 0 {
|
||||
dataType = g.Type(dataPath)
|
||||
}
|
||||
return gjson_hack.Value(consts.DataType(dataType), g.gjsonResult.Get(dataPath), defaultValue)
|
||||
}
|
@ -1,128 +0,0 @@
|
||||
// Package implement ...
|
||||
//
|
||||
// Description : implement ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2025-05-06 11:53
|
||||
package implement
|
||||
|
||||
import (
|
||||
"git.zhangdeman.cn/zhangdeman/consts"
|
||||
"git.zhangdeman.cn/zhangdeman/json_filter/abstract"
|
||||
"git.zhangdeman.cn/zhangdeman/serialize"
|
||||
"github.com/tidwall/sjson"
|
||||
"reflect"
|
||||
"sync"
|
||||
)
|
||||
|
||||
func NewSjsonWrite() abstract.IJsonWrite {
|
||||
return &SjsonWrite{
|
||||
res: "",
|
||||
l: &sync.RWMutex{},
|
||||
}
|
||||
}
|
||||
|
||||
type SjsonWrite struct {
|
||||
res string
|
||||
l *sync.RWMutex
|
||||
}
|
||||
|
||||
func (s *SjsonWrite) Delete(dataPath string) error {
|
||||
var (
|
||||
err error
|
||||
)
|
||||
s.l.Lock()
|
||||
defer s.l.Unlock()
|
||||
if s.res, err = sjson.Delete(s.res, dataPath); nil != err {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *SjsonWrite) Set(dataPath string, data any) error {
|
||||
var (
|
||||
err error
|
||||
)
|
||||
|
||||
s.l.Lock()
|
||||
defer s.l.Unlock()
|
||||
existResRead := NewGjsonRead(s.res)
|
||||
if !existResRead.Exist(dataPath) || data == nil {
|
||||
// 路径不存在
|
||||
if s.res, err = sjson.Set(s.res, dataPath, data); nil != err {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
// 路径已存在, 判断是否为map
|
||||
if existResRead.Type(dataPath) != consts.DataTypeMapStrAny.String() {
|
||||
if s.res, err = sjson.Set(s.res, dataPath, data); nil != err {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
// 判断data是否为map
|
||||
dataType := reflect.TypeOf(data)
|
||||
if dataType.Kind() == reflect.Ptr {
|
||||
dataType = dataType.Elem()
|
||||
}
|
||||
if dataType.Kind() != reflect.Struct && dataType.Kind() != reflect.Map {
|
||||
// 既不是map, 也不是struct
|
||||
if s.res, err = sjson.Set(s.res, dataPath, data); nil != err {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
// 路径已存在, 已存在数据为map, 且要设置的数据也为map, 进行数据合并
|
||||
mergeDataMap := map[string]any{}
|
||||
existMapVal := map[string]any{}
|
||||
|
||||
if existMapVal, err = existResRead.Map(dataPath); nil != err {
|
||||
return err
|
||||
}
|
||||
// 合并输入数据
|
||||
if err = serialize.JSON.MergeDataForReceiver(&mergeDataMap, existMapVal, data); nil != err {
|
||||
return err
|
||||
}
|
||||
if s.res, err = sjson.Set(s.res, dataPath, mergeDataMap); nil != err {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *SjsonWrite) Result() string {
|
||||
s.l.RLock()
|
||||
defer s.l.RUnlock()
|
||||
return s.res
|
||||
}
|
||||
|
||||
func (s *SjsonWrite) Map() (map[string]any, error) {
|
||||
var (
|
||||
mapRes map[string]any
|
||||
err error
|
||||
)
|
||||
if err = s.MapWithReceiver(&mapRes); nil != err {
|
||||
return nil, err
|
||||
}
|
||||
return mapRes, nil
|
||||
}
|
||||
|
||||
func (s *SjsonWrite) MapWithReceiver(receiver any) error {
|
||||
return serialize.JSON.UnmarshalWithNumberForString(s.Result(), receiver)
|
||||
}
|
||||
|
||||
func (s *SjsonWrite) Array() ([]any, error) {
|
||||
var (
|
||||
arrRes []any
|
||||
err error
|
||||
)
|
||||
if err = s.ArrayWithReceiver(&arrRes); nil != err {
|
||||
return nil, err
|
||||
}
|
||||
return arrRes, nil
|
||||
}
|
||||
|
||||
func (s *SjsonWrite) ArrayWithReceiver(receiver any) error {
|
||||
return serialize.JSON.UnmarshalWithNumberForString(s.Result(), receiver)
|
||||
}
|
@ -9,6 +9,7 @@ package sjson_hack
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"git.zhangdeman.cn/zhangdeman/json_filter/gjson_hack"
|
||||
"git.zhangdeman.cn/zhangdeman/wrapper"
|
||||
"github.com/tidwall/sjson"
|
||||
@ -26,6 +27,7 @@ var (
|
||||
//
|
||||
// Date : 11:36 2024/12/3
|
||||
func Set(jsonRes string, path string, value any) (string, error) {
|
||||
fmt.Println(jsonRes, value)
|
||||
var (
|
||||
err error
|
||||
res string = jsonRes
|
||||
|
Loading…
x
Reference in New Issue
Block a user