Speed up getBoilTag
This commit is contained in:
parent
113f8be521
commit
2179570afd
2 changed files with 79 additions and 10 deletions
|
@ -314,22 +314,30 @@ func findField(names []string, titleCases map[string]string, v reflect.Value) (i
|
||||||
|
|
||||||
func getBoilTag(field reflect.StructField, titleCases map[string]string) (name string, recurse bool) {
|
func getBoilTag(field reflect.StructField, titleCases map[string]string) (name string, recurse bool) {
|
||||||
tag := field.Tag.Get("boil")
|
tag := field.Tag.Get("boil")
|
||||||
|
name = field.Name
|
||||||
|
|
||||||
if len(tag) != 0 {
|
if len(tag) == 0 {
|
||||||
tagTokens := strings.Split(tag, ",")
|
return name, false
|
||||||
var ok bool
|
}
|
||||||
name, ok = titleCases[tagTokens[0]]
|
|
||||||
|
var ok bool
|
||||||
|
ind := strings.IndexByte(tag, ',')
|
||||||
|
if ind == -1 {
|
||||||
|
name, ok = titleCases[tag]
|
||||||
if !ok {
|
if !ok {
|
||||||
name = strmangle.TitleCase(tagTokens[0])
|
name = strmangle.TitleCase(tag)
|
||||||
}
|
}
|
||||||
recurse = len(tagTokens) > 1 && tagTokens[1] == "bind"
|
return name, false
|
||||||
|
} else if ind == 0 {
|
||||||
|
return name, true
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(name) == 0 {
|
nameFragment := tag[:ind]
|
||||||
name = field.Name
|
name, ok = titleCases[nameFragment]
|
||||||
|
if !ok {
|
||||||
|
name = strmangle.TitleCase(nameFragment)
|
||||||
}
|
}
|
||||||
|
return name, true
|
||||||
return name, recurse
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetStructValues returns the values (as interface) of the matching columns in obj
|
// GetStructValues returns the values (as interface) of the matching columns in obj
|
||||||
|
|
|
@ -2,6 +2,7 @@ package boil
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql/driver"
|
"database/sql/driver"
|
||||||
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -64,6 +65,66 @@ func TestBind(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGetBoilTag(t *testing.T) {
|
||||||
|
type TestStruct struct {
|
||||||
|
FirstName string `boil:"test_one,boil"`
|
||||||
|
LastName string `boil:"test_two"`
|
||||||
|
MiddleName string `boil:"middle_name,boil"`
|
||||||
|
AwesomeName string `boil:"awesome_name"`
|
||||||
|
Age string `boil:",boil"`
|
||||||
|
Face string `boil:"-"`
|
||||||
|
Nose string
|
||||||
|
}
|
||||||
|
|
||||||
|
var testTitleCases = map[string]string{
|
||||||
|
"test_one": "TestOne",
|
||||||
|
"test_two": "TestTwo",
|
||||||
|
"middle_name": "MiddleName",
|
||||||
|
"awesome_name": "AwesomeName",
|
||||||
|
"age": "Age",
|
||||||
|
"face": "Face",
|
||||||
|
"nose": "Nose",
|
||||||
|
}
|
||||||
|
|
||||||
|
var structFields []reflect.StructField
|
||||||
|
typ := reflect.TypeOf(TestStruct{})
|
||||||
|
removeOk := func(thing reflect.StructField, ok bool) reflect.StructField {
|
||||||
|
if !ok {
|
||||||
|
panic("Exploded")
|
||||||
|
}
|
||||||
|
return thing
|
||||||
|
}
|
||||||
|
structFields = append(structFields, removeOk(typ.FieldByName("FirstName")))
|
||||||
|
structFields = append(structFields, removeOk(typ.FieldByName("LastName")))
|
||||||
|
structFields = append(structFields, removeOk(typ.FieldByName("MiddleName")))
|
||||||
|
structFields = append(structFields, removeOk(typ.FieldByName("AwesomeName")))
|
||||||
|
structFields = append(structFields, removeOk(typ.FieldByName("Age")))
|
||||||
|
structFields = append(structFields, removeOk(typ.FieldByName("Face")))
|
||||||
|
structFields = append(structFields, removeOk(typ.FieldByName("Nose")))
|
||||||
|
|
||||||
|
expect := []struct {
|
||||||
|
Name string
|
||||||
|
Recurse bool
|
||||||
|
}{
|
||||||
|
{"TestOne", true},
|
||||||
|
{"TestTwo", false},
|
||||||
|
{"MiddleName", true},
|
||||||
|
{"AwesomeName", false},
|
||||||
|
{"Age", true},
|
||||||
|
{"-", false},
|
||||||
|
{"Nose", false},
|
||||||
|
}
|
||||||
|
for i, s := range structFields {
|
||||||
|
name, recurse := getBoilTag(s, testTitleCases)
|
||||||
|
if expect[i].Name != name {
|
||||||
|
t.Errorf("Invalid name, expect %q, got %q", expect[i].Name, name)
|
||||||
|
}
|
||||||
|
if expect[i].Recurse != recurse {
|
||||||
|
t.Errorf("Invalid recurse, expect %v, got %v", !recurse, recurse)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestBindSingular(t *testing.T) {
|
func TestBindSingular(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue