Skip to content

Commit

Permalink
Fix "has_one" pointer association (#378)
Browse files Browse the repository at this point in the history
* hasOneAssociation did not work with pointers
  • Loading branch information
ypjama authored and stanislas-m committed Apr 29, 2019
1 parent 72243ad commit 6ee624e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
3 changes: 3 additions & 0 deletions associations/has_one_association.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ func hasOneAssociationBuilder(p associationParams) (Association, error) {
}

func (h *hasOneAssociation) Kind() reflect.Kind {
if h.ownedType.Kind() == reflect.Ptr {
return h.ownedType.Elem().Kind()
}
return h.ownedType.Kind()
}

Expand Down
18 changes: 18 additions & 0 deletions associations/has_one_association_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ type FooHasOne struct {
type barHasOne struct {
Title string `db:"title"`
FooHasOneID nulls.UUID `db:"foo_has_one_id"`
BazHasOneID nulls.UUID `db:"baz_id"`
}

type bazHasOne struct {
ID uuid.UUID `db:"id"`
BarHasOne *barHasOne `has_one:"barHasOne" fk_id:"baz_id"`
}

func Test_Has_One_Association(t *testing.T) {
Expand All @@ -44,6 +50,18 @@ func Test_Has_One_Association(t *testing.T) {
for index := range after {
a.Equal(nil, after[index].AfterInterface())
}

baz := bazHasOne{ID: id}

as, err = associations.ForStruct(&baz)

a.NoError(err)
a.Equal(len(as), 1)
a.Equal(reflect.Struct, as[0].Kind())

where, args = as[0].Constraint()
a.Equal("baz_id = ?", where)
a.Equal(id, args[0].(uuid.UUID))
}

func Test_Has_One_SetValue(t *testing.T) {
Expand Down

0 comments on commit 6ee624e

Please sign in to comment.