Skip to content

Commit

Permalink
Merge pull request #3 from timonwong/fix-nullint64
Browse files Browse the repository at this point in the history
Fix NullInt64 json deserialization
  • Loading branch information
timonwong committed Jul 20, 2017
2 parents 2235002 + 530488a commit 51d14e5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
2 changes: 1 addition & 1 deletion dbr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func reset(sess *Session) {
fmt.Sprintf(`CREATE TABLE null_types (
id %s,
string_val varchar(255) NULL,
int64_val integer NULL,
int64_val bigint NULL,
float64_val float NULL,
time_val timestamp NULL ,
bool_val bool NULL
Expand Down
4 changes: 3 additions & 1 deletion types.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ func (n *NullString) UnmarshalJSON(b []byte) error {
// UnmarshalJSON correctly deserializes a NullInt64 from JSON
func (n *NullInt64) UnmarshalJSON(b []byte) error {
var s interface{}
if err := json.Unmarshal(b, &s); err != nil {
dec := json.NewDecoder(bytes.NewReader(b))
dec.UseNumber()
if err := dec.Decode(&s); err != nil {
return err
}
return n.Scan(s)
Expand Down
31 changes: 24 additions & 7 deletions types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
var (
filledRecord = nullTypedRecord{
StringVal: NewNullString("wow"),
Int64Val: NewNullInt64(42),
Int64Val: NewNullInt64(4211223344),
Float64Val: NewNullFloat64(1.618),
TimeVal: NewNullTime(time.Date(2009, 1, 3, 18, 15, 5, 0, time.UTC)),
BoolVal: NewNullBool(true),
Expand All @@ -31,11 +31,16 @@ func TestNullTypesScanning(t *testing.T) {
for _, sess := range testSession {
test.in.Id = nextID()
_, err := sess.InsertInto("null_types").Columns("id", "string_val", "int64_val", "float64_val", "time_val", "bool_val").Record(test.in).Exec()
assert.NoError(t, err)
if !assert.NoError(t, err) {
continue
}

var record nullTypedRecord
err = sess.Select("*").From("null_types").Where(Eq("id", test.in.Id)).LoadStruct(&record)
assert.NoError(t, err)
if !assert.NoError(t, err) {
continue
}

if sess.Dialect == dialect.PostgreSQL {
// TODO: https://github.com/lib/pq/issues/329
if !record.TimeVal.Time.IsZero() {
Expand Down Expand Up @@ -70,7 +75,13 @@ func TestNullTypesJSON(t *testing.T) {
in: &filledRecord.Int64Val,
in2: filledRecord.Int64Val,
out: new(NullInt64),
want: "42",
want: "4211223344",
},
{
in: new(NullInt64),
in2: NullInt64{},
out: new(NullInt64),
want: "null",
},
{
in: &filledRecord.StringVal,
Expand All @@ -87,17 +98,23 @@ func TestNullTypesJSON(t *testing.T) {
} {
// marshal ptr
b, err := json.Marshal(test.in)
assert.NoError(t, err)
if !assert.NoError(t, err) {
return
}
assert.Equal(t, test.want, string(b))

// marshal value
b, err = json.Marshal(test.in2)
assert.NoError(t, err)
if !assert.NoError(t, err) {
return
}
assert.Equal(t, test.want, string(b))

// unmarshal
err = json.Unmarshal(b, test.out)
assert.NoError(t, err)
if !assert.NoError(t, err) {
return
}
assert.Equal(t, test.in, test.out)
}
}

0 comments on commit 51d14e5

Please sign in to comment.