Skip to content

Commit

Permalink
Merge pull request #331 from upper/hotfix/dont-allow-nil-values-on-em…
Browse files Browse the repository at this point in the history
…pty-arrays

Don't allow nil values on empty arrays.
  • Loading branch information
José Carlos authored Feb 9, 2017
2 parents c619b7b + 65fdb71 commit 0c07ed7
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 10 deletions.
6 changes: 2 additions & 4 deletions lib/sqlbuilder/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ type stringArray []string

func (a *stringArray) Scan(src interface{}) error {
if src == nil {
*a = nil
*a = stringArray{}
return nil
}

Expand All @@ -98,7 +98,6 @@ func (a *stringArray) Scan(src interface{}) error {
return errors.New("Scan source was not []bytes")
}
if len(b) == 0 {
*a = nil
return nil
}

Expand Down Expand Up @@ -222,15 +221,14 @@ type int64Array []int64

func (a *int64Array) Scan(src interface{}) error {
if src == nil {
*a = nil
*a = int64Array{}
return nil
}
b, ok := src.([]byte)
if !ok {
return errors.New("Scan source was not []bytes")
}
if len(b) == 0 {
*a = nil
return nil
}

Expand Down
25 changes: 23 additions & 2 deletions postgresql/adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,18 @@ func TestPgTypes(t *testing.T) {

expected := pgTypeTests[i]
expected.ID = id.(int64)
assert.Equal(t, expected, actual)

// db.v2: db.v2 forces empty arrays instead of nil values.
assert.Equal(t, expected.ID, actual.ID)
assert.Equal(t, expected.Field1, actual.Field1)
assert.Equal(t, expected.Field2, actual.Field2)
assert.Equal(t, expected.Field3, actual.Field3)
assert.Equal(t, expected.StringValue, actual.StringValue)
assert.Equal(t, len(expected.IntegerArray), len(actual.IntegerArray))
assert.Equal(t, len(expected.StringArray), len(actual.StringArray))

// db.v3: This will be the expected behaviour on db.v3.
// assert.Equal(t, expected, actual)
}

for i := range pgTypeTests {
Expand All @@ -488,7 +499,17 @@ func TestPgTypes(t *testing.T) {
expected := pgTypeTests[i]
expected.ID = id

assert.Equal(t, expected, actual)
// db.v2: db.v2 forces empty arrays instead of nil values.
assert.Equal(t, expected.ID, actual.ID)
assert.Equal(t, expected.Field1, actual.Field1)
assert.Equal(t, expected.Field2, actual.Field2)
assert.Equal(t, expected.Field3, actual.Field3)
assert.Equal(t, expected.StringValue, actual.StringValue)
assert.Equal(t, len(expected.IntegerArray), len(actual.IntegerArray))
assert.Equal(t, len(expected.StringArray), len(actual.StringArray))

// db.v3: This will be the expected behaviour on db.v3.
// assert.Equal(t, expected, actual)
}

inserter := sess.InsertInto("pg_types")
Expand Down
22 changes: 18 additions & 4 deletions postgresql/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,26 @@ func TestStringAndInt64Array(t *testing.T) {
tt := []arrayType{
// Test nil arrays.
arrayType{
ID: 1,
ID: 1,
},

// Test nil arrays.
arrayType{
ID: 2,
Integers: nil,
Strings: nil,
},

// Test empty arrays.
arrayType{
ID: 2,
ID: 3,
Integers: []int64{},
Strings: []string{},
},

// Test non-empty arrays.
arrayType{
ID: 3,
ID: 4,
Integers: []int64{1, 2, 3},
Strings: []string{"1", "2", "3"},
},
Expand All @@ -70,10 +75,19 @@ func TestStringAndInt64Array(t *testing.T) {
var itemCheck arrayType
err = arrayTypes.Find(db.Cond{"id": id}).One(&itemCheck)
assert.NoError(t, err)

assert.Len(t, itemCheck.Integers, len(item.Integers))
assert.Len(t, itemCheck.Strings, len(item.Strings))

assert.Equal(t, item, itemCheck)
// db.v2: Check nil/zero values just to make sure that the arrays won't be
// JSON-marshalled into `null` instead of empty array `[]`.
assert.NotNil(t, itemCheck.Integers)
assert.NotNil(t, itemCheck.Strings)
assert.NotZero(t, itemCheck.Integers)
assert.NotZero(t, itemCheck.Strings)

// db.v3: This will be the expected behaviour on db.v3.
//assert.Equal(t, item, itemCheck)
}
}

Expand Down

0 comments on commit 0c07ed7

Please sign in to comment.