mirror of https://github.com/VinGarcia/ksql.git
Fix behavior of json modifier to match what json.Unmarshal does for nil values
In the original implementation I thought that the `json` package would set fields to its zero value during unmarshal if the input JSON had a `null` value for that attribute. After performing an small experiment I noticed this is not the case. Thus, I have removed that behavior and now if the json contains a `null` value for a field this field will be ignored by the json modifier thus keeping its original value whatever it was.pull/29/head
parent
e48f82c255
commit
136ee66fe9
|
@ -4,7 +4,6 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// This modifier serializes objects as JSON when
|
// This modifier serializes objects as JSON when
|
||||||
|
@ -13,10 +12,6 @@ import (
|
||||||
var jsonModifier = AttrModifier{
|
var jsonModifier = AttrModifier{
|
||||||
Scan: func(ctx context.Context, opInfo OpInfo, attrPtr interface{}, dbValue interface{}) error {
|
Scan: func(ctx context.Context, opInfo OpInfo, attrPtr interface{}, dbValue interface{}) error {
|
||||||
if dbValue == nil {
|
if dbValue == nil {
|
||||||
v := reflect.ValueOf(attrPtr)
|
|
||||||
// Set the struct to its 0 value just like json.Unmarshal
|
|
||||||
// does for nil attributes:
|
|
||||||
v.Elem().Set(reflect.Zero(reflect.TypeOf(attrPtr).Elem()))
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -21,9 +21,11 @@ func TestAttrScan(t *testing.T) {
|
||||||
expectErrToContain []string
|
expectErrToContain []string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
desc: "should set struct to zero value if input is nil",
|
desc: "should not set struct to zero value if input is nil",
|
||||||
dbInput: nil,
|
dbInput: nil,
|
||||||
expectedValue: FakeAttr{},
|
expectedValue: FakeAttr{
|
||||||
|
Foo: "notZeroValue",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
desc: "should work when input is a byte slice",
|
desc: "should work when input is a byte slice",
|
||||||
|
|
Loading…
Reference in New Issue