Compare commits

..

No commits in common. "feature/filter" and "master" have entirely different histories.

8 changed files with 13 additions and 168 deletions

View File

@ -1,23 +0,0 @@
// Package define ...
//
// Description : define ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2024-04-07 21:43
package define
// FilterRule ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 21:43 2024/4/7
type FilterRule struct {
SourceDataPath string `json:"source_data_path"` // 原始数据路径
TargetDataPath string `json:"target_data_path"` // 目标数据路径
IsRequired bool `json:"is_required"` // 要求字段必须存在
AllowNil bool `json:"allow_nil"` // 允许nil值
DefaultValue string `json:"default_value"` // 不存在时的默认值
Type string `json:"type"` // 数据类型
DataMaskStrategy string `json:"data_mask_strategy"` // 数据脱敏策略
}

View File

@ -8,11 +8,14 @@
package define
const (
TypeDefault = "DEFAULT_DATA_MASK" // 默认实现
TypeMail = "MAIL_DATA_MASK" // 邮箱数据脱敏
TypePhone = "PHONE_DATA_MASK" // 手机号脱敏
TypePassword = "PASSWORD_DATA_MASK" // 密码数据脱敏
TypeName = "NAME_DATA_MASK" // 姓名数据脱敏
// TypeDefault 默认实现
TypeDefault = "DEFAULT_DATA_MASK"
// TypeMail 邮箱数据脱敏
TypeMail = "MAIL_DATA_MASK"
// TypePhone 手机号脱敏
TypePhone = "PHONE_DATA_MASK"
// TypePassword 密码数据脱敏
TypePassword = "PASSWORD_DATA_MASK"
)
// TypeDesc 脱敏类型描述
@ -31,6 +34,5 @@ var (
{TypeMail, "邮箱数据脱敏"},
{TypePhone, "手机号数据脱敏"},
{TypePassword, "密码数据脱敏"},
{TypeName, "姓名数据脱敏"},
}
)

View File

@ -7,10 +7,7 @@
// Date : 2023-05-23 14:25
package data_mask
import (
"errors"
"git.zhangdeman.cn/zhangdeman/data_mask/strategy"
)
import "git.zhangdeman.cn/zhangdeman/data_mask/strategy"
var (
maskInstanceTable = make(map[string]IDataMask)
@ -19,28 +16,14 @@ var (
func init() {
maskInstanceList := []IDataMask{
&strategy.MailDataMask{}, // 邮箱脱敏
&strategy.PhoneDataMask{}, // 手机号脱敏
&strategy.PasswordDataMask{}, // 密码脱敏
&DefaultDataMask{}, // 默认脱敏
&strategy.NameDataMask{}, // 姓名脱敏
&strategy.MailDataMask{}, // 邮箱脱敏
&strategy.PhoneDataMask{}, // 手机号脱敏
}
for _, item := range maskInstanceList {
maskInstanceTable[item.Type()] = item
}
}
// RegisterStrategy 注册数据脱敏策略
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 22:18 2024/4/7
func RegisterStrategy(dataMaskStrategy ...IDataMask) {
for _, item := range dataMaskStrategy {
maskInstanceTable[item.Type()] = item
}
}
// Execute 执行数据脱敏
//
// Author : go_developer@163.com<白茶清欢>
@ -53,19 +36,3 @@ 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
}

22
go.mod
View File

@ -1,25 +1,3 @@
module git.zhangdeman.cn/zhangdeman/data_mask
go 1.21
require (
github.com/tidwall/gjson v1.17.1
github.com/tidwall/sjson v1.2.5
)
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
)

35
go.sum
View File

@ -1,35 +0,0 @@
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=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
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=

View File

@ -1,4 +1,4 @@
// Package strategy ...
// Package data_mask ...
//
// Description : data_mask ...
//

View File

@ -1,4 +1,4 @@
// Package strategy ...
// Package data_mask ...
//
// Description : data_mask ...
//

View File

@ -1,44 +0,0 @@
// Package strategy ...
//
// Description : strategy ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2024-09-23 16:25
package strategy
import (
"git.zhangdeman.cn/zhangdeman/data_mask/define"
"strings"
)
// NameDataMask 姓名脱敏
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:30 2024/9/23
type NameDataMask struct {
}
func (n *NameDataMask) Mask(input string) string {
if len(input) == 0 {
return ""
}
nameArr := strings.Split(input, "")
if len(nameArr) == 2 {
return nameArr[0] + "*"
}
return nameArr[0] + n.getMaskString(len(nameArr)) + nameArr[len(nameArr)-1]
}
func (n *NameDataMask) Type() string {
return define.TypeName
}
func (n *NameDataMask) getMaskString(nameLength int) string {
maskStr := ""
for i := 0; i < nameLength-2; i++ {
maskStr = maskStr + "*"
}
return maskStr
}