Skip to content

Commit

Permalink
fix: deep copy Any objects
Browse files Browse the repository at this point in the history
Signed-off-by: Charles-Edouard Brétéché <[email protected]>
  • Loading branch information
eddycharly committed Sep 16, 2024
1 parent 59491c9 commit 3e4eac3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/apis/policy/v1alpha1/any.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type Any struct {
}

func (in *Any) DeepCopyInto(out *Any) {
if err := copier.Copy(out, in); err != nil {
if err := copier.CopyWithOption(out, in, copier.Option{DeepCopy: true}); err != nil {
panic("deep copy failed")
}
}
Expand Down
11 changes: 11 additions & 0 deletions pkg/apis/policy/v1alpha1/any_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ func TestAny_DeepCopyInto(t *testing.T) {
assert.Equal(t, tt.in, tt.out)
})
}
{
inner := map[string]any{
"foo": 42,
}
in := Any{map[string]any{"inner": inner}}
out := in.DeepCopy()
inPtr := in.Value.(map[string]any)["inner"].(map[string]any)
inPtr["foo"] = 55
outPtr := out.Value.(map[string]any)["inner"].(map[string]any)
assert.NotEqual(t, inPtr, outPtr)
}
}

func TestAny_MarshalJSON(t *testing.T) {
Expand Down

0 comments on commit 3e4eac3

Please sign in to comment.