Compare commits

..

2 Commits

Author SHA1 Message Date
4536be4d10 feat: more clear output 2025-03-18 03:23:15 +07:00
6e5e676e84 fix: logic 2025-03-18 01:36:42 +07:00
7 changed files with 70 additions and 8 deletions

29
.idea/watcherTasks.xml generated Normal file
View 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>

View File

@@ -5,13 +5,13 @@ import (
) )
func IsBelongsTo(field common.Field, model common.Model, relatedModel common.Model) bool { func IsBelongsTo(field common.Field, model common.Model, relatedModel common.Model) bool {
foreignKey := field.Tags.GetParamOr("foreignKey", "Id") references := field.Tags.GetParamOr("references", "Id")
references := field.Tags.GetParamOr("references", relatedModel.Name+"Id") foreignKey := field.Tags.GetParamOr("foreignKey", field.Name+"Id")
if !model.HasField(references) { if !model.HasField(foreignKey) {
return false return false
} }
if !relatedModel.HasField(foreignKey) { if !relatedModel.HasField(references) {
return false return false
} }

View File

@@ -1 +1,18 @@
package relationsCheck 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
}

View File

@@ -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) fmt.Printf("Found 1:M relation in model `%s` with model `%s`\n", model.Name, *baseType)
} }
if !foundOneToMany {
foundBelongsTo := IsBelongsTo(field, model, *relatedModel) foundBelongsTo := IsBelongsTo(field, model, *relatedModel)
hasOne := IsHasOne(field, model, *relatedModel)
if !foundOneToMany {
if foundBelongsTo { if foundBelongsTo {
fmt.Printf("Found belongs to relation in model `%s` with model `%s`\n", model.Name, *baseType) fmt.Printf("`%s` belongs `%s`\n", *baseType, model.Name)
} else if hasOne {
fmt.Printf("`%s` has one `%s` \n", model.Name, relatedModel.Name)
} else { } else {
pass.Reportf(field.Pos, "Invalid relation in field `%s`", field.Name) pass.Reportf(field.Pos, "Invalid relation in field `%s`", field.Name)
} }

View File

@@ -7,6 +7,5 @@ import (
) )
func TestNullSafety(t *testing.T) { func TestNullSafety(t *testing.T) {
t.Parallel()
analysistest.Run(t, analysistest.TestData(), nullSafetyCheck.NullSafetyAnalyzer, "null_safety") analysistest.Run(t, analysistest.TestData(), nullSafetyCheck.NullSafetyAnalyzer, "null_safety")
} }

View File

@@ -7,6 +7,5 @@ import (
) )
func TestRelationsCheck(t *testing.T) { func TestRelationsCheck(t *testing.T) {
t.Parallel()
analysistest.Run(t, analysistest.TestData(), relationsCheck.RelationsAnalyzer, "relations_check") analysistest.Run(t, analysistest.TestData(), relationsCheck.RelationsAnalyzer, "relations_check")
} }

View File

@@ -61,3 +61,17 @@ type ShoppingCart struct {
Id uint `gorm:"primaryKey"` Id uint `gorm:"primaryKey"`
SerializedItems string 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
}