feat: many2many relations check

This commit is contained in:
2025-01-01 17:11:34 +07:00
parent caee1b8eff
commit a6574924fe
10 changed files with 303 additions and 0 deletions

View File

@@ -0,0 +1,12 @@
package tests
import (
"golang.org/x/tools/go/analysis/analysistest"
"gormlint/relationsCheck"
"testing"
)
func TestRelationsCheck(t *testing.T) {
t.Parallel()
analysistest.Run(t, analysistest.TestData(), relationsCheck.RelationsAnalyzer, "relations_check")
}

View File

@@ -1,5 +1,7 @@
package references_check
// TODO: add test with annotations on back-references
type WorkArea struct {
Id uint `gorm:"primaryKey"`
Workshop Workshop `gorm:"foreignKey:WorkshopId;references:Id;"`

View File

@@ -1,5 +1,7 @@
package references_check
// TODO: add test with annotations on back-references
type User struct {
Name string
CompanyID string

View File

@@ -0,0 +1,16 @@
package relations_check
type Library struct {
Id uint `gorm:"primaryKey"`
Books []*Book `gorm:"many2many:library_book;"`
}
type Book struct {
Id uint `gorm:"primaryKey"`
Libraries []*Library `gorm:"many2many:library_book;"`
}
type Employee struct {
Id uint `gorm:"primaryKey"`
Subordinates []*Employee `gorm:"many2many:employee_subordinates;"` // self-reference
}

View File

@@ -0,0 +1,21 @@
package relations_check
type Student struct {
Id uint `gorm:"primaryKey"`
Courses []Course `gorm:"many2many:student_courses;"`
}
type Course struct {
Id uint `gorm:"primaryKey"`
Students []Course `gorm:"many2many:student_courses;"` // want "Invalid type `Course` in M2M relation \\(use \\[\\]\\*Student or self-reference\\)"
}
type Author struct {
Id uint `gorm:"primaryKey"`
Articles []Article `gorm:"many2many:author_articles;"`
}
type Article struct {
Id uint `gorm:"primaryKey"`
Authors Author `gorm:"many2many:author_articles;"` // want "M2M relation `author_articles` with bad type `Author` \\(should be a slice\\)"
}