fix: logic
This commit is contained in:
29
.idea/watcherTasks.xml
generated
Normal file
29
.idea/watcherTasks.xml
generated
Normal file
@@ -0,0 +1,29 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectTasksOptions">
|
||||
<TaskOptions isEnabled="true">
|
||||
<option name="arguments" value="run --disable=typecheck $FileDir$" />
|
||||
<option name="checkSyntaxErrors" value="true" />
|
||||
<option name="description" />
|
||||
<option name="exitCodeBehavior" value="ERROR" />
|
||||
<option name="fileExtension" value="go" />
|
||||
<option name="immediateSync" value="false" />
|
||||
<option name="name" value="golangci-lint" />
|
||||
<option name="output" value="" />
|
||||
<option name="outputFilters">
|
||||
<array />
|
||||
</option>
|
||||
<option name="outputFromStdout" value="false" />
|
||||
<option name="program" value="golangci-lint" />
|
||||
<option name="runOnExternalChanges" value="false" />
|
||||
<option name="scopeName" value="Project Files" />
|
||||
<option name="trackOnlyRoot" value="true" />
|
||||
<option name="workingDir" value="$ProjectFileDir$" />
|
||||
<envs>
|
||||
<env name="GOROOT" value="$GOROOT$" />
|
||||
<env name="GOPATH" value="$GOPATH$" />
|
||||
<env name="PATH" value="$GoBinDirs$" />
|
||||
</envs>
|
||||
</TaskOptions>
|
||||
</component>
|
||||
</project>
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
14
tests/testdata/src/relations_check/negative.go
vendored
14
tests/testdata/src/relations_check/negative.go
vendored
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user