From 582283faf9713c55ca303ca989b16a480f2edb64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Mon, 23 Sep 2024 16:17:54 +0800 Subject: [PATCH] code clean --- filter/data_type.go | 90 ---------------------- filter/filter.go | 178 -------------------------------------------- 2 files changed, 268 deletions(-) delete mode 100644 filter/data_type.go delete mode 100644 filter/filter.go diff --git a/filter/data_type.go b/filter/data_type.go deleted file mode 100644 index 938d9d5..0000000 --- a/filter/data_type.go +++ /dev/null @@ -1,90 +0,0 @@ -// 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") -} diff --git a/filter/filter.go b/filter/filter.go deleted file mode 100644 index 91e810c..0000000 --- a/filter/filter.go +++ /dev/null @@ -1,178 +0,0 @@ -// Package filter ... -// -// Description : filter ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 2024-04-07 21:59 -package filter - -import ( - "errors" - "fmt" - "git.zhangdeman.cn/zhangdeman/data_mask" - "git.zhangdeman.cn/zhangdeman/data_mask/define" - "github.com/tidwall/gjson" - "github.com/tidwall/sjson" - "strings" -) - -// Execute 数据转换 -// -// Author : go_developer@163.com<张德满> -// -// Date : 12:39 2024/3/26 -func Execute(sourceData string, convertRule []*define.FilterRule) *Transform { - sourceData = strings.TrimSpace(sourceData) - t := &Transform{ - sourceData: sourceData, - ruleList: convertRule, - result: "{}", - } - return t -} - -// Transform 转换 -// -// Author : go_developer@163.com<张德满> -// -// Date : 10:56 2024/3/27 -type Transform struct { - sourceData string - ruleList []*define.FilterRule - result string -} - -// Execute 执行 -// -// Author : go_developer@163.com<张德满> -// -// Date : 10:57 2024/3/27 -func (t *Transform) Execute() error { - sourceResult := gjson.Parse(t.sourceData) - if !sourceResult.IsObject() { - return errors.New("source data is not object") - } - if err := t.checkRule(); nil != err { - return err - } - for _, itemRule := range t.ruleList { - if err := t.rewrite(itemRule); nil != err { - return err - } - } - return nil -} - -// GetResult 获取处理结果 -// -// Author : go_developer@163.com<张德满> -// -// Date : 11:32 2024/3/27 -func (t *Transform) GetResult() string { - return t.result -} - -// checkRule 校验规则 -// -// Author : go_developer@163.com<张德满> -// -// Date : 11:18 2024/3/27 -func (t *Transform) checkRule() error { - for _, itemRule := range t.ruleList { - itemRule.SourceDataPath = strings.TrimRight(itemRule.SourceDataPath, ".[]") - itemRule.TargetDataPath = strings.TrimRight(itemRule.TargetDataPath, ".[]") - fromPathArr := strings.Split(itemRule.SourceDataPath, "[]") - targetPathArr := strings.Split(itemRule.TargetDataPath, "[]") - if len(targetPathArr) > len(fromPathArr) { - return errors.New("slice path config invalid : " + itemRule.SourceDataPath + " -> " + itemRule.TargetDataPath) - } - } - return nil -} - -// rewrite 数据重写 -// -// Author : go_developer@163.com<张德满> -// -// Date : 11:25 2024/3/27 -func (t *Transform) rewrite(rule *define.FilterRule) error { - var ( - err error - ) - - sourceResult := gjson.Get(t.sourceData, rule.SourceDataPath) - if !sourceResult.Exists() { - if rule.IsRequired { - return errors.New(rule.SourceDataPath + " is not exist, but required") - } - 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 - } - return nil - } - - // 数据脱敏 - realValue := sourceResult.String() - if len(rule.DataMaskStrategy) > 0 { - 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()) - return err -} - -// rewriteForSlice 数组重写 -// -// Author : go_developer@163.com<张德满> -// -// 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 { - 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 -} - -// unfoldSourceSliceData 展开元数据的数组, 平铺 -// -// Author : go_developer@163.com<张德满> -// -// Date : 17:39 2024/3/27 -func (t *Transform) unfoldSourceSliceData(sourceData string, pathList []string, res []gjson.Result) []gjson.Result { - if nil == res { - res = make([]gjson.Result, 0) - } - - arrRes := gjson.Get(sourceData, pathList[0]).Array() - if len(pathList) == 1 { - for _, itemRes := range arrRes { - res = append(res, itemRes) - } - return res - } - - for i := 0; i < len(arrRes)-1; i++ { - res = t.unfoldSourceSliceData(arrRes[i].String(), pathList[1:], res) - } - - return t.unfoldSourceSliceData(arrRes[len(arrRes)-1].String(), pathList[1:], res) -}