Remove a bunch of unused code.

- Optimize the TitleCaseIdentifier loop
This commit is contained in:
Aaron L 2016-08-31 21:11:42 -07:00
parent 9f52cf6ec9
commit 2d0924a625
4 changed files with 89 additions and 88 deletions

View file

@ -4,7 +4,6 @@ import (
"database/sql"
"fmt"
"reflect"
"strconv"
"strings"
"sync"
@ -19,6 +18,7 @@ var (
const (
loadMethodPrefix = "Load"
relationshipStructName = "R"
sentinel = uint64(255)
)
// BindP executes the query and inserts the
@ -329,17 +329,7 @@ func bindMapping(typ reflect.Type, titleCases map[string]string, cols []string)
ColLoop:
for i, c := range cols {
names := strings.Split(c, ".")
for j, n := range names {
t, ok := titleCases[n]
if ok {
names[j] = t
continue
}
names[j] = strmangle.TitleCase(n)
}
name := strings.Join(names, ".")
name := strmangle.TitleCaseIdentifier(c, titleCases)
ptrMap, ok := mapping[name]
if ok {
ptrs[i] = ptrMap
@ -370,8 +360,6 @@ func ptrsFromMapping(val reflect.Value, mapping []uint64) []interface{} {
return ptrs
}
var sentinel = uint64(255)
// ptrFromMapping expects to be passed an addressable struct that it's looking
// for things on.
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) {
tag := field.Tag.Get("boil")
name = field.Name

View file

@ -3,6 +3,8 @@ package boil
import (
"database/sql/driver"
"reflect"
"strconv"
"strings"
"testing"
"time"
@ -10,6 +12,24 @@ import (
"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 {
int
rows []driver.Value

View file

@ -262,6 +262,54 @@ func CamelCase(name string) 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:
// "key": "value", "key": "value"
func MakeStringMap(types map[string]string) string {

View file

@ -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) {
t.Parallel()