Skip to content

Commit

Permalink
Update default tag key with custom tag key handling
Browse files Browse the repository at this point in the history
  • Loading branch information
m-mizutani committed Nov 12, 2024
1 parent 37d8c50 commit 968546f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
6 changes: 5 additions & 1 deletion masq.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import (
)

const (
// DefaultTagKey is a default key name of struct tag for masq. WithCustomTagKey option can change this value.
DefaultTagKey = "masq"

// DefaultRedactMessage is a default message to replace redacted value. WithRedactMessage option can change this value.
DefaultRedactMessage = "[REDACTED]"
)

Expand All @@ -31,7 +35,7 @@ func newMasq(options ...Option) *masq {
m := &masq{
redactMessage: DefaultRedactMessage,
allowedTypes: map[reflect.Type]struct{}{},
tagKey: "masq",
tagKey: DefaultTagKey,
}
m.defaultRedactor = func(src, dst reflect.Value) bool {
switch src.Kind() {
Expand Down
6 changes: 5 additions & 1 deletion options.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,12 @@ func WithTag(tag string, redactors ...Redactor) Option {
return WithCensor(newTagCensor(tag), redactors...)
}

// WithCustomTagKey is an option to set the custom tag key. The default tag key is `masq`.
// WithCustomTagKey is an option to set the custom tag key. The default tag key is `masq`. If the field has the target tag in the custom tag key AND the field is matched with the target tag specified by WithTag, the field will be redacted. If tagKey is empty, WithCustomTagKey panics.
func WithCustomTagKey(tagKey string) Option {
if tagKey == "" {
panic("masq: tag key must not be empty")
}

return func(m *masq) {
m.tagKey = tagKey
}
Expand Down
10 changes: 10 additions & 0 deletions options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,16 @@ func ExampleWithCustomTagKey() {
// {"level":"INFO","msg":"Got record","record":{"EMail":"[REDACTED]","ID":"m-mizutani"},"time":"2022-12-25T09:00:00.123456789"}
}

func TestCustomTagKeyPanic(t *testing.T) {
defer func() {
if recover() == nil {
t.Errorf("Failed to panic")
}
}()

masq.New(masq.WithCustomTagKey(""))
}

func ExampleWithFieldName() {
out := &fixedTimeWriter{}

Expand Down

0 comments on commit 968546f

Please sign in to comment.