Skip to content

Commit

Permalink
Fix/case when fk is pointer (#38)
Browse files Browse the repository at this point in the history
* fix: avoid error when fk is pointer

* test: handle new test case

* refactor: remove unnecessary condition

---------

Co-authored-by: Eyo Chen <[email protected]>
  • Loading branch information
eyo-chen and Eyo Chen authored Sep 21, 2024
1 parent a84f599 commit 819105d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
37 changes: 19 additions & 18 deletions gofacto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,10 @@ type testStructWithID2 struct {

// testAssocStruct is a struct with a foreign key to test the association functionality.
type testAssocStruct struct {
ID int
ForeignKey int `gofacto:"foreignKey,struct:testStructWithID,field:ForeignValue"`
ForeignKey2 int `gofacto:"foreignKey,struct:testStructWithID2,field:ForeignValue2,table:test_struct_with_id2s"`
ID int
ForeignKey int `gofacto:"foreignKey,struct:testStructWithID,field:ForeignValue"`
ForeignKey2 *int `gofacto:"foreignKey,struct:testStructWithID2,field:ForeignValue2,table:test_struct_with_id2s"`

ForeignValue testStructWithID
ForeignValue2 *testStructWithID2
}
Expand Down Expand Up @@ -2563,16 +2564,16 @@ func setZero_OnBuilderListMany(t *testing.T) {

func TestWithOne(t *testing.T) {
for _, fn := range map[string]func(*testing.T){
"when withOne on builder, insert successfully": withOne_OnBuilder,
"when withOne on builder not pass ptr, return error": withOne_OnBuilderNotPassPtr,
"when withOne on builder not pass struct, return error": withOne_OnBuilderNotPassStruct,
"when withOne on builder not pass struct in tag, return error": withOne_OnBuilderNotPassStructInTag,
"when withOne on builder with err, return error": withOne_OnBuilderWithErr,
"when withOne on builder list, insert successfully": withOne_OnBuilderList,
"when withOne on builder list not pass ptr, return error": withOne_OnBuilderListNotPassPtr,
"when withOne on builder list not pass struct, return error": withOne_OnBuilderListNotPassStruct,
"when withOne on builder list not pass struct in tag, return error": withOne_OnBuilderListNotPassStructInTag,
"when withOne on builder list with err, return error": withOne_OnBuilderListWithErr,
"when on builder, insert successfully": withOne_OnBuilder,
"when on builder not pass ptr, return error": withOne_OnBuilderNotPassPtr,
"when on builder not pass struct, return error": withOne_OnBuilderNotPassStruct,
"when on builder not pass struct in tag, return error": withOne_OnBuilderNotPassStructInTag,
"when on builder with err, return error": withOne_OnBuilderWithErr,
"when on builder list, insert successfully": withOne_OnBuilderList,
"when on builder list not pass ptr, return error": withOne_OnBuilderListNotPassPtr,
"when on builder list not pass struct, return error": withOne_OnBuilderListNotPassStruct,
"when on builder list not pass struct in tag, return error": withOne_OnBuilderListNotPassStructInTag,
"when on builder list with err, return error": withOne_OnBuilderListWithErr,
} {
t.Run(testutils.GetFunName(fn), func(t *testing.T) {
fn(t)
Expand All @@ -2594,7 +2595,7 @@ func withOne_OnBuilder(t *testing.T) {
t.Fatalf("ForeignKey should be %v", assVal.ID)
}

if val.ForeignKey2 != assVal.ID {
if val.ForeignKey2 != nil && *val.ForeignKey2 != assVal.ID {
t.Fatalf("ForeignKey2 should be %v", assVal.ID)
}

Expand Down Expand Up @@ -2699,15 +2700,15 @@ func withOne_OnBuilderList(t *testing.T) {
t.Fatal(err.Error())
}

if vals[0].ForeignKey2 != assVal2.ID {
if vals[0].ForeignKey2 != nil && *vals[0].ForeignKey2 != assVal2.ID {
t.Fatalf("ForeignKey2 should be %v", assVal2.ID)
}

if err := testutils.CompareVal(vals[0].ForeignValue2, &assVal2); err != nil {
t.Fatal(err.Error())
}

if vals[1].ForeignKey2 != assVal2.ID {
if vals[1].ForeignKey2 != nil && *vals[1].ForeignKey2 != assVal2.ID {
t.Fatalf("ForeignKey2 should be %v", assVal2.ID)
}

Expand Down Expand Up @@ -2824,15 +2825,15 @@ func withMany_CorrectCase(t *testing.T) {
t.Fatal(err.Error())
}

if vals[0].ForeignKey2 != assVal3.ID {
if vals[0].ForeignKey2 != nil && *vals[0].ForeignKey2 != assVal3.ID {
t.Fatalf("ForeignKey2 should be %v", assVal3.ID)
}

if err := testutils.CompareVal(vals[0].ForeignValue2, &assVal3); err != nil {
t.Fatal(err.Error())
}

if vals[1].ForeignKey2 != assVal4.ID {
if vals[1].ForeignKey2 != nil && *vals[1].ForeignKey2 != assVal4.ID {
t.Fatalf("ForeignKey2 should be %v", assVal4.ID)
}

Expand Down
4 changes: 4 additions & 0 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,10 @@ func extractTag(dataType reflect.Type) (map[string]tagInfo, []string, error) {
// and it also handles the conversion between int and uint.
// Normally, it's used to set the ID field of the target struct
func setIntValue(target, source reflect.Value) {
if target.Kind() == reflect.Ptr {
target = target.Elem()
}

targetKind := target.Kind()
sourceKind := source.Kind()

Expand Down

0 comments on commit 819105d

Please sign in to comment.