diff --git a/abstract.go b/abstract.go new file mode 100644 index 0000000..88546f3 --- /dev/null +++ b/abstract.go @@ -0,0 +1,28 @@ +// Package data_mask ... +// +// Description : data_mask ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2023-05-23 14:23 +package data_mask + +// IDataMask 接口约束 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 14:23 2023/5/23 +type IDataMask interface { + // Mask 将输入的input, 转换成输出数据 + // + // Author : go_developer@163.com<白茶清欢> + // + // Date : 14:24 2023/5/23 + Mask(input string) string + // Type 数据脱敏类型 + // + // Author : go_developer@163.com<白茶清欢> + // + // Date : 14:24 2023/5/23 + Type() string +} diff --git a/default.go b/default.go new file mode 100644 index 0000000..029e2a2 --- /dev/null +++ b/default.go @@ -0,0 +1,24 @@ +// Package data_mask ... +// +// Description : data_mask ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2023-05-23 14:25 +package data_mask + +// DefaultDataMask 默认实现 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 14:27 2023/5/23 +type DefaultDataMask struct { +} + +func (d DefaultDataMask) Mask(input string) string { + return input +} + +func (d DefaultDataMask) Type() string { + return TypeDefault +} diff --git a/define.go b/define.go new file mode 100644 index 0000000..febd542 --- /dev/null +++ b/define.go @@ -0,0 +1,13 @@ +// Package data_mask ... +// +// Description : data_mask ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2023-05-23 14:26 +package data_mask + +const ( + // TypeDefault 默认实现 + TypeDefault = "DEFAULT_DATA_MASK" +) diff --git a/execute.go b/execute.go new file mode 100644 index 0000000..9de32f5 --- /dev/null +++ b/execute.go @@ -0,0 +1,33 @@ +// Package data_mask ... +// +// Description : data_mask ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2023-05-23 14:25 +package data_mask + +var ( + maskInstanceTable = make(map[string]IDataMask) + defaultDataMask = &DefaultDataMask{} +) + +func init() { + maskInstanceList := []IDataMask{} + for _, item := range maskInstanceList { + maskInstanceTable[item.Type()] = item + } +} + +// Execute 执行数据脱敏 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 14:31 2023/5/23 +func Execute(data string, maskType string) string { + dataMaskInstance, exist := maskInstanceTable[maskType] + if !exist || nil == dataMaskInstance { + dataMaskInstance = defaultDataMask + } + return dataMaskInstance.Mask(data) +}