Skip to content

Commit cb9dfb3

Browse files
authored
fix PutChange with nil value (#6)
1 parent 6124af8 commit cb9dfb3

File tree

3 files changed

+43
-6
lines changed

3 files changed

+43
-6
lines changed

put_change.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,14 @@ func PutChange(ch *Changeset, field string, value interface{}, opts ...Option) {
1616
options.apply(opts)
1717

1818
if typ, exist := ch.types[field]; exist {
19-
if value != (interface{})(nil) {
19+
if value != nil {
2020
valTyp := reflect.TypeOf(value)
2121
if valTyp.Kind() == reflect.Ptr {
22+
if reflect.ValueOf(value).IsNil() {
23+
ch.changes[field] = nil
24+
return
25+
}
26+
2227
valTyp = valTyp.Elem()
2328
}
2429

@@ -27,7 +32,7 @@ func PutChange(ch *Changeset, field string, value interface{}, opts ...Option) {
2732
return
2833
}
2934
} else {
30-
ch.changes[field] = reflect.Zero(reflect.PtrTo(typ)).Interface()
35+
ch.changes[field] = value
3136
return
3237
}
3338
}

put_change_test.go

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"testing"
66

77
"github.com/go-rel/changeset/params"
8+
"github.com/go-rel/rel"
89
"github.com/stretchr/testify/assert"
910
)
1011

@@ -40,26 +41,59 @@ func TestPutChange(t *testing.T) {
4041
assert.Equal(t, 10, ch.Changes()["field1"])
4142
}
4243

44+
func TestPutChange_ptr(t *testing.T) {
45+
var (
46+
nullable = false
47+
a struct {
48+
ID int
49+
Nullable *bool
50+
}
51+
)
52+
53+
ch := Cast(a, params.Map{}, []string{})
54+
PutChange(ch, "nullable", &nullable)
55+
56+
assert.Nil(t, ch.Error())
57+
assert.Equal(t, &nullable, ch.Changes()["nullable"])
58+
59+
assert.NotPanics(t, func() {
60+
rel.Apply(rel.NewDocument(&a), ch)
61+
assert.Equal(t, &nullable, a.Nullable)
62+
})
63+
}
64+
4365
func TestPutChange_nil(t *testing.T) {
4466
var a struct {
67+
ID int
4568
Nullable *bool
4669
}
4770

4871
ch := Cast(a, params.Map{}, []string{})
4972
PutChange(ch, "nullable", nil)
5073

5174
assert.Nil(t, ch.Error())
52-
assert.Equal(t, (*bool)(nil), ch.Changes()["nullable"])
75+
assert.Equal(t, nil, ch.Changes()["nullable"])
76+
77+
assert.NotPanics(t, func() {
78+
rel.Apply(rel.NewDocument(&a), ch)
79+
assert.Nil(t, a.Nullable)
80+
})
5381
}
5482

5583
func TestPutChange_typedNil(t *testing.T) {
5684
var a struct {
85+
ID int
5786
Nullable *bool
5887
}
5988

6089
ch := Cast(a, params.Map{}, []string{})
6190
PutChange(ch, "nullable", (*bool)(nil))
6291

6392
assert.Nil(t, ch.Error())
64-
assert.Equal(t, (*bool)(nil), ch.Changes()["nullable"])
93+
assert.Equal(t, nil, ch.Changes()["nullable"])
94+
95+
assert.NotPanics(t, func() {
96+
rel.Apply(rel.NewDocument(&a), ch)
97+
assert.Nil(t, a.Nullable)
98+
})
6599
}

validate_required_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package changeset
22

33
import (
4-
"fmt"
54
"strings"
65
"testing"
76
"time"
@@ -90,7 +89,6 @@ func TestValidateRequired_cast_error(t *testing.T) {
9089
}
9190

9291
ct := customType{Field1: "field1"}
93-
fmt.Println(ct.FieldTime)
9492
ch := Cast(ct, params.Map{"field2": "field2"}, []string{"field1", "field2", "field3", "field_time"})
9593
ValidateRequired(ch, []string{"field1", "field2", "field3", "field_time"})
9694

0 commit comments

Comments
 (0)