2016-06-12 20:19:23 +02:00
|
|
|
package main
|
2016-06-12 03:25:00 +02:00
|
|
|
|
|
|
|
import (
|
2016-06-20 07:22:50 +02:00
|
|
|
"fmt"
|
2016-06-12 03:25:00 +02:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"text/template"
|
|
|
|
|
2016-08-09 09:59:30 +02:00
|
|
|
"github.com/vattle/sqlboiler/bdb"
|
|
|
|
"github.com/vattle/sqlboiler/strmangle"
|
2016-06-12 03:25:00 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// templateData for sqlboiler templates
|
|
|
|
type templateData struct {
|
2016-06-23 08:09:56 +02:00
|
|
|
Tables []bdb.Table
|
|
|
|
Table bdb.Table
|
2016-06-12 03:25:00 +02:00
|
|
|
DriverName string
|
|
|
|
PkgName string
|
2016-06-20 07:22:50 +02:00
|
|
|
|
|
|
|
StringFuncs map[string]func(string) string
|
2016-06-12 03:25:00 +02:00
|
|
|
}
|
|
|
|
|
2016-07-17 08:55:15 +02:00
|
|
|
type templateList struct {
|
|
|
|
*template.Template
|
|
|
|
}
|
|
|
|
|
|
|
|
type templateNameList []string
|
2016-06-12 03:25:00 +02:00
|
|
|
|
2016-07-17 08:55:15 +02:00
|
|
|
func (t templateNameList) Len() int {
|
2016-06-12 03:25:00 +02:00
|
|
|
return len(t)
|
|
|
|
}
|
|
|
|
|
2016-07-17 08:55:15 +02:00
|
|
|
func (t templateNameList) Swap(k, j int) {
|
2016-06-12 03:25:00 +02:00
|
|
|
t[k], t[j] = t[j], t[k]
|
|
|
|
}
|
|
|
|
|
2016-07-17 08:55:15 +02:00
|
|
|
func (t templateNameList) Less(k, j int) bool {
|
2016-06-12 03:25:00 +02:00
|
|
|
// Make sure "struct" goes to the front
|
2016-07-17 08:55:15 +02:00
|
|
|
if t[k] == "struct.tpl" {
|
2016-06-12 03:25:00 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-07-17 08:55:15 +02:00
|
|
|
res := strings.Compare(t[k], t[j])
|
2016-06-12 03:25:00 +02:00
|
|
|
if res <= 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-07-17 08:55:15 +02:00
|
|
|
// Templates returns the name of all the templates defined in the template list
|
|
|
|
func (t templateList) Templates() []string {
|
|
|
|
tplList := t.Template.Templates()
|
|
|
|
|
|
|
|
if len(tplList) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ret := make([]string, 0, len(tplList))
|
|
|
|
for _, tpl := range tplList {
|
|
|
|
if name := tpl.Name(); strings.HasSuffix(name, ".tpl") {
|
|
|
|
ret = append(ret, name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Sort(templateNameList(ret))
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2016-06-12 03:25:00 +02:00
|
|
|
// loadTemplates loads all of the template files in the specified directory.
|
2016-07-17 08:55:15 +02:00
|
|
|
func loadTemplates(dir string) (*templateList, error) {
|
2016-06-12 03:25:00 +02:00
|
|
|
wd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
pattern := filepath.Join(wd, dir, "*.tpl")
|
|
|
|
tpl, err := template.New("").Funcs(templateFunctions).ParseGlob(pattern)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-07-17 08:55:15 +02:00
|
|
|
return &templateList{Template: tpl}, err
|
2016-06-12 03:25:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// loadTemplate loads a single template file.
|
|
|
|
func loadTemplate(dir string, filename string) (*template.Template, error) {
|
|
|
|
wd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
pattern := filepath.Join(wd, dir, filename)
|
|
|
|
tpl, err := template.New("").Funcs(templateFunctions).ParseFiles(pattern)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return tpl.Lookup(filename), err
|
|
|
|
}
|
|
|
|
|
2016-06-20 07:22:50 +02:00
|
|
|
// templateStringMappers are placed into the data to make it easy to use the
|
|
|
|
// stringMap function.
|
|
|
|
var templateStringMappers = map[string]func(string) string{
|
2016-06-20 07:57:46 +02:00
|
|
|
// String ops
|
|
|
|
"quoteWrap": func(a string) string { return fmt.Sprintf(`"%s"`, a) },
|
|
|
|
|
|
|
|
// Pluralization
|
2016-06-20 07:22:50 +02:00
|
|
|
"singular": strmangle.Singular,
|
|
|
|
"plural": strmangle.Plural,
|
|
|
|
|
|
|
|
// Casing
|
|
|
|
"toLower": strings.ToLower,
|
|
|
|
"toUpper": strings.ToUpper,
|
|
|
|
"titleCase": strmangle.TitleCase,
|
|
|
|
"camelCase": strmangle.CamelCase,
|
2016-08-08 01:34:11 +02:00
|
|
|
"snakeCase": strmangle.SnakeCase,
|
2016-06-20 07:22:50 +02:00
|
|
|
}
|
|
|
|
|
2016-06-12 03:25:00 +02:00
|
|
|
// templateFunctions is a map of all the functions that get passed into the
|
|
|
|
// templates. If you wish to pass a new function into your own template,
|
|
|
|
// add a function pointer here.
|
|
|
|
var templateFunctions = template.FuncMap{
|
2016-06-20 07:22:50 +02:00
|
|
|
// String ops
|
2016-07-06 08:02:35 +02:00
|
|
|
"trimPrefix": func(pre, str string) string { return strings.TrimPrefix(str, pre) },
|
|
|
|
"remove": func(rem, str string) string { return strings.Replace(str, rem, "", -1) },
|
|
|
|
"replace": func(rep, with, str string) string { return strings.Replace(str, rep, with, -1) },
|
|
|
|
"prefix": func(add, str string) string { return fmt.Sprintf("%s%s", add, str) },
|
|
|
|
"quoteWrap": func(a string) string { return fmt.Sprintf(`"%s"`, a) },
|
2016-07-14 05:31:01 +02:00
|
|
|
"substring": strmangle.Substring,
|
2016-07-06 08:04:54 +02:00
|
|
|
"id": strmangle.Identifier,
|
2016-06-20 07:22:50 +02:00
|
|
|
|
|
|
|
// Pluralization
|
|
|
|
"singular": strmangle.Singular,
|
|
|
|
"plural": strmangle.Plural,
|
|
|
|
|
|
|
|
// Casing
|
|
|
|
"toLower": strings.ToLower,
|
|
|
|
"toUpper": strings.ToUpper,
|
|
|
|
"titleCase": strmangle.TitleCase,
|
|
|
|
"camelCase": strmangle.CamelCase,
|
|
|
|
|
|
|
|
// String Slice ops
|
|
|
|
"join": func(sep string, slice []string) string { return strings.Join(slice, sep) },
|
2016-06-27 08:54:23 +02:00
|
|
|
"joinSlices": strmangle.JoinSlices,
|
2016-06-20 07:22:50 +02:00
|
|
|
"stringMap": strmangle.StringMap,
|
|
|
|
"hasElement": strmangle.HasElement,
|
|
|
|
"prefixStringSlice": strmangle.PrefixStringSlice,
|
|
|
|
|
2016-07-13 18:51:40 +02:00
|
|
|
// String Map ops
|
|
|
|
"makeStringMap": strmangle.MakeStringMap,
|
|
|
|
|
2016-06-20 07:22:50 +02:00
|
|
|
// Database related mangling
|
2016-06-23 08:48:49 +02:00
|
|
|
"whereClause": strmangle.WhereClause,
|
2016-06-20 07:22:50 +02:00
|
|
|
|
2016-07-14 05:31:01 +02:00
|
|
|
// Text helpers
|
2016-07-18 02:48:08 +02:00
|
|
|
"textsFromForeignKey": textsFromForeignKey,
|
|
|
|
"textsFromOneToOneRelationship": textsFromOneToOneRelationship,
|
|
|
|
"textsFromRelationship": textsFromRelationship,
|
2016-07-14 05:31:01 +02:00
|
|
|
|
2016-06-20 07:22:50 +02:00
|
|
|
// dbdrivers ops
|
|
|
|
"driverUsesLastInsertID": strmangle.DriverUsesLastInsertID,
|
|
|
|
"makeDBName": strmangle.MakeDBName,
|
2016-06-23 08:48:49 +02:00
|
|
|
"filterColumnsByDefault": bdb.FilterColumnsByDefault,
|
2016-07-06 08:02:35 +02:00
|
|
|
"filterColumnsBySimpleDefault": bdb.FilterColumnsBySimpleDefault,
|
2016-06-23 08:48:49 +02:00
|
|
|
"filterColumnsByAutoIncrement": bdb.FilterColumnsByAutoIncrement,
|
2016-08-08 01:34:11 +02:00
|
|
|
"filterColumnsByValidated": bdb.FilterColumnsByValidated,
|
2016-06-23 08:48:49 +02:00
|
|
|
"autoIncPrimaryKey": bdb.AutoIncPrimaryKey,
|
|
|
|
"sqlColDefinitions": bdb.SQLColDefinitions,
|
|
|
|
"sqlColDefStrings": bdb.SQLColDefStrings,
|
|
|
|
"columnNames": bdb.ColumnNames,
|
2016-07-15 14:26:45 +02:00
|
|
|
"columnTypes": bdb.ColumnTypes,
|
2016-07-13 18:51:40 +02:00
|
|
|
"columnDBTypes": bdb.ColumnDBTypes,
|
2016-07-08 18:39:36 +02:00
|
|
|
"defaultValues": bdb.DefaultValues,
|
2016-06-12 03:25:00 +02:00
|
|
|
}
|