Skip to content

Commit

Permalink
allow schema field lookups to be case insensitive
Browse files Browse the repository at this point in the history
  • Loading branch information
joboon committed May 1, 2024
1 parent 9d370bc commit 80f7f95
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
7 changes: 7 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
16 changes: 16 additions & 0 deletions schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}
Expand Down
44 changes: 44 additions & 0 deletions schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"sync"
"testing"

"github.com/stretchr/testify/assert"
"gorm.io/gorm"
"gorm.io/gorm/schema"
"gorm.io/gorm/utils/tests"
Expand Down Expand Up @@ -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)
}

0 comments on commit 80f7f95

Please sign in to comment.