-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathredact.go
48 lines (40 loc) · 1.19 KB
/
redact.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
package conf
import (
"fmt"
"reflect"
)
// Redact returns a copy of v with all fields zeroed out unless they are marked
// as "noredact". Redact does not modify v.
//
// A field in v will be copied over as-is only if it is exported and has a
// "conf" tag of the form:
//
// Foo string `conf:"foo,noredact"`
//
// This is the same syntax Load supports. If ",noredact" is not present, then
// Redact will not copy the field over. Unexported fields will always be zero.
//
// Redact will recursively apply to struct-valued fields in v only if that field
// is marked as "noredact". Otherwise, the entire field is left as zero.
func Redact[T any](v T) T {
val := reflect.ValueOf(v)
if val.Kind() != reflect.Struct {
panic(fmt.Errorf("conf: Redact called on %v (only structs are acceptable)", val.Kind()))
}
return redact(val).Interface().(T)
}
func redact(v reflect.Value) reflect.Value {
t := v.Type()
if t.Kind() != reflect.Struct {
return v
}
out := reflect.New(t)
for i := 0; i < t.NumField(); i++ {
_, noredact := parseConfTag(t.Field(i).Tag.Get(tagConf))
if !noredact || !out.Elem().Field(i).CanSet() {
continue
}
out.Elem().Field(i).Set(redact(v.Field(i)))
}
return out.Elem()
}