Documentation ¶
Overview ¶
Example ¶
rand.Seed(12345) type Address struct { PostCode string `mask:"zero"` } type User struct { ID string Name string `mask:"filled"` Age int `mask:"random100"` Address Address } user := User{ ID: "123456", Name: "Usagi", Age: 3, Address: Address{ PostCode: "123-4567", }, } maskUser, err := Mask(user) if err != nil { fmt.Println(err) return } fmt.Printf("%+v", maskUser)
Output: {ID:123456 Name:***** Age:83 Address:{PostCode:}}
Example (CustomMaskFunc) ¶
package main import ( "fmt" "regexp" "strings" "unicode/utf8" mask "github.com/showa-93/go-mask" ) func init() { maskTypeRegExp := "regexp" mask.RegisterMaskStringFunc(maskTypeRegExp, MaskRegExp) } // MaskRegExp is sample to add a custom mask function func MaskRegExp(arg, value string) (string, error) { var ( reg *regexp.Regexp err error ) reg, err = regexp.Compile(arg) if err != nil { return "", err } indexes := reg.FindStringSubmatchIndex(value) if len(indexes) >= 4 && indexes[2] >= 0 && indexes[3] >= 0 { var sb strings.Builder sb.WriteString(value[:indexes[2]]) sb.WriteString(strings.Repeat(mask.MaskChar(), utf8.RuneCountInString(value[indexes[2]:indexes[3]]))) sb.WriteString(value[indexes[3]:]) return sb.String(), nil } return value, nil } func main() { mask.SetMaskChar("■") type Hachiware struct { Message string `mask:"regexp(最高)."` } input := Hachiware{Message: "これって…最高じゃん"} got, _ := mask.Mask(input) fmt.Printf("\"%s\", Hachiware says\n", got.Message) }
Output: "これって…■■じゃん", Hachiware says
Index ¶
- Constants
- func Float64(tag string, value float64) (float64, error)
- func Int(tag string, value int) (int, error)
- func Mask[T any](target T) (ret T, err error)
- func MaskChar() string
- func RegisterMaskAnyFunc(maskType string, maskFunc MaskAnyFunc)
- func RegisterMaskField(fieldName, maskType string)
- func RegisterMaskFloat64Func(maskType string, maskFunc MaskFloat64Func)
- func RegisterMaskIntFunc(maskType string, maskFunc MaskIntFunc)
- func RegisterMaskStringFunc(maskType string, maskFunc MaskStringFunc)
- func RegisterMaskUintFunc(maskType string, maskFunc MaskUintFunc)
- func SetMaskChar(s string)
- func String(tag, value string) (string, error)
- func Uint(tag string, value uint) (uint, error)
- type MaskAnyFunc
- type MaskFloat64Func
- type MaskIntFunc
- type MaskStringFunc
- type MaskUintFunc
- type Masker
- func (m *Masker) Cache(enable bool)
- func (m *Masker) Float64(tag string, value float64) (float64, error)
- func (m *Masker) Int(tag string, value int) (int, error)
- func (m *Masker) Mask(target any) (ret any, err error)
- func (m *Masker) MaskChar() string
- func (m *Masker) MaskFilledString(arg, value string) (string, error)
- func (m *Masker) MaskFixedString(arg, value string) (string, error)
- func (m *Masker) MaskHashString(arg, value string) (string, error)
- func (m *Masker) MaskRandomFloat64(arg string, value float64) (float64, error)
- func (m *Masker) MaskRandomInt(arg string, value int) (int, error)
- func (m *Masker) MaskZero(arg string, value any) (any, error)
- func (m *Masker) RegisterMaskAnyFunc(maskType string, maskFunc MaskAnyFunc)
- func (m *Masker) RegisterMaskField(fieldName, maskType string)
- func (m *Masker) RegisterMaskFloat64Func(maskType string, maskFunc MaskFloat64Func)
- func (m *Masker) RegisterMaskIntFunc(maskType string, maskFunc MaskIntFunc)
- func (m *Masker) RegisterMaskStringFunc(maskType string, maskFunc MaskStringFunc)
- func (m *Masker) RegisterMaskUintFunc(maskType string, maskFunc MaskUintFunc)
- func (m *Masker) SetMaskChar(s string)
- func (m *Masker) SetTagName(s string)
- func (m *Masker) String(tag, value string) (string, error)
- func (m *Masker) Uint(tag string, value uint) (uint, error)
Examples ¶
Constants ¶
const ( MaskTypeFilled = "filled" MaskTypeFixed = "fixed" MaskTypeRandom = "random" MaskTypeHash = "hash" MaskTypeZero = "zero" )
Default tag that can be specified as a mask
const TagName = "mask"
Tag name of the field in the structure when masking
Variables ¶
This section is empty.
Functions ¶
func Mask ¶
Mask returns an object with the mask applied to any given object. The function's argument can accept any type, including pointer, map, and slice types, in addition to struct. from default masker.
func MaskChar ¶
func MaskChar() string
MaskChar returns the current character used for masking. from default masker.
func RegisterMaskAnyFunc ¶
func RegisterMaskAnyFunc(maskType string, maskFunc MaskAnyFunc)
RegisterMaskAnyFunc registers a masking function that can be applied to any type. The function will be applied when the string set in the first argument is assigned as a tag to a field in the structure. from default masker.
func RegisterMaskField ¶ added in v0.4.0
func RegisterMaskField(fieldName, maskType string)
RegisterMaskField allows you to register a mask tag to be applied to the value of a struct field or map key that matches the fieldName. If a mask tag is set on the struct field, it will take precedence. from default masker.
Example ¶
rand.Seed(12345) type User2 struct { ID string Name string Age int ExtData map[string]string } user := User2{ ID: "123456", Name: "Usagi", Age: 3, ExtData: map[string]string{ "ID": "123456", "Favorite": "Cat", }, } RegisterMaskField("ID", "zero") RegisterMaskField("Age", "random100") RegisterMaskField("Name", "filled4") RegisterMaskField("Favorite", "filled6") maskUser, err := Mask(user) if err != nil { fmt.Println(err) return } fmt.Printf("%+v", maskUser)
Output: {ID: Name:**** Age:83 ExtData:map[Favorite:****** ID:]}
func RegisterMaskFloat64Func ¶
func RegisterMaskFloat64Func(maskType string, maskFunc MaskFloat64Func)
RegisterMaskFloat64Func registers a masking function for float64 values. The function will be applied when the string set in the first argument is assigned as a tag to a field in the structure. from default masker.
func RegisterMaskIntFunc ¶
func RegisterMaskIntFunc(maskType string, maskFunc MaskIntFunc)
RegisterMaskIntFunc registers a masking function for int values. The function will be applied when the string set in the first argument is assigned as a tag to a field in the structure. from default masker.
func RegisterMaskStringFunc ¶
func RegisterMaskStringFunc(maskType string, maskFunc MaskStringFunc)
RegisterMaskStringFunc registers a masking function for string values. The function will be applied when the string set in the first argument is assigned as a tag to a field in the structure. from default masker.
func RegisterMaskUintFunc ¶ added in v0.5.0
func RegisterMaskUintFunc(maskType string, maskFunc MaskUintFunc)
RegisterMaskUintFunc registers a masking function for uint values. The function will be applied when the string set in the first argument is assigned as a tag to a field in the structure. from default masker.
func SetMaskChar ¶
func SetMaskChar(s string)
SetMaskChar changes the character used for masking from default masker.
Types ¶
type MaskAnyFunc ¶
Function type that must be satisfied to add a custom mask
type MaskFloat64Func ¶
Function type that must be satisfied to add a custom mask
type MaskIntFunc ¶
Function type that must be satisfied to add a custom mask
type MaskStringFunc ¶
Function type that must be satisfied to add a custom mask
type MaskUintFunc ¶ added in v0.5.0
Function type that must be satisfied to add a custom mask
type Masker ¶ added in v0.2.0
type Masker struct {
// contains filtered or unexported fields
}
Masker is a struct that defines the masking process.
func (*Masker) Cache ¶ added in v0.6.0
Cache can be toggled to cache the type information of the struct. default true
func (*Masker) Mask ¶ added in v0.2.0
Mask returns an object with the mask applied to any given object. The function's argument can accept any type, including pointer, map, and slice types, in addition to struct.
func (*Masker) MaskFilledString ¶ added in v0.3.0
MaskFilledString masks the string length of the value with the same length. If you pass a number like "2" to arg, it masks with the length of the number.(**)
func (*Masker) MaskFixedString ¶ added in v0.3.0
MaskFixedString masks with a fixed length (8 characters).
func (*Masker) MaskHashString ¶ added in v0.3.0
MaskHashString masks and hashes (sha1) a string.
func (*Masker) MaskRandomFloat64 ¶ added in v0.3.0
MaskRandomFloat64 converts a float64 to a random number. For example, if you pass "100.3" to arg, it sets a random number in the range of 0.000 to 99.999.
func (*Masker) MaskRandomInt ¶ added in v0.3.0
MaskRandomInt converts an integer (int) into a random number. For example, if you pass "100" as the arg, it sets a random number in the range of 0-99.
func (*Masker) RegisterMaskAnyFunc ¶ added in v0.2.0
func (m *Masker) RegisterMaskAnyFunc(maskType string, maskFunc MaskAnyFunc)
RegisterMaskAnyFunc registers a masking function that can be applied to any type. The function will be applied when the string set in the first argument is assigned as a tag to a field in the structure.
func (*Masker) RegisterMaskField ¶ added in v0.4.0
RegisterMaskField allows you to register a mask tag to be applied to the value of a struct field or map key that matches the fieldName. If a mask tag is set on the struct field, it will take precedence.
func (*Masker) RegisterMaskFloat64Func ¶ added in v0.2.0
func (m *Masker) RegisterMaskFloat64Func(maskType string, maskFunc MaskFloat64Func)
RegisterMaskFloat64Func registers a masking function for float64 values. The function will be applied when the string set in the first argument is assigned as a tag to a field in the structure.
func (*Masker) RegisterMaskIntFunc ¶ added in v0.2.0
func (m *Masker) RegisterMaskIntFunc(maskType string, maskFunc MaskIntFunc)
RegisterMaskIntFunc registers a masking function for int values. The function will be applied when the string set in the first argument is assigned as a tag to a field in the structure.
func (*Masker) RegisterMaskStringFunc ¶ added in v0.2.0
func (m *Masker) RegisterMaskStringFunc(maskType string, maskFunc MaskStringFunc)
RegisterMaskStringFunc registers a masking function for string values. The function will be applied when the string set in the first argument is assigned as a tag to a field in the structure.
func (*Masker) RegisterMaskUintFunc ¶ added in v0.5.0
func (m *Masker) RegisterMaskUintFunc(maskType string, maskFunc MaskUintFunc)
RegisterMaskUintFunc registers a masking function for uint values. The function will be applied when the uint slice set in the first argument is assigned as a tag to a field in the structure.
func (*Masker) SetMaskChar ¶ added in v0.2.0
SetMaskChar changes the character used for masking
func (*Masker) SetTagName ¶ added in v0.4.0
SetTagName allows you to change the tag name from "mask" to something else.