+
Skip to content

Mask certain keys #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions repr.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ func IgnoreGoStringer() Option { return func(o *Printer) { o.ignoreGoStringer =
// IgnorePrivate disables private field members from output.
func IgnorePrivate() Option { return func(o *Printer) { o.ignorePrivate = true } }

// MaskKeys removes certain keys from output.
func MaskKeys(keys ...string) Option {
return func(o *Printer) {
o.excludeKeys = keys
}
}

// ScalarLiterals forces the use of literals for scalars, rather than a string representation if available.
//
// For example, `time.Hour` will be printed as `time.Duration(3600000000000)` rather than `time.Duration(1h0m0s)`.
Expand Down Expand Up @@ -102,6 +109,7 @@ type Printer struct {
exclude map[reflect.Type]bool
w io.Writer
useLiterals bool
excludeKeys []string
}

// New creates a new Printer on w with the given Options.
Expand Down Expand Up @@ -271,6 +279,25 @@ func (p *Printer) reprValue(seen map[reflect.Value]bool, v reflect.Value, indent
}
previous = true
fmt.Fprintf(p.w, "%s%s: ", ni, t.Name)

// ignore specific Keye
if p.excludeKeys != nil {
skip := false
for _, k := range p.excludeKeys {
if k == t.Name {
skip = true
break
}
}
if skip {
fmt.Fprint(p.w, "***")
if p.indent != "" {
fmt.Fprintf(p.w, ",\n")
}
continue
}
}

p.reprValue(seen, f, ni, true, t.Type == anyType)

// if private fields should be ignored, look up if a public
Expand Down
10 changes: 10 additions & 0 deletions repr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,16 @@ func TestReprPrivateMixedIgnorePrivate(t *testing.T) {
equal(t, `repr.mixedTestStruct{A: "hello", C: "goodbye"}`, String(s, IgnorePrivate()))
}

func TestReprExcludeKeys(t *testing.T) {
s := mixedTestStruct{"hello", "world", "goodbye", "cruel world"}
equal(t, `repr.mixedTestStruct{A: "hello", C: ***}`, String(s, MaskKeys("C"), IgnorePrivate()))
}

func TestReprExcludeKeys2(t *testing.T) {
s := mixedTestStruct{"hello", "world", "goodbye", "cruel world"}
equal(t, `repr.mixedTestStruct{A: "hello", b: ***, C: ***, _D: ***}`, String(s, MaskKeys("C", "b", "_D")))
}

func TestReprNilAlone(t *testing.T) {
var err error
s := String(err)
Expand Down
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载