util/filter.go

138 lines
3.0 KiB
Go
Raw Permalink Normal View History

2022-07-04 11:58:34 +08:00
// Package util ...
//
// Description : util ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2022-07-04 11:45
package util
2022-07-04 12:30:53 +08:00
import (
2022-07-04 13:37:16 +08:00
"fmt"
2022-07-04 12:30:53 +08:00
"github.com/Jeffail/gabs"
"github.com/pkg/errors"
2022-07-04 13:37:16 +08:00
"github.com/tidwall/gjson"
2022-07-04 12:30:53 +08:00
)
2022-07-04 12:26:02 +08:00
// 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
}
2022-07-04 11:58:34 +08:00
// NewFilter 过滤器实例
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:54 2022/7/4
2022-07-04 13:37:16 +08:00
func NewFilter(sourceData string, filterRuleList []MapRule) *filter {
2022-07-04 11:58:34 +08:00
return &filter{
2022-07-04 12:26:02 +08:00
sourceData: sourceData,
filterRuleList: filterRuleList,
jsonObj: &gabs.Container{},
2022-07-04 11:58:34 +08:00
}
}
// filter 数据过滤
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:58 2022/7/4
type filter struct {
2022-07-04 13:37:16 +08:00
sourceData string
2022-07-04 12:26:02 +08:00
filterRuleList []MapRule
jsonObj *gabs.Container // 生成的json对象实例
}
// Deal ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:59 2022/7/4
func (f *filter) Deal() ([]byte, error) {
2022-07-04 13:37:16 +08:00
for _, rule := range f.filterRuleList {
sourceResult := gjson.Get(f.sourceData, rule.SourcePath)
sourceVal := sourceResult.String()
if !sourceResult.Exists() {
// 不存在, 使用默认值
sourceVal = rule.DefaultValue
}
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 _, err := f.jsonObj.SetP(formatVal, rule.MapPath); nil != err {
return nil, fmt.Errorf("%s set val = %v fail : %s", rule.MapPath, formatVal, err.Error())
}
}
return f.jsonObj.EncodeJSON(), nil
2022-07-04 12:26:02 +08:00
}
2022-07-04 13:37:16 +08:00
// getValue 获取值
2022-07-04 12:26:02 +08:00
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:25 2022/7/4
2022-07-04 13:37:16 +08:00
func (f *filter) getValue(dataType string, defaultValue string) (interface{}, error) {
2022-07-04 12:30:53 +08:00
switch dataType {
case "int8":
fallthrough
case "int16":
fallthrough
case "int32":
fallthrough
case "int64":
fallthrough
case "int":
var (
err error
val int64
)
err = 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 = ConvertAssign(&val, defaultValue)
return val, err
case "bool":
var (
err error
val bool
)
err = ConvertAssign(&val, defaultValue)
return val, err
case "float32":
fallthrough
case "float64":
var (
err error
val float64
)
err = ConvertAssign(&val, defaultValue)
return val, err
2022-07-04 13:37:16 +08:00
case "string":
return defaultValue, nil
2022-07-04 12:30:53 +08:00
default:
return nil, errors.New(dataType + " is not support!")
}
2022-07-04 11:58:34 +08:00
}