增加数据类型转换

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
}

12
go.mod
View File

@ -8,6 +8,18 @@ require (
)
require (
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240408083510-54975506dff0 // indirect
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20240311030808-e2a2e6a3c211 // indirect
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240325080031-1f58204e8687 // indirect
git.zhangdeman.cn/zhangdeman/util v0.0.0-20231227095334-7eb5cdbf9253 // indirect
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240320040704-e125c7e75dfb // indirect
github.com/BurntSushi/toml v1.3.2 // indirect
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 // indirect
github.com/go-ini/ini v1.67.0 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mozillazg/go-pinyin v0.20.0 // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

25
go.sum
View File

@ -1,3 +1,25 @@
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240408083510-54975506dff0 h1:MqZVuOaReq6IGWkwUzTOw7qzVhRlIrJVi9E2J6pWit0=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240408083510-54975506dff0/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20240311030808-e2a2e6a3c211 h1:I/wOsRpCSRkU9vo1u703slQsmK0wnNeZzsWQOGtIAG0=
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20240311030808-e2a2e6a3c211/go.mod h1:SrtvrQRdzt+8KfYzvosH++gWxo2ShPTzR1m3VQ6uX7U=
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240325080031-1f58204e8687 h1:uQcGqdzi4UdpZlp4f4FUPeBqoygP58pEKJkmN3ROsE0=
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240325080031-1f58204e8687/go.mod h1:gf7SW2TXATgux8pfdFedMkXWv2515OtIIM/5c4atkFw=
git.zhangdeman.cn/zhangdeman/util v0.0.0-20231227095334-7eb5cdbf9253 h1:GO3oZa5a2sqwAzGcLDJtQzmshSWRmoP7IDS8bwFqvC4=
git.zhangdeman.cn/zhangdeman/util v0.0.0-20231227095334-7eb5cdbf9253/go.mod h1:VpPjBlwz8U+OxZuxzHQBv1aEEZ3pStH6bZvT21ADEbI=
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240320040704-e125c7e75dfb h1:B9KrV6bvHpIVeDwxIz/t3t/fZNr4+k2GQ8h0Ppp/mgI=
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240320040704-e125c7e75dfb/go.mod h1:W2Dk+WLkUL6MzVS4XRKz9qZ7jfw25tqoXm04eDzGIKw=
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 h1:OYA+5W64v3OgClL+IrOD63t4i/RW7RqrAVl9LTZ9UqQ=
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394/go.mod h1:Q8n74mJTIgjX4RBBcHnJ05h//6/k6foqmgE45jTQtxg=
github.com/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A=
github.com/go-ini/ini v1.67.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mozillazg/go-pinyin v0.20.0 h1:BtR3DsxpApHfKReaPO1fCqF4pThRwH9uwvXzm+GnMFQ=
github.com/mozillazg/go-pinyin v0.20.0/go.mod h1:iR4EnMMRXkfpFVV5FMi4FNB6wGq9NV6uDWbUuPhP4Yc=
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/gjson v1.17.1 h1:wlYEnwqAHgzmhNUFfw7Xalt2JzQvsMx2Se4PcoFCT/U=
github.com/tidwall/gjson v1.17.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
@ -8,3 +30,6 @@ github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4=
github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY=
github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=