diff --git a/go.mod b/go.mod index deb61b747c..11c6334ed4 100644 --- a/go.mod +++ b/go.mod @@ -5,4 +5,11 @@ go 1.18 require ( github.com/jinzhu/inflection v1.0.0 github.com/jinzhu/now v1.1.5 + github.com/stretchr/testify v1.9.0 +) + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index bd6104c9b5..1f55377b59 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,14 @@ +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/schema/schema.go b/schema/schema.go index 3e7459ce74..3968372036 100644 --- a/schema/schema.go +++ b/schema/schema.go @@ -77,9 +77,20 @@ func (schema Schema) LookUpField(name string) *Field { if field, ok := schema.FieldsByDBName[name]; ok { return field } + for key, field := range schema.FieldsByDBName { + if strings.EqualFold(key, name) { + return field + } + } if field, ok := schema.FieldsByName[name]; ok { return field } + for key, field := range schema.FieldsByName { + if strings.EqualFold(key, name) { + return field + } + } + return nil } @@ -100,6 +111,11 @@ func (schema Schema) LookUpFieldByBindName(bindNames []string, name string) *Fie if field, ok := schema.FieldsByBindName[find]; ok { return field } + for key, field := range schema.FieldsByBindName { + if strings.EqualFold(key, find) { + return field + } + } } return nil } diff --git a/schema/schema_test.go b/schema/schema_test.go index 45e152e903..44540143a4 100644 --- a/schema/schema_test.go +++ b/schema/schema_test.go @@ -5,6 +5,7 @@ import ( "sync" "testing" + "github.com/stretchr/testify/assert" "gorm.io/gorm" "gorm.io/gorm/schema" "gorm.io/gorm/utils/tests" @@ -334,3 +335,46 @@ func TestCompositePrimaryKeyWithAutoIncrement(t *testing.T) { t.Fatalf("PrioritizedPrimaryField of non autoincrement composite key should be nil") } } + +func TestLookupField(t *testing.T) { + type Product struct { + ProductID uint `gorm:"primaryKey;autoIncrement"` + Code string `gorm:"column:product_code"` + Name string + } + product, err := schema.Parse(&Product{}, &sync.Map{}, schema.NamingStrategy{}) + if err != nil { + t.Fatalf("failed to parse product struct with composite primary key, got error %v", err) + } + field := product.LookUpField("ProductID") + assert.NotNil(t, field) + field = product.LookUpField("productid") + assert.NotNil(t, field) + field = product.LookUpField("product_code") + assert.NotNil(t, field) + field = product.LookUpField("PRODUCT_CODE") + assert.NotNil(t, field) +} + +func TestLookupFieldByBindName(t *testing.T) { + type Product struct { + ID uint `gorm:"primaryKey;autoIncrement"` + } + type Sellable struct { + Name string + Product Product `gorm:"embedded;embeddedPrefix:product_"` + } + + product, err := schema.Parse(&Sellable{}, &sync.Map{}, schema.NamingStrategy{}) + if err != nil { + t.Fatalf("failed to parse Sellable struct with composite primary key, got error %v", err) + } + field := product.LookUpFieldByBindName([]string{"Product", "ID"}, "ID") + assert.NotNil(t, field) + field = product.LookUpFieldByBindName([]string{"Product", "ID"}, "id") + assert.NotNil(t, field) + field = product.LookUpFieldByBindName([]string{"Product", "id"}, "id") + assert.NotNil(t, field) + field = product.LookUpFieldByBindName([]string{"product", "id"}, "id") + assert.NotNil(t, field) +}