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"
|
2023-05-05 15:03:49 +08:00
|
|
|
"git.zhangdeman.cn/zhangdeman/wrapper"
|
2022-07-04 17:50:42 +08:00
|
|
|
"strings"
|
|
|
|
|
2023-02-12 18:26:27 +08:00
|
|
|
"github.com/tidwall/gjson"
|
2023-01-02 22:12:48 +08:00
|
|
|
"github.com/tidwall/sjson"
|
|
|
|
|
2023-01-07 23:24:55 +08:00
|
|
|
"errors"
|
2022-07-04 17:50:42 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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 {
|
2023-01-01 19:28:20 +08:00
|
|
|
if f.IsArray(rule) {
|
2022-07-04 17:50:42 +08:00
|
|
|
// 对于list的处理
|
2023-01-01 19:28:20 +08:00
|
|
|
if err = f.handleArray(rule); nil != err {
|
|
|
|
return err
|
|
|
|
}
|
2022-07-04 17:50:42 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
sourceResult := gjson.Get(f.sourceData, rule.SourcePath)
|
2023-05-04 17:41:55 +08:00
|
|
|
if formatVal, err = f.getValue(rule.DataType, sourceResult, rule.DefaultValue); nil != err {
|
2022-12-31 22:03:19 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-01-01 19:28:20 +08:00
|
|
|
// IsArray 判断是否为数组
|
|
|
|
//
|
|
|
|
// Author : zhangdeman001@ke.com<张德满>
|
|
|
|
//
|
|
|
|
// Date : 17:48 2023/1/1
|
|
|
|
func (f *filter) IsArray(rule MapRule) bool {
|
|
|
|
return strings.Contains(rule.SourcePath, "[]")
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleArray 处理数组(最复杂的场景)
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 17:41 2023/1/1
|
|
|
|
func (f *filter) handleArray(rule MapRule) error {
|
2023-01-02 21:17:51 +08:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
2023-01-01 19:28:20 +08:00
|
|
|
sourcePathArray := strings.Split(rule.SourcePath, "[]")
|
2023-01-02 22:12:48 +08:00
|
|
|
for idx, item := range sourcePathArray {
|
|
|
|
sourcePathArray[idx] = strings.Trim(item, ".")
|
|
|
|
}
|
|
|
|
mapPathArray := strings.Split(strings.TrimRight(rule.MapPath, ".[]"), "[]")
|
|
|
|
for idx, item := range mapPathArray {
|
|
|
|
mapPathArray[idx] = strings.Trim(item, ".")
|
|
|
|
}
|
2023-01-01 19:28:20 +08:00
|
|
|
if len(sourcePathArray) != len(mapPathArray) {
|
|
|
|
if len(mapPathArray) != 1 {
|
|
|
|
return errors.New("map rule is invalid")
|
|
|
|
}
|
|
|
|
// 提取某一个list下的字段, 组成一个list
|
2023-01-02 21:17:51 +08:00
|
|
|
res := make([]string, 0)
|
2023-01-02 22:12:48 +08:00
|
|
|
if len(sourcePathArray[0]) == 0 {
|
|
|
|
f.getAllFinalData(&res, gjson.Parse(f.sourceData).Array(), sourcePathArray[1:])
|
|
|
|
} else {
|
|
|
|
f.getAllFinalData(&res, gjson.Get(f.sourceData, sourcePathArray[0]).Array(), sourcePathArray[1:])
|
|
|
|
}
|
2023-01-02 21:17:51 +08:00
|
|
|
if f.formatResult, err = sjson.Set(f.formatResult, mapPathArray[0], res); nil != err {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-01 19:28:20 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-02 21:17:51 +08:00
|
|
|
// extraFinalResult 提取全部最底层结果
|
2023-01-01 19:28:20 +08:00
|
|
|
//
|
2023-01-02 21:17:51 +08:00
|
|
|
// Author : zhangdeman001@ke.com<张德满>
|
2023-01-01 19:28:20 +08:00
|
|
|
//
|
2023-01-02 21:17:51 +08:00
|
|
|
// Date : 14:00 2023/1/2
|
2023-01-02 22:12:48 +08:00
|
|
|
func (f *filter) getAllFinalData(res *[]string, resultList []gjson.Result, pathArr []string) {
|
|
|
|
if len(pathArr) == 1 {
|
2023-01-02 21:17:51 +08:00
|
|
|
for _, item := range resultList {
|
2023-01-02 22:12:48 +08:00
|
|
|
*res = append(*res, item.Get(pathArr[0]).String())
|
2023-01-02 21:17:51 +08:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, item := range resultList {
|
2023-01-02 22:12:48 +08:00
|
|
|
f.getAllFinalData(res, item.Array(), pathArr[1:])
|
2023-01-02 21:17:51 +08:00
|
|
|
}
|
|
|
|
return
|
2023-01-01 19:28:20 +08:00
|
|
|
}
|
|
|
|
|
2022-12-31 22:03:19 +08:00
|
|
|
// 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
|
2023-05-04 17:41:55 +08:00
|
|
|
func (f *filter) getValue(dataType string, sourceValue gjson.Result, defaultValue string) (interface{}, error) {
|
|
|
|
sourceValueStr := defaultValue
|
|
|
|
if sourceValue.Exists() {
|
2023-05-04 18:18:03 +08:00
|
|
|
str := sourceValue.String()
|
|
|
|
if len(str) > 0 {
|
|
|
|
sourceValueStr = str
|
|
|
|
}
|
2023-05-04 17:41:55 +08:00
|
|
|
}
|
|
|
|
|
2023-05-05 15:03:49 +08:00
|
|
|
strVal := wrapper.String(sourceValueStr)
|
|
|
|
|
2022-07-04 17:50:42 +08:00
|
|
|
switch dataType {
|
2023-05-05 15:03:49 +08:00
|
|
|
case wrapper.DataTypeInt8:
|
|
|
|
return strVal.ToInt8()
|
|
|
|
case wrapper.DataTypeInt16:
|
|
|
|
return strVal.ToInt16()
|
|
|
|
case wrapper.DataTypeInt32:
|
|
|
|
return strVal.ToInt32()
|
|
|
|
case wrapper.DataTypeInt64:
|
|
|
|
return strVal.ToInt64()
|
|
|
|
case wrapper.DataTypeInt:
|
|
|
|
return strVal.ToInt()
|
|
|
|
case wrapper.DataTypeUint8:
|
|
|
|
return strVal.ToUint8()
|
|
|
|
case wrapper.DataTypeUint16:
|
|
|
|
return strVal.ToUint16()
|
|
|
|
case wrapper.DataTypeUint32:
|
|
|
|
return strVal.ToUint32()
|
|
|
|
case wrapper.DataTypeUint64:
|
|
|
|
return strVal.ToUint64()
|
|
|
|
case wrapper.DataTypeUint:
|
|
|
|
return strVal.ToUint()
|
|
|
|
case wrapper.DataTypeBool:
|
|
|
|
return strVal.ToBool()
|
|
|
|
case wrapper.DataTypeFloat32:
|
|
|
|
return strVal.ToFloat64()
|
|
|
|
case wrapper.DataTypeFloat64:
|
|
|
|
fallthrough
|
|
|
|
case wrapper.DataTypeFloat:
|
|
|
|
fallthrough
|
|
|
|
case wrapper.DataTypeDouble:
|
|
|
|
return strVal.ToFloat64()
|
|
|
|
case wrapper.DataTypeNumber:
|
|
|
|
return strVal.ToNumber()
|
|
|
|
case wrapper.DataTypeString:
|
2023-05-04 17:50:45 +08:00
|
|
|
return sourceValueStr, nil
|
2023-05-05 15:03:49 +08:00
|
|
|
case wrapper.DataTypeAny:
|
2023-05-04 17:41:55 +08:00
|
|
|
return sourceValue.Value(), nil
|
2023-05-05 15:03:49 +08:00
|
|
|
case wrapper.DataTypeAnySlice:
|
2023-05-04 18:18:03 +08:00
|
|
|
// 任意类型的list
|
2023-05-05 15:03:49 +08:00
|
|
|
return strVal.ToAnySlice()
|
|
|
|
case wrapper.DataTypeObject:
|
2023-05-04 18:22:54 +08:00
|
|
|
// object
|
2023-05-05 15:03:49 +08:00
|
|
|
return strVal.ToObject()
|
2022-07-04 17:50:42 +08:00
|
|
|
default:
|
|
|
|
return nil, errors.New(dataType + " is not support!")
|
|
|
|
}
|
|
|
|
}
|