增加数据类型转换

This commit is contained in:
2024-04-08 17:31:18 +08:00
parent bd2957255f
commit 231d2539e1
4 changed files with 138 additions and 1 deletions

90
filter/data_type.go Normal file
View File

@ -0,0 +1,90 @@
// Package filter ...
//
// Description : filter ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2024-04-08 16:49
package filter
import (
"errors"
"git.zhangdeman.cn/zhangdeman/consts"
"git.zhangdeman.cn/zhangdeman/serialize"
"git.zhangdeman.cn/zhangdeman/wrapper"
"github.com/tidwall/gjson"
"strings"
)
// format 数据格式的转换
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:50 2024/4/8
func format(inputValue gjson.Result, targetType string) (interface{}, error) {
switch strings.ToLower(targetType) {
case consts.DataTypeAny: // 任意类型
return inputValue.Value(), nil
case consts.DataTypeString: // string
return inputValue, nil
case consts.DataTypeInt: // int
res := wrapper.String(inputValue.String()).ToInt64()
return res.Value, res.Err
case consts.DataTypeUint: // uint
res := wrapper.String(inputValue.String()).ToUint()
return res.Value, res.Err
case consts.DataTypeFloat: // float64
res := wrapper.String(inputValue.String()).ToFloat64()
return res.Value, res.Err
case consts.DataTypeBool: // bool
res := wrapper.String(inputValue.String()).ToBool()
return res.Value, res.Err
case consts.DataTypeSliceString: // []string
res := wrapper.String(inputValue.String()).ToStringSlice()
return res.Value, res.Err
case consts.DataTypeSliceInt: // []int64
res := wrapper.String(inputValue.String()).ToInt64Slice()
return res.Value, res.Err
case consts.DataTypeSliceUint: // []uint64
res := wrapper.String(inputValue.String()).ToUint64Slice()
return res.Value, res.Err
case consts.DataTypeSliceFloat: // []float
res := wrapper.String(inputValue.String()).ToFloat64Slice()
return res.Value, res.Err
case consts.DataTypeSliceBool: // []float
res := wrapper.String(inputValue.String()).ToBoolSlice()
return res.Value, res.Err
case consts.DataTypeMapStrBool: // map[string]bool
var res map[string]bool
err := serialize.JSON.UnmarshalWithNumber([]byte(inputValue.String()), &res)
return res, err
case consts.DataTypeMapStrInt: // map[string]int64
var res map[string]int64
err := serialize.JSON.UnmarshalWithNumber([]byte(inputValue.String()), &res)
return res, err
case consts.DataTypeMapStrAny: // map[string]interface{}
var res map[string]interface{}
err := serialize.JSON.UnmarshalWithNumber([]byte(inputValue.String()), &res)
return res, err
case consts.DataTypeMapStrFloat: // map[string]float64
var res map[string]float64
err := serialize.JSON.UnmarshalWithNumber([]byte(inputValue.String()), &res)
return res, err
case consts.DataTypeMapStrUint: // map[string]uint64
var res map[string]uint64
err := serialize.JSON.UnmarshalWithNumber([]byte(inputValue.String()), &res)
return res, err
case consts.DataTypeMapStrSlice: // map[string]uint64
var res map[string][]interface{}
err := serialize.JSON.UnmarshalWithNumber([]byte(inputValue.String()), &res)
return res, err
case consts.DataTypeMapAnyAny: // map[interface{}]interface{}
res := make(map[interface{}]interface{})
inputValue.ForEach(func(key, value gjson.Result) bool {
res[key.Value()] = value.Value()
return true
})
return res, nil
}
return nil, errors.New(targetType + " : is not support")
}

View File

@ -109,6 +109,7 @@ func (t *Transform) rewrite(rule *define.FilterRule) error {
if strings.Contains(rule.SourceDataPath, "[]") {
return t.rewriteForSlice(rule)
}
// 走的默认值认为无需脱敏
if t.result, err = sjson.Set(t.result, rule.TargetDataPath, rule.DefaultValue); nil != err {
return err
}
@ -121,6 +122,9 @@ func (t *Transform) rewrite(rule *define.FilterRule) error {
if realValue, err = data_mask.ExecuteWithError(realValue, rule.DataMaskStrategy); nil != err {
return err
}
// 一旦执行数据脱敏, 统一以字符串输出
t.result, err = sjson.Set(t.result, rule.TargetDataPath, realValue)
return err
}
// TODO : 格式转换
t.result, err = sjson.Set(t.result, rule.TargetDataPath, sourceResult.Value())
@ -133,11 +137,17 @@ func (t *Transform) rewrite(rule *define.FilterRule) error {
//
// Date : 15:22 2024/3/27
func (t *Transform) rewriteForSlice(rule *define.FilterRule) error {
var (
err error
)
pathArr := strings.Split(rule.SourceDataPath, ".[].")
sliceUnfoldResult := t.unfoldSourceSliceData(t.sourceData, pathArr[0:len(pathArr)-1], nil)
targetArr := strings.Split(rule.TargetDataPath, ".[].")
for idx, itemRes := range sliceUnfoldResult {
t.result, _ = sjson.Set(t.result, fmt.Sprintf("%v.%v.%v", targetArr[0], idx, targetArr[1]), itemRes.Get(pathArr[len(pathArr)-1]).Value())
if t.result, err = sjson.Set(t.result, fmt.Sprintf("%v.%v.%v", targetArr[0], idx, targetArr[1]), itemRes.Get(pathArr[len(pathArr)-1]).Value()); nil != err {
return err
}
}
return nil
}