Remove a bunch of unused code.
- Optimize the TitleCaseIdentifier loop
This commit is contained in:
parent
9f52cf6ec9
commit
2d0924a625
4 changed files with 89 additions and 88 deletions
|
@ -4,7 +4,6 @@ import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
@ -19,6 +18,7 @@ var (
|
||||||
const (
|
const (
|
||||||
loadMethodPrefix = "Load"
|
loadMethodPrefix = "Load"
|
||||||
relationshipStructName = "R"
|
relationshipStructName = "R"
|
||||||
|
sentinel = uint64(255)
|
||||||
)
|
)
|
||||||
|
|
||||||
// BindP executes the query and inserts the
|
// BindP executes the query and inserts the
|
||||||
|
@ -329,17 +329,7 @@ func bindMapping(typ reflect.Type, titleCases map[string]string, cols []string)
|
||||||
|
|
||||||
ColLoop:
|
ColLoop:
|
||||||
for i, c := range cols {
|
for i, c := range cols {
|
||||||
names := strings.Split(c, ".")
|
name := strmangle.TitleCaseIdentifier(c, titleCases)
|
||||||
for j, n := range names {
|
|
||||||
t, ok := titleCases[n]
|
|
||||||
if ok {
|
|
||||||
names[j] = t
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
names[j] = strmangle.TitleCase(n)
|
|
||||||
}
|
|
||||||
name := strings.Join(names, ".")
|
|
||||||
|
|
||||||
ptrMap, ok := mapping[name]
|
ptrMap, ok := mapping[name]
|
||||||
if ok {
|
if ok {
|
||||||
ptrs[i] = ptrMap
|
ptrs[i] = ptrMap
|
||||||
|
@ -370,8 +360,6 @@ func ptrsFromMapping(val reflect.Value, mapping []uint64) []interface{} {
|
||||||
return ptrs
|
return ptrs
|
||||||
}
|
}
|
||||||
|
|
||||||
var sentinel = uint64(255)
|
|
||||||
|
|
||||||
// ptrFromMapping expects to be passed an addressable struct that it's looking
|
// ptrFromMapping expects to be passed an addressable struct that it's looking
|
||||||
// for things on.
|
// for things on.
|
||||||
func ptrFromMapping(val reflect.Value, mapping uint64) reflect.Value {
|
func ptrFromMapping(val reflect.Value, mapping uint64) reflect.Value {
|
||||||
|
@ -429,80 +417,6 @@ func makeStructMappingHelper(typ reflect.Type, prefix string, current uint64, de
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func bin64(i uint64) string {
|
|
||||||
str := strconv.FormatUint(i, 2)
|
|
||||||
pad := 64 - len(str)
|
|
||||||
if pad > 0 {
|
|
||||||
str = strings.Repeat("0", pad) + str
|
|
||||||
}
|
|
||||||
|
|
||||||
var newStr string
|
|
||||||
for i := 0; i < len(str); i += 8 {
|
|
||||||
if i != 0 {
|
|
||||||
newStr += " "
|
|
||||||
}
|
|
||||||
newStr += str[i : i+8]
|
|
||||||
}
|
|
||||||
|
|
||||||
return newStr
|
|
||||||
}
|
|
||||||
|
|
||||||
func findField(names []string, titleCases map[string]string, v reflect.Value) (interface{}, bool) {
|
|
||||||
if !v.IsValid() || len(names) == 0 {
|
|
||||||
return nil, false
|
|
||||||
}
|
|
||||||
|
|
||||||
if v.Kind() == reflect.Ptr {
|
|
||||||
if v.IsNil() {
|
|
||||||
return nil, false
|
|
||||||
}
|
|
||||||
v = reflect.Indirect(v)
|
|
||||||
}
|
|
||||||
|
|
||||||
if v.Kind() != reflect.Struct {
|
|
||||||
return nil, false
|
|
||||||
}
|
|
||||||
|
|
||||||
var name string
|
|
||||||
var ok bool
|
|
||||||
name, ok = titleCases[names[0]]
|
|
||||||
if !ok {
|
|
||||||
name = strmangle.TitleCase(names[0])
|
|
||||||
}
|
|
||||||
typ := v.Type()
|
|
||||||
|
|
||||||
n := typ.NumField()
|
|
||||||
for i := 0; i < n; i++ {
|
|
||||||
f := typ.Field(i)
|
|
||||||
fieldName, recurse := getBoilTag(f, titleCases)
|
|
||||||
|
|
||||||
if fieldName == "-" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if recurse {
|
|
||||||
if fieldName == name {
|
|
||||||
names = names[1:]
|
|
||||||
}
|
|
||||||
if ptr, ok := findField(names, titleCases, v.Field(i)); ok {
|
|
||||||
return ptr, ok
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if fieldName != name || len(names) > 1 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
fieldVal := v.Field(i)
|
|
||||||
if fieldVal.Kind() != reflect.Ptr {
|
|
||||||
return fieldVal.Addr().Interface(), true
|
|
||||||
}
|
|
||||||
return fieldVal.Interface(), true
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil, false
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
name = field.Name
|
||||||
|
|
|
@ -3,6 +3,8 @@ package boil
|
||||||
import (
|
import (
|
||||||
"database/sql/driver"
|
"database/sql/driver"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -10,6 +12,24 @@ import (
|
||||||
"gopkg.in/nullbio/null.v4"
|
"gopkg.in/nullbio/null.v4"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func bin64(i uint64) string {
|
||||||
|
str := strconv.FormatUint(i, 2)
|
||||||
|
pad := 64 - len(str)
|
||||||
|
if pad > 0 {
|
||||||
|
str = strings.Repeat("0", pad) + str
|
||||||
|
}
|
||||||
|
|
||||||
|
var newStr string
|
||||||
|
for i := 0; i < len(str); i += 8 {
|
||||||
|
if i != 0 {
|
||||||
|
newStr += " "
|
||||||
|
}
|
||||||
|
newStr += str[i : i+8]
|
||||||
|
}
|
||||||
|
|
||||||
|
return newStr
|
||||||
|
}
|
||||||
|
|
||||||
type mockRowMaker struct {
|
type mockRowMaker struct {
|
||||||
int
|
int
|
||||||
rows []driver.Value
|
rows []driver.Value
|
||||||
|
|
|
@ -262,6 +262,54 @@ func CamelCase(name string) string {
|
||||||
return buf.String()
|
return buf.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TitleCaseIdentifier splits on dots and then titlecases each fragment.
|
||||||
|
// map titleCase (split c ".")
|
||||||
|
func TitleCaseIdentifier(id string, titleCases map[string]string) string {
|
||||||
|
nextDot := strings.IndexByte(id, '.')
|
||||||
|
if nextDot < 0 {
|
||||||
|
titled, ok := titleCases[id]
|
||||||
|
if !ok {
|
||||||
|
titled = TitleCase(id)
|
||||||
|
}
|
||||||
|
return titled
|
||||||
|
}
|
||||||
|
|
||||||
|
buf := GetBuffer()
|
||||||
|
lastDot := 0
|
||||||
|
ln := len(id)
|
||||||
|
addDots := false
|
||||||
|
|
||||||
|
for i := 0; nextDot >= 0; i++ {
|
||||||
|
fmt.Println(lastDot, nextDot)
|
||||||
|
fragment := id[lastDot:nextDot]
|
||||||
|
|
||||||
|
titled, ok := titleCases[fragment]
|
||||||
|
if !ok {
|
||||||
|
titled = TitleCase(fragment)
|
||||||
|
}
|
||||||
|
|
||||||
|
if addDots {
|
||||||
|
buf.WriteByte('.')
|
||||||
|
}
|
||||||
|
buf.WriteString(titled)
|
||||||
|
addDots = true
|
||||||
|
|
||||||
|
if nextDot == ln {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
lastDot = nextDot + 1
|
||||||
|
if nextDot = strings.IndexByte(id[lastDot:], '.'); nextDot >= 0 {
|
||||||
|
nextDot += lastDot
|
||||||
|
} else {
|
||||||
|
nextDot = ln
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
PutBuffer(buf)
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
|
|
||||||
// MakeStringMap converts a map[string]string into the format:
|
// MakeStringMap converts a map[string]string into the format:
|
||||||
// "key": "value", "key": "value"
|
// "key": "value", "key": "value"
|
||||||
func MakeStringMap(types map[string]string) string {
|
func MakeStringMap(types map[string]string) string {
|
||||||
|
|
|
@ -229,6 +229,25 @@ func TestCamelCase(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestTitleCaseIdentifier(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
In string
|
||||||
|
Out string
|
||||||
|
}{
|
||||||
|
{"hello", "Hello"},
|
||||||
|
{"hello.world", "Hello.World"},
|
||||||
|
{"hey.id.world", "Hey.ID.World"},
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, test := range tests {
|
||||||
|
if out := TitleCaseIdentifier(test.In, nil); out != test.Out {
|
||||||
|
t.Errorf("[%d] (%s) Out was wrong: %q, want: %q", i, test.In, out, test.Out)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestMakeStringMap(t *testing.T) {
|
func TestMakeStringMap(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue