-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclone.go
147 lines (121 loc) · 3.38 KB
/
clone.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package masq
import (
"context"
"reflect"
"unsafe"
)
type ctxKeyDepth struct{}
const (
maxDepth = 32
)
var (
// ignoreTypes is a map of types that should not be redacted. It lists types that can not be copied. For example, reflect.Type is a pointer to a struct and copying it causes panic. Especially, reflect.rtype is unexported type. Then, the ignoreTypes is list of string of type name.
ignoreTypes = map[string]struct{}{
"*reflect.rtype": {},
}
)
func (x *masq) clone(ctx context.Context, fieldName string, src reflect.Value, tag string) reflect.Value {
if v, ok := ctx.Value(ctxKeyDepth{}).(int); !ok {
ctx = context.WithValue(ctx, ctxKeyDepth{}, 0)
} else {
if v >= maxDepth {
return src
}
ctx = context.WithValue(ctx, ctxKeyDepth{}, v+1)
}
if _, ok := x.allowedTypes[src.Type()]; ok {
return src
}
if _, ok := ignoreTypes[src.Type().String()]; ok {
return src
}
if src.Kind() == reflect.Ptr && src.IsNil() {
return reflect.New(src.Type()).Elem()
}
for _, filter := range x.filters {
if filter.censor(fieldName, src.Interface(), tag) {
dst := reflect.New(src.Type())
if !filter.redactors.Redact(src, dst) {
_ = x.defaultRedactor(src, dst)
}
if !dst.CanInterface() {
return dst
}
return dst.Elem()
}
}
switch src.Kind() {
case reflect.String:
dst := reflect.New(src.Type())
dst.Elem().SetString(src.String())
return dst.Elem()
case reflect.Struct:
dst := reflect.New(src.Type())
t := src.Type()
for i := 0; i < t.NumField(); i++ {
f := t.Field(i)
srcValue := src.Field(i)
dstValue := dst.Elem().Field(i)
if !srcValue.CanInterface() {
dstValue = reflect.NewAt(dstValue.Type(), unsafe.Pointer(dstValue.UnsafeAddr())).Elem()
if !srcValue.CanAddr() {
switch {
case srcValue.CanInt():
dstValue.SetInt(srcValue.Int())
case srcValue.CanUint():
dstValue.SetUint(srcValue.Uint())
case srcValue.CanFloat():
dstValue.SetFloat(srcValue.Float())
case srcValue.CanComplex():
dstValue.SetComplex(srcValue.Complex())
case srcValue.Kind() == reflect.Bool:
dstValue.SetBool(srcValue.Bool())
}
continue
}
srcValue = reflect.NewAt(srcValue.Type(), unsafe.Pointer(srcValue.UnsafeAddr())).Elem()
}
tagValue := f.Tag.Get(x.tagKey)
copied := x.clone(ctx, f.Name, srcValue, tagValue)
dstValue.Set(copied)
}
return dst.Elem()
case reflect.Map:
dst := reflect.MakeMap(src.Type())
keys := src.MapKeys()
for i := 0; i < src.Len(); i++ {
mValue := src.MapIndex(keys[i])
dst.SetMapIndex(keys[i], x.clone(ctx, keys[i].String(), mValue, ""))
}
return dst
case reflect.Slice:
dst := reflect.MakeSlice(src.Type(), src.Len(), src.Cap())
for i := 0; i < src.Len(); i++ {
dst.Index(i).Set(x.clone(ctx, fieldName, src.Index(i), ""))
}
return dst
case reflect.Array:
if src.Len() == 0 {
return src // can not access to src.Index(0)
}
dst := reflect.New(src.Type()).Elem()
for i := 0; i < src.Len(); i++ {
dst.Index(i).Set(x.clone(ctx, fieldName, src.Index(i), ""))
}
return dst
case reflect.Ptr:
dst := reflect.New(src.Elem().Type())
copied := x.clone(ctx, fieldName, src.Elem(), tag)
dst.Elem().Set(copied)
return dst
case reflect.Interface:
if src.IsNil() {
return src
}
return x.clone(ctx, fieldName, src.Elem(), tag)
default:
dst := reflect.New(src.Type())
dst.Elem().Set(src)
return dst.Elem()
}
}