Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions reflectx/reflectx.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,10 @@ func FieldByIndexes(v reflect.Value, indexes []int) reflect.Value {
v = reflect.Indirect(v).Field(i)
// if this is a pointer, it's possible it is nil
if v.Kind() == reflect.Ptr && v.IsNil() {
if !v.CanSet() {
continue
}

alloc := reflect.New(Deref(v.Type()))
v.Set(alloc)
}
Expand Down
4 changes: 3 additions & 1 deletion sqlx-runner/fixtures_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Person struct {
Key dat.NullString `db:"key"`
Name string `db:"name"`
CreatedAt dat.NullTime `db:"created_at"`
Nullable *string `db:"nullable"`

Posts []*Post `json:"posts"`
}
Expand Down Expand Up @@ -79,7 +80,8 @@ const createTables = `
image bytea,
key text,
name text NOT NULL,
created_at timestamptz default now()
created_at timestamptz default now(),
nullable text
);
CREATE TABLE posts (
id SERIAL PRIMARY KEY,
Expand Down
11 changes: 11 additions & 0 deletions sqlx-runner/insert_exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,17 @@ func TestInsertReal(t *testing.T) {
assert.NoError(t, err)
assert.True(t, person2.ID > 0)
assert.NotEqual(t, person.ID, person2.ID)

person3 := Person{Name: "Barack", Nullable: nil}
err = s.
InsertInto("people").
Columns("name", "nullable").
Record(person3).
Returning("id", "nullable").
QueryStruct(&person3)
assert.True(t, person3.ID > 0)
assert.NotEqual(t, person2.ID, person3.ID)
assert.Nil(t, person3.Nullable)
}

func TestInsertMultipleRecords(t *testing.T) {
Expand Down
5 changes: 3 additions & 2 deletions sqlx-runner/select_exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func TestSelectQueryStruct(t *testing.T) {
// Found:
var person Person
err := s.
Select("id", "name", "email").
Select("id", "name", "email", "nullable").
From("people").
Where("email = $1", "john@acme.com").
QueryStruct(&person)
Expand All @@ -95,11 +95,12 @@ func TestSelectQueryStruct(t *testing.T) {
assert.Equal(t, person.Name, "John")
assert.True(t, person.Email.Valid)
assert.Equal(t, person.Email.String, "john@acme.com")
assert.Nil(t, person.Nullable)

// Not found:
var person2 Person
err = s.
Select("id", "name", "email").
Select("id", "name", "email", "nullable").
From("people").Where("email = $1", "dontexist@acme.com").
QueryStruct(&person2)
assert.Contains(t, err.Error(), "no rows")
Expand Down
51 changes: 50 additions & 1 deletion sqlx-runner/update_exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package runner
import (
"testing"

"gopkg.in/mgutz/dat.v1"
dat "gopkg.in/mgutz/dat.v2"
"gopkg.in/stretchr/testify.v1/assert"
)

Expand Down Expand Up @@ -61,6 +61,51 @@ func TestUpdateReal(t *testing.T) {
assert.Equal(t, person.Email.String, "barack@whitehouse.gov")
}

func TestUpdateRealNullable(t *testing.T) {
s := beginTxWithFixtures()
defer s.AutoRollback()

person := Person{Name: "Barack", Nullable: nil}
err := s.
InsertInto("people").
Columns("name", "nullable").
Record(person).
Returning("*").
QueryStruct(&person)
person.Nullable = strToPtr("obama2@whitehouse.gov")

_, err = s.
Update("people").
Set("nullable", person.Nullable).
Where("id = $1", person.ID).
Exec()
assert.NoError(t, err)

err = s.
Select("*").
From("people").
Where("id = $1", person.ID).
QueryStruct(&person)
assert.NoError(t, err)
assert.NotNil(t, person.Nullable)
assert.Equal(t, *person.Nullable, "obama2@whitehouse.gov")

_, err = s.
Update("people").
Set("nullable", nil).
Where("id = $1", person.ID).
Exec()
assert.NoError(t, err)

err = s.
Select("*").
From("people").
Where("id = $1", person.ID).
QueryStruct(&person)
assert.NoError(t, err)
assert.Nil(t, person.Nullable)
}

func TestUpdateReturningStar(t *testing.T) {
s := beginTxWithFixtures()
defer s.AutoRollback()
Expand Down Expand Up @@ -232,3 +277,7 @@ func TestUpdateScopeFunc(t *testing.T) {
assert.Equal(t, person.Email.Valid, true)
assert.Equal(t, person.Email.String, "barack@whitehouse.gov")
}

func strToPtr(s string) *string {
return &s
}