Skip to content

Commit 320ba14

Browse files
committed
fixing patch issue r3labs#63
1 parent 04a5761 commit 320ba14

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

change_value.go

+4
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ func (c *ChangeValue) Set(value reflect.Value, convertCompatibleTypes bool) {
123123
}
124124
c.target.Set(value.Convert(c.target.Type()))
125125
} else {
126+
if !value.IsValid() && (c.target.Kind() == reflect.Ptr || c.target.Kind() == reflect.Interface) {
127+
c.target.Set(reflect.Zero(c.target.Type()))
128+
return
129+
}
126130
c.target.Set(value)
127131
}
128132
c.SetFlag(FlagApplied)

diff_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ type tstruct struct {
7373
Map map[string]string `diff:"map"`
7474
Time time.Time `diff:"time"`
7575
Pointer *string `diff:"pointer"`
76+
ValuesPointer *[]string `diff:"values_pointer"`
7677
Ignored bool `diff:"-"`
7778
Identifiables []tistruct `diff:"identifiables"`
7879
Unidentifiables []tuistruct `diff:"unidentifiables"`
@@ -84,6 +85,10 @@ func sptr(s string) *string {
8485
return &s
8586
}
8687

88+
func saptr(s []string) *[]string {
89+
return &s
90+
}
91+
8792
func TestDiff(t *testing.T) {
8893
cases := []struct {
8994
Name string

patch_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,13 @@ func TestPatch(t *testing.T) {
123123
},
124124
nil,
125125
},
126+
{
127+
"struct-string-slice-pointer-nil-update", &tstruct{ValuesPointer: saptr([]string{"foo"})}, &tstruct{ValuesPointer: nil},
128+
Changelog{
129+
Change{Type: UPDATE, Path: []string{"values_pointer"}, From: sptr("test"), To: nil},
130+
},
131+
nil,
132+
},
126133
{
127134
"struct-generic-slice-insert", &tstruct{Values: []string{"one"}}, &tstruct{Values: []string{"one", "two"}},
128135
Changelog{

0 commit comments

Comments
 (0)