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 展开数组的结果
|
||||
|
@ -7,9 +7,9 @@
|
||||
// Date : 2024-11-30 19:14
|
||||
package gjson_hack
|
||||
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
ErrDataPathNotFound = "data_path_not_found"
|
||||
ErrDataIsNotObject = "data_is_not_object"
|
||||
ErrDataIsNotArray = "data_is_not_an_array"
|
||||
ErrDataTypeIsNotSupport = "data_type_is_not_support"
|
||||
ErrDataIsNotObject = errors.New("data is not an object")
|
||||
ErrDataIsNotArray = errors.New("data is not an array")
|
||||
)
|
||||
|
@ -10,7 +10,6 @@ package gjson_hack
|
||||
import (
|
||||
"errors"
|
||||
"git.zhangdeman.cn/zhangdeman/consts"
|
||||
"git.zhangdeman.cn/zhangdeman/exception"
|
||||
"git.zhangdeman.cn/zhangdeman/serialize"
|
||||
"git.zhangdeman.cn/zhangdeman/util"
|
||||
"git.zhangdeman.cn/zhangdeman/wrapper"
|
||||
@ -23,7 +22,7 @@ import (
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 10:57 2024/11/27
|
||||
func Number(gjsonResult gjson.Result, numberReceiver any) error {
|
||||
func Number[T int64 | uint64 | float64](gjsonResult gjson.Result, numberReceiver *T) error {
|
||||
if !gjsonResult.Exists() {
|
||||
return errors.New("gjson result not found")
|
||||
}
|
||||
@ -41,8 +40,8 @@ func Number(gjsonResult gjson.Result, numberReceiver any) error {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 11:19 2024/11/27
|
||||
func Int[T int8 | int16 | int32 | int64 | int](gjsonResult gjson.Result) (T, error) {
|
||||
var intResult T
|
||||
func Int(gjsonResult gjson.Result) (int64, error) {
|
||||
var intResult int64
|
||||
if err := Number(gjsonResult, &intResult); nil != err {
|
||||
return 0, err
|
||||
}
|
||||
@ -54,25 +53,25 @@ func Int[T int8 | int16 | int32 | int64 | int](gjsonResult gjson.Result) (T, err
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 11:21 2024/11/27
|
||||
func Uint[T uint8 | uint16 | uint32 | uint64 | uint](gjsonResult gjson.Result) (T, error) {
|
||||
var uintResult T
|
||||
func Uint(gjsonResult gjson.Result) (uint64, error) {
|
||||
var uintResult uint64
|
||||
if err := Number(gjsonResult, &uintResult); nil != err {
|
||||
return 0, err
|
||||
}
|
||||
return uintResult, nil
|
||||
}
|
||||
|
||||
// Float 转为float64
|
||||
// Float64 转为float64
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 11:24 2024/11/27
|
||||
func Float[T float32 | float64](gjsonResult gjson.Result) (T, error) {
|
||||
var floatResult T
|
||||
if err := Number(gjsonResult, &floatResult); nil != err {
|
||||
func Float64(gjsonResult gjson.Result) (float64, error) {
|
||||
var float64Result float64
|
||||
if err := Number(gjsonResult, &float64Result); nil != err {
|
||||
return 0, err
|
||||
}
|
||||
return floatResult, nil
|
||||
return float64Result, nil
|
||||
}
|
||||
|
||||
// String 获取字符串值
|
||||
@ -97,11 +96,8 @@ func String(sourceValue gjson.Result, defaultValue string) string {
|
||||
//
|
||||
// Date : 17:48 2024/12/1
|
||||
func MapAnyAny(sourceValue gjson.Result) (map[string]any, error) {
|
||||
if !sourceValue.Exists() {
|
||||
return nil, exception.New(ErrDataPathNotFound, nil, "data path not found")
|
||||
}
|
||||
if !sourceValue.IsObject() {
|
||||
return nil, exception.New(ErrDataIsNotObject, nil)
|
||||
return nil, ErrDataIsNotObject
|
||||
}
|
||||
res := make(map[string]any)
|
||||
sourceValue.ForEach(func(key, value gjson.Result) bool {
|
||||
@ -121,33 +117,13 @@ func Any(sourceValue gjson.Result) (any, error) {
|
||||
return nil, nil
|
||||
}
|
||||
// 可能存在精度丢失, 原因 : gjson.Value 内置的转换, int64 超过一定大小会存在丢失精度问题
|
||||
if sourceValue.Type == gjson.Number {
|
||||
if sourceValue.Num > 0 || sourceValue.Num < 0 {
|
||||
// 说明是数字
|
||||
if strings.Contains(sourceValue.String(), ".") {
|
||||
// 小数
|
||||
var res float64
|
||||
if err := Number(sourceValue, &res); nil != err {
|
||||
return nil, err
|
||||
}
|
||||
return res, nil
|
||||
} else {
|
||||
// 整数
|
||||
if strings.HasPrefix(sourceValue.String(), "-") {
|
||||
// 负数
|
||||
var res int64
|
||||
if err := Number(sourceValue, &res); nil != err {
|
||||
return nil, err
|
||||
}
|
||||
return res, nil
|
||||
} else {
|
||||
// 正数
|
||||
var res uint64
|
||||
if err := Number(sourceValue, &res); nil != err {
|
||||
return nil, err
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
var res float64
|
||||
if err := util.ConvertAssign(&res, sourceValue.String()); nil != err {
|
||||
return nil, err
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
if sourceValue.IsObject() {
|
||||
return MapAnyAny(sourceValue)
|
||||
@ -167,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
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -284,6 +253,7 @@ func SliceBool(gjsonResult gjson.Result) ([]bool, error) {
|
||||
if gjsonResult.Value() == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
strVal := strings.TrimSpace(gjsonResult.String())
|
||||
// 任意类型的list
|
||||
if strings.HasPrefix(strVal, "[") && strings.HasSuffix(strVal, "]") {
|
||||
@ -330,8 +300,8 @@ func SliceMapStringAny(gjsonResult gjson.Result) ([]map[string]any, error) {
|
||||
|
||||
strVal := strings.TrimSpace(gjsonResult.String())
|
||||
// 任意类型的list
|
||||
if !gjsonResult.IsArray() {
|
||||
return nil, exception.New(ErrDataIsNotArray, nil)
|
||||
if !strings.HasPrefix(strVal, "[") || !strings.HasSuffix(strVal, "]") {
|
||||
return nil, ErrDataIsNotArray
|
||||
}
|
||||
var res []map[string]any
|
||||
if err := serialize.JSON.UnmarshalWithNumber([]byte(strVal), &res); nil != err {
|
||||
@ -352,8 +322,8 @@ func SliceSlice(gjsonResult gjson.Result) ([][]any, error) {
|
||||
|
||||
strVal := strings.TrimSpace(gjsonResult.String())
|
||||
// 任意类型的list
|
||||
if !gjsonResult.IsArray() {
|
||||
return nil, exception.New(ErrDataIsNotArray, nil)
|
||||
if !strings.HasPrefix(strVal, "[") || !strings.HasSuffix(strVal, "]") {
|
||||
return nil, ErrDataIsNotArray
|
||||
}
|
||||
var res [][]any
|
||||
if err := serialize.JSON.UnmarshalWithNumber([]byte(strVal), &res); nil != err {
|
||||
@ -369,7 +339,7 @@ func SliceSlice(gjsonResult gjson.Result) ([][]any, error) {
|
||||
// Date : 14:26 2024/12/2
|
||||
func MapStrAny[T string | int64 | uint64 | bool | float64 | []any | map[string]any | any](gjsonResult gjson.Result) (map[string]T, error) {
|
||||
if !gjsonResult.IsObject() {
|
||||
return nil, exception.New(ErrDataIsNotArray, nil)
|
||||
return nil, ErrDataIsNotObject
|
||||
}
|
||||
var res map[string]T
|
||||
if err := serialize.JSON.UnmarshalWithNumber([]byte(gjsonResult.String()), &res); nil != err {
|
||||
@ -393,36 +363,17 @@ func Value(dataType consts.DataType, sourceValue gjson.Result, defaultValue any)
|
||||
strVal := wrapper.String(sourceValue.String())
|
||||
switch dataType {
|
||||
case consts.DataTypeInt:
|
||||
return Int[int](sourceValue)
|
||||
case consts.DataTypeInt8:
|
||||
return Int[int8](sourceValue)
|
||||
case consts.DataTypeInt16:
|
||||
return Int[int16](sourceValue)
|
||||
case consts.DataTypeInt32:
|
||||
return Int[int32](sourceValue)
|
||||
case consts.DataTypeInt64:
|
||||
return Int[int64](sourceValue)
|
||||
return Int(sourceValue)
|
||||
case consts.DataTypeUint:
|
||||
return Uint[uint](sourceValue)
|
||||
case consts.DataTypeUint8:
|
||||
return Uint[uint8](sourceValue)
|
||||
case consts.DataTypeUint16:
|
||||
return Uint[uint16](sourceValue)
|
||||
case consts.DataTypeUint32:
|
||||
return Uint[uint32](sourceValue)
|
||||
case consts.DataTypeUint64:
|
||||
return Uint[uint64](sourceValue)
|
||||
case consts.DataTypeFloat64:
|
||||
return Float[float64](sourceValue)
|
||||
case consts.DataTypeFloat32:
|
||||
return Float[float32](sourceValue)
|
||||
return Uint(sourceValue)
|
||||
case consts.DataTypeFloat:
|
||||
return Float64(sourceValue)
|
||||
case consts.DataTypeBool:
|
||||
boolVal := strVal.ToBool()
|
||||
return boolVal.Value, boolVal.Err
|
||||
case consts.DataTypeString:
|
||||
return sourceValue.String(), nil
|
||||
case consts.DataTypeAny:
|
||||
// 经过getRealDataType类型处理后依旧是any,不考虑精度问题
|
||||
return sourceValue.Value(), nil
|
||||
case consts.DataTypeSliceAny:
|
||||
// 任意类型的list
|
||||
@ -464,6 +415,6 @@ func Value(dataType consts.DataType, sourceValue gjson.Result, defaultValue any)
|
||||
case consts.DataTypeMapStrSlice:
|
||||
return MapStrAny[[]any](sourceValue)
|
||||
default:
|
||||
return nil, exception.New(ErrDataTypeIsNotSupport, map[string]any{"data_type": dataType.String()})
|
||||
return nil, errors.New("data_type = `" + dataType.String() + "` is not support!")
|
||||
}
|
||||
}
|
||||
|
15
go.mod
15
go.mod
@ -1,15 +1,15 @@
|
||||
module git.zhangdeman.cn/zhangdeman/json_filter
|
||||
|
||||
go 1.23.0
|
||||
go 1.21
|
||||
|
||||
toolchain go1.24.3
|
||||
toolchain go1.21.5
|
||||
|
||||
require (
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250425024726-cc17224cb995
|
||||
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-20250504055908-8d68e6106ea9
|
||||
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,16 +18,15 @@ require (
|
||||
)
|
||||
|
||||
require (
|
||||
git.zhangdeman.cn/zhangdeman/exception v0.0.0-20250510123912-a0d52fc093ab // indirect
|
||||
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
|
||||
github.com/jtolds/gls v4.20.0+incompatible // indirect
|
||||
github.com/mitchellh/go-homedir v1.1.0 // indirect
|
||||
github.com/mozillazg/go-pinyin v0.20.0 // indirect
|
||||
github.com/sbabiv/xml2map v1.2.1 // indirect
|
||||
github.com/smarty/assertions v1.15.0 // indirect
|
||||
github.com/spaolacci/murmur3 v1.1.0 // indirect
|
||||
github.com/tidwall/match v1.1.1 // indirect
|
||||
|
56
go.sum
56
go.sum
@ -1,25 +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-20250425024726-cc17224cb995 h1:LmPRAf0AsxRVFPibdpZR89ajlsz8hof2IvMMyTqiEq4=
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250425024726-cc17224cb995/go.mod h1:5p8CEKGBxi7qPtTXDI3HDmqKAfIm5i/aBWdrbkbdNjc=
|
||||
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/exception v0.0.0-20250510123912-a0d52fc093ab h1:O0XaAKKb8qrjcjewonmKfnRsMFoCfJF+tUv6RfhRe94=
|
||||
git.zhangdeman.cn/zhangdeman/exception v0.0.0-20250510123912-a0d52fc093ab/go.mod h1:Voc8J4ordx7nuMWpgACXXZULQy7ZIuBzcEIoS8VnDIw=
|
||||
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-20250504055908-8d68e6106ea9 h1:/GLQaFoLb+ciHOtAS2BIyPNnf4O5ME3AC5PUaJY9kfs=
|
||||
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20250504055908-8d68e6106ea9/go.mod h1:ABJ655C5QenQNOzf7LjCe4sSB52CXvaWLX2Zg4uwDJY=
|
||||
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,17 +54,17 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/sbabiv/xml2map v1.2.1 h1:1lT7t0hhUvXZCkdxqtq4n8/ZCnwLWGq4rDuDv5XOoFE=
|
||||
github.com/sbabiv/xml2map v1.2.1/go.mod h1:2TPoAfcaM7+Sd4iriPvzyntb2mx7GY+kkQpB/GQa/eo=
|
||||
github.com/smarty/assertions v1.15.0 h1:cR//PqUBUiQRakZWqBiFFQ9wb8emQGDb0HeGdqGByCY=
|
||||
github.com/smarty/assertions v1.15.0/go.mod h1:yABtdzeQs6l1brC900WlRNwj6ZR55d7B+E8C6HtKdec=
|
||||
github.com/smartystreets/goconvey v1.8.1 h1:qGjIddxOk4grTu9JPOU31tVfq3cNdBlNa5sSznIX1xY=
|
||||
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,142 +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"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// 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:
|
||||
valStr := pathRes.String()
|
||||
if strings.Contains(valStr, ".") {
|
||||
return consts.DataTypeFloat64.String()
|
||||
}
|
||||
if strings.HasPrefix(valStr, "-") {
|
||||
return consts.DataTypeInt64.String()
|
||||
}
|
||||
return consts.DataTypeUint64.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[int64](g.gjsonResult.Get(dataPath))
|
||||
}
|
||||
|
||||
func (g *gjsonRead) Uint(dataPath string) (uint64, error) {
|
||||
return gjson_hack.Uint[uint64](g.gjsonResult.Get(dataPath))
|
||||
}
|
||||
|
||||
func (g *gjsonRead) Float(dataPath string) (float64, error) {
|
||||
return gjson_hack.Float[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