Skip to content

Commit 0b3880e

Browse files
authored
[scrubber] skip if interface is nil (#20418)
* [scrubber] skip if interface is nil * update
1 parent 26d2b1c commit 0b3880e

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

components/scrubber/scrubber.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,9 @@ func (s *scrubberImpl) deepCopyStruct(fieldName string, src reflect.Value, scrub
388388
return dst
389389

390390
case reflect.Interface:
391+
if src.IsNil() {
392+
return src
393+
}
391394
dst := reflect.New(src.Elem().Type())
392395
copied := s.deepCopyStruct(fieldName, src.Elem(), scrubTag, skipScrub)
393396
dst.Elem().Set(copied)

components/scrubber/scrubber_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,42 @@ func TestDeepCopyStruct(t *testing.T) {
444444
},
445445
CmpOpts: []cmp.Option{cmpopts.IgnoreUnexported(UnexportedStructToTest{})},
446446
},
447+
{
448+
Name: "nil interface",
449+
Struct: &struct {
450+
Hashed string `scrub:"hash"`
451+
NilInterface interface{}
452+
}{
453+
Hashed: "foo",
454+
},
455+
Expectation: Expectation{
456+
Result: &struct {
457+
Hashed string `scrub:"hash"`
458+
NilInterface interface{}
459+
}{
460+
Hashed: "[redacted:md5:acbd18db4cc2f85cedef654fccc4a4d8]",
461+
NilInterface: nil,
462+
},
463+
},
464+
},
465+
{
466+
Name: "nil point interface",
467+
Struct: &struct {
468+
Hashed string `scrub:"hash"`
469+
NilInterface *string
470+
}{
471+
Hashed: "foo",
472+
},
473+
Expectation: Expectation{
474+
Result: &struct {
475+
Hashed string `scrub:"hash"`
476+
NilInterface *string
477+
}{
478+
Hashed: "[redacted:md5:acbd18db4cc2f85cedef654fccc4a4d8]",
479+
NilInterface: nil,
480+
},
481+
},
482+
},
447483
}
448484

449485
for _, test := range tests {

0 commit comments

Comments
 (0)