update
This commit is contained in:
76
filter.go
76
filter.go
@ -8,28 +8,16 @@
|
||||
package filter
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/tidwall/sjson"
|
||||
"strings"
|
||||
|
||||
"git.zhangdeman.cn/zhangdeman/util"
|
||||
"github.com/Jeffail/gabs"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
|
||||
// MapRule 映射规则
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 12:21 2022/7/4
|
||||
type MapRule struct {
|
||||
SourcePath string `json:"source_path"` // 原路径
|
||||
MapPath string `json:"map_path"` // 映射路径
|
||||
Required bool `json:"required"` // 必须存在
|
||||
DataType string `json:"data_type"` // 数据类型
|
||||
DefaultValue string `json:"default_value"` // 默认值, 以字符串传入, 会转换成 DataType
|
||||
}
|
||||
|
||||
// NewFilter 过滤器实例
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
@ -38,8 +26,8 @@ type MapRule struct {
|
||||
func NewFilter(sourceData string, filterRuleList []MapRule) *filter {
|
||||
return &filter{
|
||||
sourceData: sourceData,
|
||||
formatResult: "{}",
|
||||
filterRuleList: filterRuleList,
|
||||
jsonObj: &gabs.Container{},
|
||||
}
|
||||
}
|
||||
|
||||
@ -50,8 +38,8 @@ func NewFilter(sourceData string, filterRuleList []MapRule) *filter {
|
||||
// Date : 11:58 2022/7/4
|
||||
type filter struct {
|
||||
sourceData string
|
||||
formatResult string
|
||||
filterRuleList []MapRule
|
||||
jsonObj *gabs.Container // 生成的json对象实例
|
||||
}
|
||||
|
||||
// Deal ...
|
||||
@ -59,27 +47,66 @@ type filter struct {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 11:59 2022/7/4
|
||||
func (f *filter) Deal() ([]byte, error) {
|
||||
func (f *filter) Deal() error {
|
||||
var (
|
||||
err error
|
||||
formatVal interface{}
|
||||
)
|
||||
|
||||
for _, rule := range f.filterRuleList {
|
||||
if strings.Contains(rule.SourcePath, "[]") {
|
||||
// 对于list的处理
|
||||
continue
|
||||
}
|
||||
sourceResult := gjson.Get(f.sourceData, rule.SourcePath)
|
||||
sourceVal := sourceResult.String()
|
||||
var (
|
||||
sourceVal string
|
||||
)
|
||||
|
||||
if !sourceResult.Exists() {
|
||||
// 不存在, 使用默认值
|
||||
sourceVal = rule.DefaultValue
|
||||
} else {
|
||||
sourceVal = sourceResult.String()
|
||||
}
|
||||
formatVal, err := f.getValue(rule.DataType, sourceVal)
|
||||
if nil != err {
|
||||
return nil, fmt.Errorf("%s = %v can not convert to %s : %s", rule.SourcePath, sourceResult.Value(), rule.DataType, err.Error())
|
||||
if formatVal, err = f.getValue(rule.DataType, sourceVal); nil != err {
|
||||
return fmt.Errorf("%s = %v can not convert to %s : %s", rule.SourcePath, sourceResult.Value(), rule.DataType, err.Error())
|
||||
}
|
||||
if _, err := f.jsonObj.SetP(formatVal, rule.MapPath); nil != err {
|
||||
return nil, fmt.Errorf("%s set val = %v fail : %s", rule.MapPath, formatVal, err.Error())
|
||||
if _, err = sjson.Set(f.formatResult, rule.MapPath, formatVal); nil != err {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return f.jsonObj.EncodeJSON(), nil
|
||||
return nil
|
||||
}
|
||||
|
||||
// String 获取格式化之后的字符串
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 21:18 2022/12/31
|
||||
func (f *filter) String() string {
|
||||
return f.formatResult
|
||||
}
|
||||
|
||||
// Byte 获取格式化之后的字节数组
|
||||
//
|
||||
// Author : zhangdeman001@ke.com<张德满>
|
||||
//
|
||||
// Date : 21:18 2022/12/31
|
||||
func (f *filter) Byte() []byte {
|
||||
return []byte(f.String())
|
||||
}
|
||||
|
||||
// Parse 解析返回结果
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 21:20 2022/12/31
|
||||
func (f *filter) Parse(receiver interface{}) error {
|
||||
if nil == receiver {
|
||||
return errors.New("receiver is nil")
|
||||
}
|
||||
return json.Unmarshal(f.Byte(), receiver)
|
||||
}
|
||||
|
||||
// getValue 获取值
|
||||
@ -141,4 +168,3 @@ func (f *filter) getValue(dataType string, defaultValue string) (interface{}, er
|
||||
return nil, errors.New(dataType + " is not support!")
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user