Compare commits

...

2 Commits

Author SHA1 Message Date
b37121a218 rename: module 2025-03-18 03:45:42 +07:00
ffbeccc4fd feat: delete constraint for belongs to and has one 2025-03-18 03:29:00 +07:00
13 changed files with 22 additions and 16 deletions

View File

@@ -1,9 +1,9 @@
package main
import (
"github.com/kuzgoga/gormlint/nullSafetyCheck"
"github.com/kuzgoga/gormlint/relationsCheck"
"golang.org/x/tools/go/analysis/multichecker"
"gormlint/nullSafetyCheck"
"gormlint/relationsCheck"
)
func main() {

2
go.mod
View File

@@ -1,4 +1,4 @@
module gormlint
module github.com/kuzgoga/gormlint
go 1.23.2

View File

@@ -1,9 +1,9 @@
package nullSafetyCheck
import (
"github.com/kuzgoga/gormlint/common"
"go/ast"
"golang.org/x/tools/go/analysis"
"gormlint/common"
"strings"
)

View File

@@ -1,7 +1,7 @@
package relationsCheck
import (
"gormlint/common"
"github.com/kuzgoga/gormlint/common"
)
func IsBelongsTo(field common.Field, model common.Model, relatedModel common.Model) bool {

View File

@@ -1,6 +1,6 @@
package relationsCheck
import "gormlint/common"
import "github.com/kuzgoga/gormlint/common"
func IsHasOne(field common.Field, model common.Model, relatedModel common.Model) bool {
foreignKey := field.Tags.GetParamOr("foreignKey", model.Name+"Id")

View File

@@ -1,10 +1,10 @@
package relationsCheck
import (
"github.com/kuzgoga/gormlint/common"
"go/token"
"go/types"
"golang.org/x/tools/go/analysis"
"gormlint/common"
"strings"
)

View File

@@ -2,9 +2,9 @@ package relationsCheck
import (
"fmt"
"github.com/kuzgoga/gormlint/common"
"go/types"
"golang.org/x/tools/go/analysis"
"gormlint/common"
)
func findBackReferenceInOneToMany(model common.Model, relatedModel common.Model) *common.Field {

View File

@@ -1,14 +1,14 @@
package relationsCheck
import (
"github.com/kuzgoga/gormlint/common"
"golang.org/x/tools/go/analysis"
"gormlint/common"
"strings"
)
func CheckCascadeDelete(pass *analysis.Pass, field common.Field) bool {
if !field.Tags.HasParam("constraint") {
pass.Reportf(field.Pos, "field %s should have a constraint", field.Name)
pass.Reportf(field.Pos, "field %s should have a delete constraint", field.Name)
return true
}
constraintValue := field.Tags.GetParam("constraint").Value

View File

@@ -2,8 +2,8 @@ package relationsCheck
import (
"fmt"
"github.com/kuzgoga/gormlint/common"
"golang.org/x/tools/go/analysis"
"gormlint/common"
"slices"
)
@@ -123,8 +123,14 @@ func CheckOneToMany(pass *analysis.Pass, models map[string]common.Model) {
if !foundOneToMany {
if foundBelongsTo {
fmt.Printf("`%s` belongs `%s`\n", *baseType, model.Name)
if CheckCascadeDelete(pass, field) {
return
}
} else if hasOne {
fmt.Printf("`%s` has one `%s` \n", model.Name, relatedModel.Name)
if CheckCascadeDelete(pass, field) {
return
}
} else {
pass.Reportf(field.Pos, "Invalid relation in field `%s`", field.Name)
}

View File

@@ -1,8 +1,8 @@
package tests
import (
"github.com/kuzgoga/gormlint/nullSafetyCheck"
"golang.org/x/tools/go/analysis/analysistest"
"gormlint/nullSafetyCheck"
"testing"
)

View File

@@ -1,8 +1,8 @@
package tests
import (
"github.com/kuzgoga/gormlint/relationsCheck"
"golang.org/x/tools/go/analysis/analysistest"
"gormlint/relationsCheck"
"testing"
)

View File

@@ -65,8 +65,8 @@ type ShoppingCart struct {
// Has one
type Hotel struct {
Id uint `gorm:"primaryKey"`
Office
Id uint `gorm:"primaryKey"`
Office // want "field Office should have a delete constraint"
}
type Office struct {

View File

@@ -45,7 +45,7 @@ type Owner struct {
Id uint `gorm:"primaryKey"`
Name string
CompanyId int
Company Company
Company Company `gorm:"constraint:OnDelete:CASCADE;"`
}
type Company struct {