Add new functions to support more templating

- Add joinSlices
- Add ways to pull apart a slice of SQLColumnDef
- Try to stop generating join tables (but it still gens for some reason)
This commit is contained in:
Aaron L 2016-06-26 23:54:23 -07:00
parent f7a4d3d34b
commit 6dac631a93
5 changed files with 83 additions and 2 deletions

View file

@ -34,9 +34,33 @@ func (s SQLColumnDef) String() string {
return fmt.Sprintf("%s %s", s.Name, s.Type)
}
// SQLColumnDefs has small helper functions
type SQLColumnDefs []SQLColumnDef
// Names returns all the names
func (s SQLColumnDefs) Names() []string {
names := make([]string, len(s))
for i, sqlDef := range s {
names[i] = sqlDef.Name
}
return names
}
// Types returns all the types
func (s SQLColumnDefs) Types() []string {
types := make([]string, len(s))
for i, sqlDef := range s {
types[i] = sqlDef.Type
}
return types
}
// SQLColDefinitions creates a definition in sql format for a column
// example: id int64, thingName string
func SQLColDefinitions(cols []Column, names []string) []SQLColumnDef {
func SQLColDefinitions(cols []Column, names []string) SQLColumnDefs {
ret := make([]SQLColumnDef, len(names))
for i, n := range names {

View file

@ -84,6 +84,11 @@ type executeTemplateData struct {
}
func executeTemplates(e executeTemplateData) error {
if e.data.Table.IsJoinTable {
return nil
}
fmt.Println(e.data.Table.Name, e.data.Table.IsJoinTable)
var out [][]byte
var imps imports
@ -112,6 +117,11 @@ func executeTemplates(e executeTemplateData) error {
}
func executeSingletonTemplates(e executeTemplateData) error {
if e.data.Table.IsJoinTable {
return nil
}
fmt.Println(e.data.Table.Name, e.data.Table.IsJoinTable)
rgxRemove := regexp.MustCompile(`[0-9]+_`)
for _, template := range e.templates {

View file

@ -150,3 +150,20 @@ func DriverUsesLastInsertID(driverName string) bool {
func Substring(start, end int, str string) string {
return str[start:end]
}
// JoinSlices merges two string slices of equal length
func JoinSlices(sep string, a, b []string) []string {
lna, lnb := len(a), len(b)
if lna != lnb {
panic("joinSlices: can only merge slices of same length")
} else if lna == 0 {
return nil
}
ret := make([]string, len(a))
for i, elem := range a {
ret[i] = fmt.Sprintf("%s%s%s", elem, sep, b[i])
}
return ret
}

View file

@ -191,3 +191,32 @@ func TestSubstring(t *testing.T) {
t.Errorf("substring was wrong: %q", got)
}
}
func TestJoinSlices(t *testing.T) {
t.Parallel()
ret := JoinSlices("", nil, nil)
if ret != nil {
t.Error("want nil, got:", ret)
}
ret = JoinSlices(" ", []string{"one", "two"}, []string{"three", "four"})
if got := ret[0]; got != "one three" {
t.Error("ret element was wrong:", got)
}
if got := ret[1]; got != "two four" {
t.Error("ret element was wrong:", got)
}
}
func TestJoinSlicesFail(t *testing.T) {
t.Parallel()
defer func() {
if recover() == nil {
t.Error("did not panic")
}
}()
JoinSlices("", nil, []string{"hello"})
}

View file

@ -123,6 +123,7 @@ var templateFunctions = template.FuncMap{
// String Slice ops
"join": func(sep string, slice []string) string { return strings.Join(slice, sep) },
"joinSlices": strmangle.JoinSlices,
"stringMap": strmangle.StringMap,
"hasElement": strmangle.HasElement,
"prefixStringSlice": strmangle.PrefixStringSlice,