Skip to content

Commit

Permalink
fix: copy nill slices correctly in DeepCopy
Browse files Browse the repository at this point in the history
  • Loading branch information
jvmakine committed Jan 8, 2025
1 parent d7895a1 commit 3549f17
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
3 changes: 3 additions & 0 deletions common/reflect/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ func copyAny(src any, ptrs map[uintptr]any, copyConf *copyConfig) (dst any) {

// Special case list.List to handle its internal structure
if reflect.TypeOf(src) == reflect.TypeFor[*list.List]() {
if v.IsNil() {
return nil
}
return copyList(src.(*list.List), ptrs, copyConf)
}

Expand Down
5 changes: 5 additions & 0 deletions common/reflect/reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ func TestCopyStructOfPointers(t *testing.T) {
}
}

func TestNilSliceCopyIsNil(t *testing.T) {
copy := DeepCopy([]int(nil))

Check warning on line 92 in common/reflect/reflect_test.go

View workflow job for this annotation

GitHub Actions / Lint

redefines-builtin-id: redefinition of the built-in function copy (revive)
assert.Assert(t, copy == nil)
}

func testEqualityOfStruct(t *testing.T, expected, actual *structOfPointers) {
t.Helper()
if expected == nil || actual == nil {
Expand Down

0 comments on commit 3549f17

Please sign in to comment.