Fix up a number more template errors from refactor
- Add quoteWrap function to help where some of the old helpers were covering.
This commit is contained in:
parent
119e683952
commit
4d0742b6f3
8 changed files with 23 additions and 14 deletions
|
@ -120,7 +120,7 @@ func executeSingletonTemplates(e executeTemplateData) error {
|
|||
|
||||
fName := template.Name()
|
||||
ext := filepath.Ext(fName)
|
||||
fName = fName[0 : len(fName)-len(ext)]
|
||||
fName = rgxRemove.ReplaceAllString(fName[:len(fName)-len(ext)], "")
|
||||
|
||||
imps := imports{
|
||||
standard: e.importNamedSet[fName].standard,
|
||||
|
@ -129,7 +129,7 @@ func executeSingletonTemplates(e executeTemplateData) error {
|
|||
|
||||
err = outHandler(
|
||||
e.state.Config.OutFolder,
|
||||
rgxRemove.ReplaceAllString(fName, "")+e.fileSuffix,
|
||||
fName+e.fileSuffix,
|
||||
e.state.Config.PkgName,
|
||||
imps,
|
||||
[][]byte{resp},
|
||||
|
|
|
@ -86,6 +86,10 @@ func loadTemplate(dir string, filename string) (*template.Template, error) {
|
|||
// templateStringMappers are placed into the data to make it easy to use the
|
||||
// stringMap function.
|
||||
var templateStringMappers = map[string]func(string) string{
|
||||
// String ops
|
||||
"quoteWrap": func(a string) string { return fmt.Sprintf(`"%s"`, a) },
|
||||
|
||||
// Pluralization
|
||||
"singular": strmangle.Singular,
|
||||
"plural": strmangle.Plural,
|
||||
|
||||
|
@ -104,6 +108,7 @@ var templateFunctions = template.FuncMap{
|
|||
"substring": strmangle.Substring,
|
||||
"remove": func(rem string, str string) string { return strings.Replace(str, rem, "", -1) },
|
||||
"prefix": func(add string, str string) string { return fmt.Sprintf("%s%s", add, str) },
|
||||
"quoteWrap": func(a string) string { return fmt.Sprintf(`"%s"`, a) },
|
||||
|
||||
// Pluralization
|
||||
"singular": strmangle.Singular,
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
{{- $varNameSingular := .Table.Name | singular | camelCase -}}
|
||||
{{- $tableNameSingular := .Table.Name | singular | titleCase -}}
|
||||
var (
|
||||
{{$varNameSingular}}Columns = []string{{"{"}}{{.Table.Columns | columnNames | join ", "}}{{"}"}}
|
||||
{{$varNameSingular}}Columns = []string{{"{"}}{{.Table.Columns | columnNames | stringMap .StringFuncs.quoteWrap | join ", "}}{{"}"}}
|
||||
{{$varNameSingular}}ColumnsWithoutDefault = []string{{"{"}}{{filterColumnsByDefault .Table.Columns false}}{{"}"}}
|
||||
{{$varNameSingular}}ColumnsWithDefault = []string{{"{"}}{{filterColumnsByDefault .Table.Columns true}}{{"}"}}
|
||||
{{$varNameSingular}}PrimaryKeyColumns = []string{{"{"}}{{.Table.PKey.Columns | join ", "}}{{"}"}}
|
||||
{{$varNameSingular}}PrimaryKeyColumns = []string{{"{"}}{{.Table.PKey.Columns | stringMap .StringFuncs.quoteWrap | join ", "}}{{"}"}}
|
||||
{{$varNameSingular}}AutoIncrementColumns = []string{{"{"}}{{filterColumnsByAutoIncrement .Table.Columns}}{{"}"}}
|
||||
{{$varNameSingular}}AutoIncPrimaryKey = "{{autoIncPrimaryKey .Table.Columns .Table.PKey}}"
|
||||
)
|
||||
|
|
|
@ -1,10 +1,11 @@
|
|||
{{- $tableNameSingular := .Table.Name | singular | titleCase -}}
|
||||
{{- $varNameSingular := .Table.Name | singular | camelCase -}}
|
||||
// {{$tableNameSingular}}Insert inserts a single record.
|
||||
// Insert a single record.
|
||||
func (o *{{$tableNameSingular}}) Insert(whitelist ... string) error {
|
||||
return o.InsertX(boil.GetDB(), whitelist...)
|
||||
}
|
||||
|
||||
// InsertX a single record using an executor.
|
||||
func (o *{{$tableNameSingular}}) InsertX(exec boil.Executor, whitelist ... string) error {
|
||||
if o == nil {
|
||||
return errors.New("{{.PkgName}}: no {{.Table.Name}} provided for insertion")
|
||||
|
@ -65,7 +66,7 @@ func (o *{{$tableNameSingular}}) InsertX(exec boil.Executor, whitelist ... strin
|
|||
ins = ins + fmt.Sprintf(` RETURNING %s`, strings.Join(returnColumns, ","))
|
||||
err = exec.QueryRow(ins, boil.GetStructValues(o, wl...)...).Scan(boil.GetStructPointers(o, returnColumns...)...)
|
||||
} else {
|
||||
_, err = exec.Exec(ins, {{.Table.Columns | columnNames | prefixStringSlice "o." | join ", "}})
|
||||
_, err = exec.Exec(ins, {{.Table.Columns | columnNames | stringMap .StringFuncs.titleCase | prefixStringSlice "o." | join ", "}})
|
||||
}
|
||||
{{end}}
|
||||
|
||||
|
|
|
@ -9,14 +9,17 @@ func (o *{{$tableNameSingular}}) Update(whitelist ... string) error {
|
|||
return o.UpdateX(boil.GetDB(), whitelist...)
|
||||
}
|
||||
|
||||
// UpdateX uses an executor to update the {{$tableNameSingular}}.
|
||||
func (o *{{$tableNameSingular}}) UpdateX(exec boil.Executor, whitelist ... string) error {
|
||||
return o.UpdateAtX(exec, {{.Table.PKey.Columns | prefixStringSlice "o." | stringMap .StringFuncs.titleCase | join ", "}}, whitelist...)
|
||||
return o.UpdateAtX(exec, {{.Table.PKey.Columns | stringMap .StringFuncs.titleCase | prefixStringSlice "o." | join ", "}}, whitelist...)
|
||||
}
|
||||
|
||||
// UpdateAt updates the {{$tableNameSingular}} using the primary key to find the row to update.
|
||||
func (o *{{$tableNameSingular}}) UpdateAt({{primaryKeyFuncSig .Table.Columns .Table.PKey.Columns}}, whitelist ...string) error {
|
||||
return o.UpdateAtX(boil.GetDB(), {{.Table.PKey.Columns | stringMap .StringFuncs.camelCase | join ", "}}, whitelist...)
|
||||
}
|
||||
|
||||
// UpdateAtX uses an executor to update the {{$tableNameSingular}} using the primary key to find the row to update.
|
||||
func (o *{{$tableNameSingular}}) UpdateAtX(exec boil.Executor, {{primaryKeyFuncSig .Table.Columns .Table.PKey.Columns}}, whitelist ...string) error {
|
||||
if err := o.doBeforeUpdateHooks(); err != nil {
|
||||
return err
|
||||
|
@ -36,8 +39,8 @@ func (o *{{$tableNameSingular}}) UpdateAtX(exec boil.Executor, {{primaryKeyFuncS
|
|||
var err error
|
||||
var query string
|
||||
if len(whitelist) != 0 {
|
||||
query = fmt.Sprintf(`UPDATE {{.Table.Name}} SET %s WHERE %s`, boil.SetParamNames(whitelist), boil.WherePrimaryKey(len(whitelist)+1, {{.Table.PKey.Columns | join ", "}}))
|
||||
_, err = exec.Exec(query, boil.GetStructValues(o, whitelist...), {{.Table.PKey.Columns | stringMap .StringFuncs.camelCase | prefixStringSlice "o." | join ", "}})
|
||||
query = fmt.Sprintf(`UPDATE {{.Table.Name}} SET %s WHERE %s`, boil.SetParamNames(whitelist), boil.WherePrimaryKey(len(whitelist)+1, {{.Table.PKey.Columns | stringMap .StringFuncs.quoteWrap | join ", "}}))
|
||||
_, err = exec.Exec(query, boil.GetStructValues(o, whitelist...), {{.Table.PKey.Columns | stringMap .StringFuncs.titleCase | prefixStringSlice "o." | join ", "}})
|
||||
} else {
|
||||
return fmt.Errorf("{{.PkgName}}: unable to update {{.Table.Name}}, could not build a whitelist for row: %s", err)
|
||||
}
|
||||
|
|
|
@ -19,7 +19,7 @@ func (o *{{$tableNameSingular}}) DeleteX(exec boil.Executor) error {
|
|||
|
||||
mods = append(mods,
|
||||
qm.Table("{{.Table.Name}}"),
|
||||
qm.Where("{{wherePrimaryKey .Table.PKey.Columns 1}}", {{.Table.PKey.Columns | stringMap .StringFuncs.camelCase | prefixStringSlice "o." | join ", "}}),
|
||||
qm.Where("{{wherePrimaryKey .Table.PKey.Columns 1}}", {{.Table.PKey.Columns | stringMap .StringFuncs.titleCase | prefixStringSlice "o." | join ", "}}),
|
||||
)
|
||||
|
||||
query := NewQueryX(exec, mods...)
|
||||
|
@ -63,7 +63,7 @@ func (o {{$varNameSingular}}Slice) DeleteAllX(exec boil.Executor) error {
|
|||
var mods []qm.QueryMod
|
||||
|
||||
args := o.inPrimaryKeyArgs()
|
||||
in := boil.WherePrimaryKeyIn(len(o), {{.Table.PKey.Columns | join ", "}})
|
||||
in := boil.WherePrimaryKeyIn(len(o), {{.Table.PKey.Columns | stringMap .StringFuncs.quoteWrap | join ", "}})
|
||||
|
||||
mods = append(mods,
|
||||
qm.Table("{{.Table.Name}}"),
|
||||
|
|
|
@ -22,11 +22,11 @@ func Test{{$tableNamePlural}}Find(t *testing.T) {
|
|||
j := make({{$varNameSingular}}Slice, 3)
|
||||
// Perform all Find queries and assign result objects to slice for comparison
|
||||
for i := 0; i < len(j); i++ {
|
||||
j[i], err = {{$tableNameSingular}}Find({{.Table.PKey.Columns | stringMap .StringFuncs.camelCase | prefixStringSlice "o[i]." | join ", "}})
|
||||
j[i], err = {{$tableNameSingular}}Find({{.Table.PKey.Columns | stringMap .StringFuncs.titleCase | prefixStringSlice "o[i]." | join ", "}})
|
||||
{{$varNameSingular}}CompareVals(o[i], j[i], t)
|
||||
}
|
||||
|
||||
f, err := {{$tableNameSingular}}Find({{.Table.PKey.Columns | stringMap .StringFuncs.camelCase | prefixStringSlice "o[0]." | join ", "}}, {{$varNameSingular}}PrimaryKeyColumns...)
|
||||
f, err := {{$tableNameSingular}}Find({{.Table.PKey.Columns | stringMap .StringFuncs.titleCase | prefixStringSlice "o[0]." | join ", "}}, {{$varNameSingular}}PrimaryKeyColumns...)
|
||||
{{range $key, $value := .Table.PKey.Columns}}
|
||||
if o[0].{{titleCase $value}} != f.{{titleCase $value}} {
|
||||
t.Errorf("Expected primary key values to match, {{titleCase $value}} did not match")
|
||||
|
|
|
@ -19,7 +19,7 @@ func Test{{$tableNamePlural}}Bind(t *testing.T) {
|
|||
|
||||
j := {{$tableNameSingular}}{}
|
||||
|
||||
err = {{$tableNamePlural}}(qm.Where("{{wherePrimaryKey .Table.PKey.Columns 1}}", {{.Table.PKey.Columns | stringMap .StringFuncs.camelCase | prefixStringSlice "o." | join ", "}})).Bind(&j)
|
||||
err = {{$tableNamePlural}}(qm.Where("{{wherePrimaryKey .Table.PKey.Columns 1}}", {{.Table.PKey.Columns | stringMap .StringFuncs.titleCase | prefixStringSlice "o." | join ", "}})).Bind(&j)
|
||||
if err != nil {
|
||||
t.Errorf("Unable to call Bind on {{$tableNameSingular}} single object: %s", err)
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue