json_filter/filter.go

171 lines
3.3 KiB
Go
Raw Normal View History

2022-07-04 17:50:42 +08:00
// Package filter ...
//
// Description : filter ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2022-07-04 17:47
package filter
import (
2022-12-31 22:03:19 +08:00
"encoding/json"
2022-07-04 17:50:42 +08:00
"fmt"
2022-12-31 22:03:19 +08:00
"github.com/tidwall/sjson"
2022-07-04 17:50:42 +08:00
"strings"
"git.zhangdeman.cn/zhangdeman/util"
"github.com/pkg/errors"
"github.com/tidwall/gjson"
)
// NewFilter 过滤器实例
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:54 2022/7/4
func NewFilter(sourceData string, filterRuleList []MapRule) *filter {
return &filter{
sourceData: sourceData,
2022-12-31 22:03:19 +08:00
formatResult: "{}",
2022-07-04 17:50:42 +08:00
filterRuleList: filterRuleList,
}
}
// filter 数据过滤
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:58 2022/7/4
type filter struct {
sourceData string
2022-12-31 22:03:19 +08:00
formatResult string
2022-07-04 17:50:42 +08:00
filterRuleList []MapRule
}
// Deal ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:59 2022/7/4
2022-12-31 22:03:19 +08:00
func (f *filter) Deal() error {
var (
err error
formatVal interface{}
)
2022-07-04 17:50:42 +08:00
for _, rule := range f.filterRuleList {
if strings.Contains(rule.SourcePath, "[]") {
// 对于list的处理
continue
}
sourceResult := gjson.Get(f.sourceData, rule.SourcePath)
2022-12-31 22:03:19 +08:00
var (
sourceVal string
)
2022-07-04 17:50:42 +08:00
if !sourceResult.Exists() {
// 不存在, 使用默认值
sourceVal = rule.DefaultValue
2022-12-31 22:03:19 +08:00
} else {
sourceVal = sourceResult.String()
2022-07-04 17:50:42 +08:00
}
2022-12-31 22:03:19 +08:00
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())
2022-07-04 17:50:42 +08:00
}
2023-01-01 17:31:25 +08:00
if f.formatResult, err = sjson.Set(f.formatResult, rule.MapPath, formatVal); nil != err {
2022-12-31 22:03:19 +08:00
return err
2022-07-04 17:50:42 +08:00
}
}
2022-12-31 22:03:19 +08:00
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)
2022-07-04 17:50:42 +08:00
}
// getValue 获取值
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:25 2022/7/4
func (f *filter) getValue(dataType string, defaultValue string) (interface{}, error) {
switch dataType {
case "int8":
fallthrough
case "int16":
fallthrough
case "int32":
fallthrough
case "int64":
fallthrough
case "int":
var (
err error
val int64
)
err = util.ConvertAssign(&val, defaultValue)
return val, err
case "uint8":
fallthrough
case "uint16":
fallthrough
case "uint32":
fallthrough
case "uint64":
fallthrough
case "uint":
var (
err error
val uint64
)
err = util.ConvertAssign(&val, defaultValue)
return val, err
case "bool":
var (
err error
val bool
)
err = util.ConvertAssign(&val, defaultValue)
return val, err
case "float32":
fallthrough
case "float64":
var (
err error
val float64
)
err = util.ConvertAssign(&val, defaultValue)
return val, err
case "string":
return defaultValue, nil
default:
return nil, errors.New(dataType + " is not support!")
}
}