Skip to content

Commit 2eb23ac

Browse files
authored
fix: update to nil pointer (#78)
1 parent aa40c07 commit 2eb23ac

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

change_value.go

+2
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ func (c *ChangeValue) Set(value reflect.Value, convertCompatibleTypes bool) {
158158
} else {
159159
c.target.Set(value)
160160
}
161+
} else if c.target.Kind() == reflect.Ptr {
162+
c.target.Set(reflect.Zero(c.target.Type()))
161163
} else if !c.target.IsZero() {
162164
t := c.target.Elem()
163165
t.Set(reflect.Zero(t.Type()))

patch_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,12 @@ func TestPatch(t *testing.T) {
126126
nil,
127127
},
128128
{
129+
"struct-string-pointer-update-to-nil", &tstruct{Pointer: sptr("test")}, &tstruct{Pointer: nil},
130+
diff.Changelog{
131+
diff.Change{Type: diff.UPDATE, Path: []string{"pointer"}, From: sptr("test"), To: nil},
132+
},
133+
nil,
134+
}, {
129135
"struct-generic-slice-insert", &tstruct{Values: []string{"one"}}, &tstruct{Values: []string{"one", "two"}},
130136
diff.Changelog{
131137
diff.Change{Type: diff.CREATE, Path: []string{"values", "1"}, From: nil, To: "two"},

0 commit comments

Comments
 (0)