diff --git a/execute.go b/execute.go index 5a8447c..5639356 100644 --- a/execute.go +++ b/execute.go @@ -7,7 +7,10 @@ // Date : 2023-05-23 14:25 package data_mask -import "git.zhangdeman.cn/zhangdeman/data_mask/strategy" +import ( + "errors" + "git.zhangdeman.cn/zhangdeman/data_mask/strategy" +) var ( maskInstanceTable = make(map[string]IDataMask) @@ -47,3 +50,19 @@ func Execute(data string, maskType string) string { } return dataMaskInstance.Mask(data) } + +// ExecuteWithError 严格执行 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 22:33 2024/4/7 +func ExecuteWithError(data string, maskType string) (string, error) { + dataMaskInstance, exist := maskInstanceTable[maskType] + if !exist { + return "", errors.New(maskType + " : data mask type is not found") + } + if nil == dataMaskInstance { + return "", errors.New(maskType + " : data mask type instance is nil") + } + return Execute(data, maskType), nil +} diff --git a/filter/filter.go b/filter/filter.go index 4d7bd8b..d5c7f6b 100644 --- a/filter/filter.go +++ b/filter/filter.go @@ -10,6 +10,7 @@ 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" @@ -113,8 +114,17 @@ func (t *Transform) rewrite(rule *define.FilterRule) error { } return nil } + + // 数据脱敏 + realValue := sourceResult.String() + if len(rule.DataMaskStrategy) > 0 { + if realValue, err = data_mask.ExecuteWithError(realValue, rule.DataMaskStrategy); nil != err { + return err + } + } + // TODO : 格式转换 t.result, err = sjson.Set(t.result, rule.TargetDataPath, sourceResult.Value()) - return nil + return err } // rewriteForSlice 数组重写