feat: models structs
This commit is contained in:
@@ -1,10 +1,14 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"golang.org/x/tools/go/analysis/singlechecker"
|
||||
"golang.org/x/tools/go/analysis/multichecker"
|
||||
"gormlint/nullSafetyCheck"
|
||||
"gormlint/referencesCheck"
|
||||
)
|
||||
|
||||
func main() {
|
||||
singlechecker.Main(nullSafetyCheck.NullSafetyAnalyzer)
|
||||
multichecker.Main(
|
||||
nullSafetyCheck.NullSafetyAnalyzer,
|
||||
referencesCheck.ReferenceAnalyzer,
|
||||
)
|
||||
}
|
||||
|
||||
1
common/model.go
Normal file
1
common/model.go
Normal file
@@ -0,0 +1 @@
|
||||
package common
|
||||
1
common/normalizeStructTag.go
Normal file
1
common/normalizeStructTag.go
Normal file
@@ -0,0 +1 @@
|
||||
package common
|
||||
29
models/testdata.go
Normal file
29
models/testdata.go
Normal 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
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package nullSafetyCheck
|
||||
package analyzers
|
||||
|
||||
import (
|
||||
"go/ast"
|
||||
@@ -8,8 +8,8 @@ import (
|
||||
)
|
||||
|
||||
var NullSafetyAnalyzer = &analysis.Analyzer{
|
||||
Name: "nullSafety",
|
||||
Doc: "reports inconsistency of nullable values",
|
||||
Name: "gormNullSafety",
|
||||
Doc: "reports problems with nullable fields with unsatisfied tag",
|
||||
Run: run,
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ func run(pass *analysis.Pass) (any, error) {
|
||||
if !ok {
|
||||
return true
|
||||
}
|
||||
pass.Fset.Position(structure.Pos())
|
||||
|
||||
if err := common.CheckUnnamedModel(*typeSpec); err != nil {
|
||||
pass.Reportf(structure.Pos(), err.Error())
|
||||
|
||||
1
referencesCheck/referencesCheck.go
Normal file
1
referencesCheck/referencesCheck.go
Normal file
@@ -0,0 +1 @@
|
||||
package referencesCheck
|
||||
@@ -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"`
|
||||
}
|
||||
Reference in New Issue
Block a user