From 6e5e676e84dfa95615a8460d936b5ec02d5ed407 Mon Sep 17 00:00:00 2001 From: gogacoder Date: Tue, 18 Mar 2025 01:36:42 +0700 Subject: [PATCH] fix: logic --- .idea/watcherTasks.xml | 29 +++++++++++++++++++ relationsCheck/checkBelongsTo.go | 8 ++--- relationsCheck/checkHasOne.go | 17 +++++++++++ relationsCheck/relationsCheck.go | 6 +++- .../testdata/src/relations_check/negative.go | 14 +++++++++ 5 files changed, 69 insertions(+), 5 deletions(-) create mode 100644 .idea/watcherTasks.xml diff --git a/.idea/watcherTasks.xml b/.idea/watcherTasks.xml new file mode 100644 index 0000000..8be2300 --- /dev/null +++ b/.idea/watcherTasks.xml @@ -0,0 +1,29 @@ + + + + + + + + \ No newline at end of file diff --git a/relationsCheck/checkBelongsTo.go b/relationsCheck/checkBelongsTo.go index a67389d..1be3eba 100644 --- a/relationsCheck/checkBelongsTo.go +++ b/relationsCheck/checkBelongsTo.go @@ -5,13 +5,13 @@ import ( ) func IsBelongsTo(field common.Field, model common.Model, relatedModel common.Model) bool { - foreignKey := field.Tags.GetParamOr("foreignKey", "Id") - references := field.Tags.GetParamOr("references", relatedModel.Name+"Id") + references := field.Tags.GetParamOr("references", "Id") + foreignKey := field.Tags.GetParamOr("foreignKey", field.Name+"Id") - if !model.HasField(references) { + if !model.HasField(foreignKey) { return false } - if !relatedModel.HasField(foreignKey) { + if !relatedModel.HasField(references) { return false } diff --git a/relationsCheck/checkHasOne.go b/relationsCheck/checkHasOne.go index 90750c8..7ab72c9 100644 --- a/relationsCheck/checkHasOne.go +++ b/relationsCheck/checkHasOne.go @@ -1 +1,18 @@ package relationsCheck + +import "gormlint/common" + +func IsHasOne(field common.Field, model common.Model, relatedModel common.Model) bool { + foreignKey := field.Tags.GetParamOr("foreignKey", model.Name+"Id") + references := field.Tags.GetParamOr("references", "Id") + + if !relatedModel.HasField(foreignKey) { + return false + } + + if !model.HasField(references) { + return false + } + + return true +} diff --git a/relationsCheck/relationsCheck.go b/relationsCheck/relationsCheck.go index 9f4d374..756dcb8 100644 --- a/relationsCheck/relationsCheck.go +++ b/relationsCheck/relationsCheck.go @@ -117,10 +117,14 @@ func CheckOneToMany(pass *analysis.Pass, models map[string]common.Model) { fmt.Printf("Found 1:M relation in model `%s` with model `%s`\n", model.Name, *baseType) } + foundBelongsTo := IsBelongsTo(field, model, *relatedModel) + hasOne := IsHasOne(field, model, *relatedModel) + if !foundOneToMany { - foundBelongsTo := IsBelongsTo(field, model, *relatedModel) if foundBelongsTo { fmt.Printf("Found belongs to relation in model `%s` with model `%s`\n", model.Name, *baseType) + } else if hasOne { + fmt.Printf("`%s` has one `%s` \n", model.Name, relatedModel.Name) } else { pass.Reportf(field.Pos, "Invalid relation in field `%s`", field.Name) } diff --git a/tests/testdata/src/relations_check/negative.go b/tests/testdata/src/relations_check/negative.go index d532061..47852a0 100644 --- a/tests/testdata/src/relations_check/negative.go +++ b/tests/testdata/src/relations_check/negative.go @@ -61,3 +61,17 @@ type ShoppingCart struct { Id uint `gorm:"primaryKey"` SerializedItems string } + +// Has one + +type Hotel struct { + Id uint `gorm:"primaryKey"` + Office +} + +type Office struct { + Id uint `gorm:"primaryKey"` + Name string + Address string + HotelId uint +}