数据过滤同时增加脱敏

This commit is contained in:
白茶清欢 2024-04-07 22:37:14 +08:00
parent 14290e2ec6
commit bf45f6dac4
2 changed files with 31 additions and 2 deletions

View File

@ -7,7 +7,10 @@
// Date : 2023-05-23 14:25 // Date : 2023-05-23 14:25
package data_mask package data_mask
import "git.zhangdeman.cn/zhangdeman/data_mask/strategy" import (
"errors"
"git.zhangdeman.cn/zhangdeman/data_mask/strategy"
)
var ( var (
maskInstanceTable = make(map[string]IDataMask) maskInstanceTable = make(map[string]IDataMask)
@ -47,3 +50,19 @@ func Execute(data string, maskType string) string {
} }
return dataMaskInstance.Mask(data) 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
}

View File

@ -10,6 +10,7 @@ package filter
import ( import (
"errors" "errors"
"fmt" "fmt"
"git.zhangdeman.cn/zhangdeman/data_mask"
"git.zhangdeman.cn/zhangdeman/data_mask/define" "git.zhangdeman.cn/zhangdeman/data_mask/define"
"github.com/tidwall/gjson" "github.com/tidwall/gjson"
"github.com/tidwall/sjson" "github.com/tidwall/sjson"
@ -113,8 +114,17 @@ func (t *Transform) rewrite(rule *define.FilterRule) error {
} }
return nil 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()) t.result, err = sjson.Set(t.result, rule.TargetDataPath, sourceResult.Value())
return nil return err
} }
// rewriteForSlice 数组重写 // rewriteForSlice 数组重写