Skip to content

Commit

Permalink
Fixes beforeSave pointer panics (#322)
Browse files Browse the repository at this point in the history
* Fixes beforeSave pointer panics

* Renames test variable
  • Loading branch information
stangah authored and stanislas-m committed Nov 30, 2018
1 parent ca53d96 commit 29bb60f
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 4 deletions.
10 changes: 6 additions & 4 deletions associations/belongs_to_association.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (b *belongsToAssociation) BeforeInterface() interface{} {
}

m := b.ownerModel
if m.Kind() == reflect.Ptr {
if m.Kind() == reflect.Ptr && !m.IsNil() {
m = b.ownerModel.Elem()
}

Expand All @@ -111,12 +111,14 @@ func (b *belongsToAssociation) BeforeInterface() interface{} {
}

func (b *belongsToAssociation) BeforeSetup() error {
ownerID := reflect.Indirect(reflect.ValueOf(b.ownerModel.Interface())).FieldByName("ID").Interface()
ownerID := reflect.Indirect(reflect.ValueOf(b.ownerModel.Interface())).FieldByName("ID")
if b.ownerID.CanSet() {
if n := nulls.New(b.ownerID.Interface()); n != nil {
b.ownerID.Set(reflect.ValueOf(n.Parse(ownerID)))
b.ownerID.Set(reflect.ValueOf(n.Parse(ownerID.Interface())))
} else if b.ownerID.Kind() == reflect.Ptr {
b.ownerID.Set(ownerID.Addr())
} else {
b.ownerID.Set(reflect.ValueOf(ownerID))
b.ownerID.Set(ownerID)
}
return nil
}
Expand Down
33 changes: 33 additions & 0 deletions executors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,39 @@ func Test_Eager_Create_Belongs_To_Pointers(t *testing.T) {

ctx, _ = tx.Count(&Head{})
r.Equal(1, ctx)

err = tx.Eager().Create(&Head{
BodyID: body.ID,
Body: nil,
})
r.NoError(err)
})
}

func Test_Create_Belongs_To_Pointers(t *testing.T) {
transaction(func(tx *Connection) {
r := require.New(t)
// Create a body without a head:
body := Body{
Head: nil,
}

err := tx.Create(&body)
r.NoError(err)
r.NotZero(body.ID)
r.Nil(body.Head)

// Create a head with the associated model set but not the ID
created := HeadPtr{
Body: &body,
}
err = tx.Create(&created)
r.NoError(err)

found := HeadPtr{}
err = tx.Find(&found, created.ID)
r.NoError(err)
r.Equal(body.ID, *found.BodyID)
})
}

Expand Down
1 change: 1 addition & 0 deletions migrations/20181130023854_head_ptr.down.fizz
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
drop_table("head_ptrs")
6 changes: 6 additions & 0 deletions migrations/20181130023854_head_ptr.up.fizz
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
create_table("head_ptrs") {
t.Column("id", "integer", {"primary": true})
t.Column("body_id", "integer", {"null": true})
t.ForeignKey("body_id", {"bodies": ["id"]}, {})
t.DisableTimestamps()
}
6 changes: 6 additions & 0 deletions pop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,3 +354,9 @@ type Head struct {
BodyID int `json:"-" db:"body_id"`
Body *Body `json:"body,omitempty" belongs_to:"body"`
}

type HeadPtr struct {
ID int `json:"id,omitempty" db:"id"`
BodyID *int `json:"-" db:"body_id"`
Body *Body `json:"body,omitempty" belongs_to:"body"`
}

0 comments on commit 29bb60f

Please sign in to comment.