Skip to content

Commit 02235df

Browse files
committed
support TrustedValue
1 parent dbc828e commit 02235df

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

components/scrubber/scrubber.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -202,12 +202,19 @@ func (s *scrubberImpl) Struct(val any) error {
202202
return nil
203203
}
204204

205-
func (s *scrubberImpl) deepCopyStruct(fieldName string, src reflect.Value, scrubTag string) reflect.Value {
205+
func (s *scrubberImpl) deepCopyStruct(fieldName string, src reflect.Value, scrubTag string, skipScrub bool) reflect.Value {
206206
if src.Kind() == reflect.Ptr && src.IsNil() {
207207
return reflect.New(src.Type()).Elem()
208208
}
209209

210-
if src.Kind() == reflect.String {
210+
if src.CanInterface() {
211+
value := src.Interface()
212+
if _, ok := value.(TrustedValue); ok {
213+
skipScrub = true
214+
}
215+
}
216+
217+
if src.Kind() == reflect.String && !skipScrub {
211218
dst := reflect.New(src.Type())
212219
var (
213220
setExplicitValue bool
@@ -241,7 +248,6 @@ func (s *scrubberImpl) deepCopyStruct(fieldName string, src reflect.Value, scrub
241248
}
242249

243250
switch src.Kind() {
244-
245251
case reflect.Struct:
246252
dst := reflect.New(src.Type())
247253
t := src.Type()
@@ -275,7 +281,7 @@ func (s *scrubberImpl) deepCopyStruct(fieldName string, src reflect.Value, scrub
275281
}
276282

277283
tagValue := f.Tag.Get("scrub")
278-
copied := s.deepCopyStruct(f.Name, srcValue, tagValue)
284+
copied := s.deepCopyStruct(f.Name, srcValue, tagValue, skipScrub)
279285
dstValue.Set(copied)
280286
}
281287
return dst.Elem()
@@ -285,31 +291,31 @@ func (s *scrubberImpl) deepCopyStruct(fieldName string, src reflect.Value, scrub
285291
keys := src.MapKeys()
286292
for i := 0; i < src.Len(); i++ {
287293
mValue := src.MapIndex(keys[i])
288-
dst.SetMapIndex(keys[i], s.deepCopyStruct(keys[i].String(), mValue, ""))
294+
dst.SetMapIndex(keys[i], s.deepCopyStruct(keys[i].String(), mValue, "", skipScrub))
289295
}
290296
return dst
291297

292298
case reflect.Slice:
293299
dst := reflect.MakeSlice(src.Type(), src.Len(), src.Cap())
294300
for i := 0; i < src.Len(); i++ {
295-
dst.Index(i).Set(s.deepCopyStruct(fieldName, src.Index(i), ""))
301+
dst.Index(i).Set(s.deepCopyStruct(fieldName, src.Index(i), "", skipScrub))
296302
}
297303
return dst
298304

299305
case reflect.Array:
300306
if src.Len() == 0 {
301-
return src // can not access to src.Index(0)
307+
return src
302308
}
303309

304310
dst := reflect.New(src.Type()).Elem()
305311
for i := 0; i < src.Len(); i++ {
306-
dst.Index(i).Set(s.deepCopyStruct(fieldName, src.Index(i), ""))
312+
dst.Index(i).Set(s.deepCopyStruct(fieldName, src.Index(i), "", skipScrub))
307313
}
308314
return dst
309315

310316
case reflect.Ptr:
311317
dst := reflect.New(src.Elem().Type())
312-
copied := s.deepCopyStruct(fieldName, src.Elem(), scrubTag)
318+
copied := s.deepCopyStruct(fieldName, src.Elem(), scrubTag, skipScrub)
313319
dst.Elem().Set(copied)
314320
return dst
315321

@@ -322,7 +328,7 @@ func (s *scrubberImpl) deepCopyStruct(fieldName string, src reflect.Value, scrub
322328

323329
// Struct implements Scrubber
324330
func (s *scrubberImpl) DeepCopyStruct(val any) any {
325-
return s.deepCopyStruct("", reflect.ValueOf(val), "").Interface()
331+
return s.deepCopyStruct("", reflect.ValueOf(val), "", false).Interface()
326332
}
327333

328334
func (s *scrubberImpl) scrubJsonObject(val map[string]interface{}) error {

0 commit comments

Comments
 (0)