IJsonRead支持Value, 泛读取数据
This commit is contained in:
parent
1124ff6a33
commit
042aad254b
@ -31,4 +31,6 @@ type IJsonRead interface {
|
|||||||
Array(dataPath string) ([]any, error)
|
Array(dataPath string) ([]any, error)
|
||||||
// ArrayWithReceiver 通过指针接收
|
// ArrayWithReceiver 通过指针接收
|
||||||
ArrayWithReceiver(dataPath string, receiver any) error
|
ArrayWithReceiver(dataPath string, receiver any) error
|
||||||
|
// Value 自适应类型数据读取
|
||||||
|
Value(dataPath string, dataType string, defaultValue any) (any, error)
|
||||||
}
|
}
|
||||||
|
22
filter.go
22
filter.go
@ -10,6 +10,8 @@ package filter
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"git.zhangdeman.cn/zhangdeman/json_filter/abstract"
|
||||||
|
"git.zhangdeman.cn/zhangdeman/json_filter/implement"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@ -21,6 +23,22 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
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,
|
||||||
|
formatResult: "",
|
||||||
|
filterRuleList: filterRuleList,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// NewFilter 过滤器实例
|
// NewFilter 过滤器实例
|
||||||
//
|
//
|
||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
@ -30,6 +48,8 @@ func NewFilter(sourceData string, filterRuleList []MapRule) *filter {
|
|||||||
return &filter{
|
return &filter{
|
||||||
sourceData: sourceData,
|
sourceData: sourceData,
|
||||||
formatResult: "{}",
|
formatResult: "{}",
|
||||||
|
jsonRaad: implement.NewGjsonRead(sourceData),
|
||||||
|
jsonWrite: implement.NewSjsonWrite(),
|
||||||
filterRuleList: filterRuleList,
|
filterRuleList: filterRuleList,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -40,6 +60,8 @@ func NewFilter(sourceData string, filterRuleList []MapRule) *filter {
|
|||||||
//
|
//
|
||||||
// Date : 11:58 2022/7/4
|
// Date : 11:58 2022/7/4
|
||||||
type filter struct {
|
type filter struct {
|
||||||
|
jsonRaad abstract.IJsonRead
|
||||||
|
jsonWrite abstract.IJsonWrite
|
||||||
sourceData string
|
sourceData string
|
||||||
formatResult string
|
formatResult string
|
||||||
filterRuleList []MapRule
|
filterRuleList []MapRule
|
||||||
|
@ -118,3 +118,9 @@ func (g *gjsonRead) ArrayWithReceiver(dataPath string, receiver any) error {
|
|||||||
}
|
}
|
||||||
return serialize.JSON.UnmarshalWithNumber([]byte(strVal), receiver)
|
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)
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user