-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.go
More file actions
46 lines (39 loc) · 1.03 KB
/
options.go
File metadata and controls
46 lines (39 loc) · 1.03 KB
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
package plog
import (
"encoding/base64"
"golang.org/x/exp/slog"
"google.golang.org/protobuf/reflect/protoreflect"
)
// Option is a log option.
type Option func(options) options
// Skip returns an [Option] that is used to skip fields based on a predicate.
func Skip(predicate func(protoreflect.FieldDescriptor) bool) Option {
return func(o options) options {
o.skip = predicate
return o
}
}
// Bytes returns an [Option] that is be used to customise how []byte slices
// are printed. The default is base64 encoded string.
func Bytes(transform func([]byte) slog.Value) Option {
return func(o options) options {
o.bytes = transform
return o
}
}
type options struct {
skip func(protoreflect.FieldDescriptor) bool
bytes func([]byte) slog.Value
}
func newOptions(ops []Option) options {
o := options{
skip: func(protoreflect.FieldDescriptor) bool { return false },
bytes: func(b []byte) slog.Value {
return slog.StringValue(base64.StdEncoding.EncodeToString(b))
},
}
for _, apply := range ops {
o = apply(o)
}
return o
}