From bf45f6dac4163852f1f634937c03bac40a9e960f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Sun, 7 Apr 2024 22:37:14 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B0=E6=8D=AE=E8=BF=87=E6=BB=A4=E5=90=8C?= =?UTF-8?q?=E6=97=B6=E5=A2=9E=E5=8A=A0=E8=84=B1=E6=95=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- execute.go | 21 ++++++++++++++++++++- filter/filter.go | 12 +++++++++++- 2 files changed, 31 insertions(+), 2 deletions(-) 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 数组重写