Skip to content

Commit

Permalink
fix: update to nil pointer (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
23doors authored Feb 3, 2022
1 parent aa40c07 commit 2eb23ac
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
2 changes: 2 additions & 0 deletions change_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ func (c *ChangeValue) Set(value reflect.Value, convertCompatibleTypes bool) {
} else {
c.target.Set(value)
}
} else if c.target.Kind() == reflect.Ptr {
c.target.Set(reflect.Zero(c.target.Type()))
} else if !c.target.IsZero() {
t := c.target.Elem()
t.Set(reflect.Zero(t.Type()))
Expand Down
6 changes: 6 additions & 0 deletions patch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@ func TestPatch(t *testing.T) {
nil,
},
{
"struct-string-pointer-update-to-nil", &tstruct{Pointer: sptr("test")}, &tstruct{Pointer: nil},
diff.Changelog{
diff.Change{Type: diff.UPDATE, Path: []string{"pointer"}, From: sptr("test"), To: nil},
},
nil,
}, {
"struct-generic-slice-insert", &tstruct{Values: []string{"one"}}, &tstruct{Values: []string{"one", "two"}},
diff.Changelog{
diff.Change{Type: diff.CREATE, Path: []string{"values", "1"}, From: nil, To: "two"},
Expand Down

0 comments on commit 2eb23ac

Please sign in to comment.