fix: logic
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user