-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmasq.go
72 lines (57 loc) · 1.42 KB
/
masq.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package masq
import (
"context"
"reflect"
"log/slog"
)
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]"
)
type masq struct {
redactMessage string
filters []*Filter
allowedTypes map[reflect.Type]struct{}
defaultRedactor Redactor
tagKey string
}
type Filter struct {
censor Censor
redactors Redactors
}
type Option func(m *masq)
func newMasq(options ...Option) *masq {
m := &masq{
redactMessage: DefaultRedactMessage,
allowedTypes: map[reflect.Type]struct{}{},
tagKey: DefaultTagKey,
}
m.defaultRedactor = func(src, dst reflect.Value) bool {
switch src.Kind() {
case reflect.String:
dst.Elem().SetString(m.redactMessage)
}
return true
}
for _, opt := range options {
opt(m)
}
return m
}
func (x *masq) redact(k string, v any) any {
if v == nil {
return nil
}
ctx := context.Background()
copied := x.clone(ctx, k, reflect.ValueOf(v), "")
return copied.Interface()
}
func New(options ...Option) func(groups []string, a slog.Attr) slog.Attr {
m := newMasq(options...)
return func(groups []string, attr slog.Attr) slog.Attr {
masked := m.redact(attr.Key, attr.Value.Any())
return slog.Any(attr.Key, masked)
}
}