Documentation ¶
Index ¶
- Variables
- type AddressMasker
- type CreditMasker
- type EmailMasker
- type IDMasker
- type Masker
- type MaskerMarshaler
- func (m *MaskerMarshaler) Get(t MaskerType) (Masker, error)
- func (m *MaskerMarshaler) List() []MaskerType
- func (m *MaskerMarshaler) Marshal(t MaskerType, value string) (string, error)
- func (m *MaskerMarshaler) Register(t MaskerType, masker Masker)
- func (m *MaskerMarshaler) SetMasker(masker string)
- func (m *MaskerMarshaler) Struct(s interface{}) (interface{}, error)
- func (m *MaskerMarshaler) Unregister(t MaskerType)
- type MaskerType
- type MobileMasker
- type NameMasker
- type NoneMasker
- type PasswordMasker
- type TelephoneMasker
- type URLMasker
Constants ¶
This section is empty.
Variables ¶
var DefaultMaskerMarshaler = &MaskerMarshaler{ Maskers: map[MaskerType]Masker{ MaskerTypeNone: &NoneMasker{}, MaskerTypePassword: &PasswordMasker{}, MaskerTypeName: &NameMasker{}, MaskerTypeAddress: &AddressMasker{}, MaskerTypeEmail: &EmailMasker{}, MaskerTypeMobile: &MobileMasker{}, MaskerTypeTel: &TelephoneMasker{}, MaskerTypeID: &IDMasker{}, MaskerTypeCredit: &CreditMasker{}, MaskerTypeURL: &URLMasker{}, }, masker: "*", }
DefaultMaskerMarshaler is a default masker marshaler It has default maskers and default masker Default maskers are:
- NoneMasker
- PasswordMasker
- NameMasker
- AddressMasker
- EmailMasker
- MobileMasker
- TelephoneMasker
- IDMasker
- CreditMasker
- URLMasker
Default masker is "*" It is used for masking sensitive data
Functions ¶
This section is empty.
Types ¶
type CreditMasker ¶
type CreditMasker struct{}
type MaskerMarshaler ¶
type MaskerMarshaler struct { Maskers map[MaskerType]Masker // contains filtered or unexported fields }
MaskerMarshaler is a masker marshaler
func NewMaskerMarshaler ¶
func NewMaskerMarshaler() *MaskerMarshaler
NewMaskerMarshaler returns a new masker marshaler It has default maskers and default masker Default maskers are:
- NoneMasker
- PasswordMasker
- NameMasker
- AddressMasker
- EmailMasker
- MobileMasker
- TelephoneMasker
- IDMasker
- CreditMasker
- URLMasker
Default masker is "*" It is used for masking sensitive data
func (*MaskerMarshaler) Get ¶
func (m *MaskerMarshaler) Get(t MaskerType) (Masker, error)
Get returns a masker by masker type It is used for getting a masker by masker type Example:
m := masker.NewMaskerMarshaler() masker, _ := m.Get(masker.MaskerTypePassword) log.Println(masker) // &{PasswordMasker}
func (*MaskerMarshaler) List ¶
func (m *MaskerMarshaler) List() []MaskerType
List returns a list of masker types It is used for listing all masker types Example:
m := masker.NewMaskerMarshaler() log.Println(m.List()) // [password name addr email tel id url none mobile credit]
func (*MaskerMarshaler) Marshal ¶
func (m *MaskerMarshaler) Marshal(t MaskerType, value string) (string, error)
Marshal returns a masked value by masker type It is used for masking sensitive data Example:
m := masker.NewMaskerMarshaler() log.Println(m.Marshal(masker.MaskerTypeNone, "none")) // none <nil> log.Println(m.Marshal(masker.MaskerTypePassword, "password")) // ************** <nil> log.Println(m.Marshal(masker.MaskerTypeName, "name")) // n**e <nil> log.Println(m.Marshal(masker.MaskerTypeAddress, "address")) // addres****** <nil> log.Println(m.Marshal(masker.MaskerTypeEmail, "email")) // ema**** <nil> log.Println(m.Marshal(masker.MaskerTypeMobile, "mobile")) // mobi*** <nil> log.Println(m.Marshal(masker.MaskerTypeTel, "tel")) // tel <nil> log.Println(m.Marshal(masker.MaskerTypeID, "id")) // id**** <nil> log.Println(m.Marshal(masker.MaskerTypeCredit, "4111111111111111")) // 411111******1111 <nil> log.Println(m.Marshal(masker.MaskerTypeURL, "http://john:password@localhost:3000")) // http://john:xxxxx@localhost:3000 <nil>
func (*MaskerMarshaler) Register ¶
func (m *MaskerMarshaler) Register(t MaskerType, masker Masker)
Register adds a masker by masker type It is used for adding or override a masker by masker type Example:
m := masker.NewMaskerMarshaler() m.Register(masker.MaskerTypePassword, &PasswordMasker{}) log.Println(m.List()) // [password name addr email tel id url none mobile credit]
func (*MaskerMarshaler) SetMasker ¶
func (m *MaskerMarshaler) SetMasker(masker string)
func (*MaskerMarshaler) Struct ¶
func (m *MaskerMarshaler) Struct(s interface{}) (interface{}, error)
Struct must input a interface{}, add tag mask on struct fields, after Struct(), return a pointer interface{} of input type and it will be masked with the tag format type
Example:
type Foo struct { Name string `mask:"name"` Email string `mask:"email"` Password string `mask:"password"` ID string `mask:"id"` Address string `mask:"addr"` Mobile string `mask:"mobile"` Telephone string `mask:"tel"` Credit string `mask:"credit"` URL string `mask:"url"` Foo *Foo `mask:"struct"` } func main() { m := masker.NewMaskerMarshaler() log.Println(m.List()) // [password name addr email tel id url none mobile credit] foo1 := &Foo{ Name: "John Doe", Email: "john@gmail.com", Password: "password", ID: "1234567890", Address: "123 Main St", Mobile: "1234567890", Telephone: "1234567890", Credit: "4111111111111111", URL: "http://john:password@localhost:3000", Foo: &Foo{ Name: "John Doe", Email: "john@gmail.com", Password: "password", ID: "1234567890", Address: "123 Main St", Mobile: "1234567890", Telephone: "1234567890", Credit: "4111111111111111", URL: "http://john:password@localhost:3000", }, } foo2, _ := m.Struct(foo1) log.Println(foo1) // &{John Doe john@gmail.com password 1234567890 123 Main St 1234567890 1234567890 4111111111111111 http://john:password@localhost:3000 0xc0000001e0} log.Println(foo1.Foo) // &{John Doe john@gmail.com password 1234567890 123 Main St 1234567890 1234567890 4111111111111111 http://john:password@localhost:3000 <nil>} log.Println(foo2) // &{J**n D**e joh****@gmail.com ************** 123456**** 123 Ma****** 1234***890 (12)3456-**** 411111******1111 http://john:xxxxx@localhost:3000 0xc000000320} log.Println(foo2.(*Foo).Foo) // &{J**n D**e joh****@gmail.com ************** 123456**** 123 Ma****** 1234***890 (12)3456-**** 411111******1111 http://john:xxxxx@localhost:3000 <nil>} }
func (*MaskerMarshaler) Unregister ¶
func (m *MaskerMarshaler) Unregister(t MaskerType)
Unregister removes a masker by masker type It is used for removing a masker by masker type Example:
m := masker.NewMaskerMarshaler() m.Unregister(masker.MaskerTypePassword) log.Println(m.List()) // [name addr email tel id url none mobile credit]
type MaskerType ¶
type MaskerType string
MaskerType is a string type for masker type
const ( MaskerTypeNone MaskerType = "none" MaskerTypePassword MaskerType = "password" MaskerTypeName MaskerType = "name" MaskerTypeAddress MaskerType = "addr" MaskerTypeEmail MaskerType = "email" MaskerTypeMobile MaskerType = "mobile" MaskerTypeTel MaskerType = "tel" MaskerTypeID MaskerType = "id" MaskerTypeCredit MaskerType = "credit" MaskerTypeURL MaskerType = "url" MaskerTypeStruct MaskerType = "struct" )
MaskerType constants
type NameMasker ¶
type NameMasker struct{}
type TelephoneMasker ¶
type TelephoneMasker struct{}
TelephoneMasker is a masker for telephone
func (*TelephoneMasker) Marshal ¶
func (m *TelephoneMasker) Marshal(s string, i string) string
Marshal masks telephone It remove "(", ")", " ", "-" chart, and mask last 4 digits of telephone number, format to "(??)????-????" Example:
TelephoneMasker{}.Marshal("*", "0227993078") // returns "(02)2799-****"