From a7f82bf8f2b116f8071f6ae42d02f8fbacde00ad Mon Sep 17 00:00:00 2001 From: GogaCoder Date: Sun, 29 Dec 2024 23:48:18 +0700 Subject: [PATCH] feat: models structs --- cmd/gormlint/main.go | 8 ++++++-- common/model.go | 1 + common/normalizeStructTag.go | 1 + models/testdata.go | 29 +++++++++++++++++++++++++++++ nullSafetyCheck/nullSafetyCheck.go | 7 ++++--- referencesCheck/referencesCheck.go | 1 + tests/models/testdata.go | 26 -------------------------- 7 files changed, 42 insertions(+), 31 deletions(-) create mode 100644 common/model.go create mode 100644 common/normalizeStructTag.go create mode 100644 models/testdata.go create mode 100644 referencesCheck/referencesCheck.go delete mode 100644 tests/models/testdata.go diff --git a/cmd/gormlint/main.go b/cmd/gormlint/main.go index ec6dfcc..31b2402 100644 --- a/cmd/gormlint/main.go +++ b/cmd/gormlint/main.go @@ -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, + ) } diff --git a/common/model.go b/common/model.go new file mode 100644 index 0000000..805d0c7 --- /dev/null +++ b/common/model.go @@ -0,0 +1 @@ +package common diff --git a/common/normalizeStructTag.go b/common/normalizeStructTag.go new file mode 100644 index 0000000..805d0c7 --- /dev/null +++ b/common/normalizeStructTag.go @@ -0,0 +1 @@ +package common diff --git a/models/testdata.go b/models/testdata.go new file mode 100644 index 0000000..330865a --- /dev/null +++ b/models/testdata.go @@ -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 +} diff --git a/nullSafetyCheck/nullSafetyCheck.go b/nullSafetyCheck/nullSafetyCheck.go index 163767b..0960784 100644 --- a/nullSafetyCheck/nullSafetyCheck.go +++ b/nullSafetyCheck/nullSafetyCheck.go @@ -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()) diff --git a/referencesCheck/referencesCheck.go b/referencesCheck/referencesCheck.go new file mode 100644 index 0000000..66fa8e8 --- /dev/null +++ b/referencesCheck/referencesCheck.go @@ -0,0 +1 @@ +package referencesCheck diff --git a/tests/models/testdata.go b/tests/models/testdata.go deleted file mode 100644 index 06f95e2..0000000 --- a/tests/models/testdata.go +++ /dev/null @@ -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"` -}