Files
gormlint/referencesCheck/referencesCheck.go
GogaCoder 49878c333c feat: references check
... and fixes: parse fields without tags and other logical errors
2024-12-30 21:15:15 +07:00

60 lines
1.5 KiB
Go

package referencesCheck
import (
"fmt"
"golang.org/x/tools/go/analysis"
"gormlint/common"
"strings"
)
// ReferenceAnalyzer todo: add URL for rule
var ReferenceAnalyzer = &analysis.Analyzer{
Name: "gormReferencesCheck",
Doc: "report about invalid references in models",
Run: run,
}
var models map[string]common.Model
func run(pass *analysis.Pass) (any, error) {
models = make(map[string]common.Model)
common.ParseModels(pass, &models)
for _, model := range models {
for _, field := range model.Fields {
for _, param := range field.Params {
pair := strings.Split(param, ":")
if len(pair) < 2 {
fmt.Printf("%s", param)
}
paramName := pair[0]
paramValue := pair[1]
if paramName == "reference" {
pass.Reportf(field.Pos, "Typo in tag: \"reference\" instead of verb \"references\"")
}
if paramName == "references" {
fieldType := common.ResolveBaseType(field.Type)
if fieldType == nil {
pass.Reportf(field.Pos, "Failed to process references check. Cannot resolve type \"%s\" in field \"%s\"", field.Type, field.Name)
return nil, nil
}
relatedModel, modelExists := models[*fieldType]
if modelExists {
_, fieldExists := relatedModel.Fields[paramValue]
if !fieldExists {
pass.Reportf(field.Pos, "Related field \"%s\" doesn't exist on model \"%s\"", paramValue, relatedModel.Name)
}
} else {
pass.Reportf(field.Pos, "Related model \"%s\" doesn't exist", *fieldType)
}
}
}
}
}
return nil, nil
}