feat: models structs

This commit is contained in:
2024-12-29 23:48:18 +07:00
parent 9a66f2f2a7
commit a7f82bf8f2
7 changed files with 42 additions and 31 deletions

View File

@@ -1,10 +1,14 @@
package main package main
import ( import (
"golang.org/x/tools/go/analysis/singlechecker" "golang.org/x/tools/go/analysis/multichecker"
"gormlint/nullSafetyCheck" "gormlint/nullSafetyCheck"
"gormlint/referencesCheck"
) )
func main() { func main() {
singlechecker.Main(nullSafetyCheck.NullSafetyAnalyzer) multichecker.Main(
nullSafetyCheck.NullSafetyAnalyzer,
referencesCheck.ReferenceAnalyzer,
)
} }

1
common/model.go Normal file
View File

@@ -0,0 +1 @@
package common

View File

@@ -0,0 +1 @@
package common

29
models/testdata.go Normal file
View File

@@ -0,0 +1,29 @@
package null_safety
type Order1 struct {
Id uint `gorm:"primaryKey"`
Description string
// not nullable - not nullable
CustomerId uint `gorm:"not null;foreignKey:CustomerId;"`
}
type Order2 struct {
Id uint `gorm:"primaryKey"`
Description string
// nullable - nullable
CustomerId *uint `gorm:"null;foreignKey:CustomerId;"`
}
type Order3 struct {
Id uint `gorm:"primaryKey"`
// nullable - unspecified
Status *string
Description string
}
type Order4 struct {
Id uint `gorm:"primaryKey"`
// not nullable - unspecified
Status *string
Description string
}

View File

@@ -1,4 +1,4 @@
package nullSafetyCheck package analyzers
import ( import (
"go/ast" "go/ast"
@@ -8,8 +8,8 @@ import (
) )
var NullSafetyAnalyzer = &analysis.Analyzer{ var NullSafetyAnalyzer = &analysis.Analyzer{
Name: "nullSafety", Name: "gormNullSafety",
Doc: "reports inconsistency of nullable values", Doc: "reports problems with nullable fields with unsatisfied tag",
Run: run, Run: run,
} }
@@ -24,6 +24,7 @@ func run(pass *analysis.Pass) (any, error) {
if !ok { if !ok {
return true return true
} }
pass.Fset.Position(structure.Pos())
if err := common.CheckUnnamedModel(*typeSpec); err != nil { if err := common.CheckUnnamedModel(*typeSpec); err != nil {
pass.Reportf(structure.Pos(), err.Error()) pass.Reportf(structure.Pos(), err.Error())

View File

@@ -0,0 +1 @@
package referencesCheck

View File

@@ -1,26 +0,0 @@
package models
type Order struct {
Id uint `gorm:"primaryKey"`
Status string
ProductTypeId uint
ProductType ProductType
ProductAmount uint
Description string
CustomerId uint `gorm:"null;foreignKey:CustomerId;"` // want "Null safety error in \"Order\" model, field \"CustomerId\": column nullable policy doesn't match to tag nullable policy"
Customer Customer
CreatedAt int64 `gorm:"autoCreateTime"`
DeadlineDate int64
}
type ProductType struct {
Id uint `gorm:"primaryKey"`
Name string
}
type Customer struct {
Id uint `gorm:"primaryKey"`
Title string
Contact string
Orders []Order `gorm:"foreignKey:CustomerId"`
}