Add --no-hooks feature

This commit is contained in:
Patrick O'brien 2016-08-28 20:48:50 +10:00
parent 09cdb7a652
commit 95ff24b918
11 changed files with 32 additions and 0 deletions

View file

@ -7,6 +7,7 @@ type Config struct {
OutFolder string `toml:"out_folder"` OutFolder string `toml:"out_folder"`
BaseDir string `toml:"base_dir"` BaseDir string `toml:"base_dir"`
ExcludeTables []string `toml:"exclude"` ExcludeTables []string `toml:"exclude"`
NoHooks bool `toml:"no_hooks"`
Postgres PostgresConfig `toml:"postgres"` Postgres PostgresConfig `toml:"postgres"`
} }

View file

@ -65,6 +65,7 @@ func main() {
rootCmd.PersistentFlags().StringP("basedir", "b", "", "The base directory templates and templates_test folders are") rootCmd.PersistentFlags().StringP("basedir", "b", "", "The base directory templates and templates_test folders are")
rootCmd.PersistentFlags().StringSliceP("exclude", "x", nil, "Tables to be excluded from the generated package") rootCmd.PersistentFlags().StringSliceP("exclude", "x", nil, "Tables to be excluded from the generated package")
rootCmd.PersistentFlags().BoolP("debug", "d", false, "Debug mode prints stack traces on error") rootCmd.PersistentFlags().BoolP("debug", "d", false, "Debug mode prints stack traces on error")
rootCmd.PersistentFlags().BoolP("no-hooks", "", false, "Disable hooks feature for your models")
viper.SetDefault("postgres.sslmode", "require") viper.SetDefault("postgres.sslmode", "require")
viper.SetDefault("postgres.port", "5432") viper.SetDefault("postgres.port", "5432")
@ -104,6 +105,7 @@ func preRun(cmd *cobra.Command, args []string) error {
DriverName: driverName, DriverName: driverName,
OutFolder: viper.GetString("output"), OutFolder: viper.GetString("output"),
PkgName: viper.GetString("pkgname"), PkgName: viper.GetString("pkgname"),
NoHooks: viper.GetBool("no-hooks"),
} }
// BUG: https://github.com/spf13/viper/issues/200 // BUG: https://github.com/spf13/viper/issues/200

View file

@ -81,6 +81,7 @@ func (s *State) Run(includeTests bool) error {
DriverName: s.Config.DriverName, DriverName: s.Config.DriverName,
UseLastInsertID: s.Driver.UseLastInsertID(), UseLastInsertID: s.Driver.UseLastInsertID(),
PkgName: s.Config.PkgName, PkgName: s.Config.PkgName,
NoHooks: s.Config.NoHooks,
StringFuncs: templateStringMappers, StringFuncs: templateStringMappers,
} }
@ -110,6 +111,7 @@ func (s *State) Run(includeTests bool) error {
DriverName: s.Config.DriverName, DriverName: s.Config.DriverName,
UseLastInsertID: s.Driver.UseLastInsertID(), UseLastInsertID: s.Driver.UseLastInsertID(),
PkgName: s.Config.PkgName, PkgName: s.Config.PkgName,
NoHooks: s.Config.NoHooks,
StringFuncs: templateStringMappers, StringFuncs: templateStringMappers,
} }

View file

@ -18,6 +18,7 @@ type templateData struct {
DriverName string DriverName string
UseLastInsertID bool UseLastInsertID bool
PkgName string PkgName string
NoHooks bool
StringFuncs map[string]func(string) string StringFuncs map[string]func(string) string
} }

View file

@ -14,7 +14,9 @@ var (
type ( type (
{{$tableNameSingular}}Slice []*{{$tableNameSingular}} {{$tableNameSingular}}Slice []*{{$tableNameSingular}}
{{if eq .NoHooks false -}}
{{$tableNameSingular}}Hook func(*{{$tableNameSingular}}) error {{$tableNameSingular}}Hook func(*{{$tableNameSingular}}) error
{{- end}}
{{$varNameSingular}}Query struct { {{$varNameSingular}}Query struct {
*boil.Query *boil.Query

View file

@ -1,3 +1,4 @@
{{- if eq .NoHooks false -}}
{{- $tableNameSingular := .Table.Name | singular | titleCase -}} {{- $tableNameSingular := .Table.Name | singular | titleCase -}}
{{- $varNameSingular := .Table.Name | singular | camelCase -}} {{- $varNameSingular := .Table.Name | singular | camelCase -}}
var {{$varNameSingular}}BeforeCreateHooks []{{$tableNameSingular}}Hook var {{$varNameSingular}}BeforeCreateHooks []{{$tableNameSingular}}Hook
@ -89,3 +90,4 @@ func {{$tableNameSingular}}AddHook(hookPoint boil.HookPoint, {{$varNameSingular}
{{$varNameSingular}}AfterUpsertHooks = append({{$varNameSingular}}AfterUpsertHooks, {{$varNameSingular}}Hook) {{$varNameSingular}}AfterUpsertHooks = append({{$varNameSingular}}AfterUpsertHooks, {{$varNameSingular}}Hook)
} }
} }
{{- end}}

View file

@ -40,9 +40,11 @@ func (o *{{$tableNameSingular}}) Insert(exec boil.Executor, whitelist ... string
) )
var err error var err error
{{if eq .NoHooks false -}}
if err := o.doBeforeCreateHooks(); err != nil { if err := o.doBeforeCreateHooks(); err != nil {
return err return err
} }
{{- end}}
ins := fmt.Sprintf(`INSERT INTO {{.Table.Name}} ("%s") VALUES (%s)`, strings.Join(wl, `","`), strmangle.Placeholders(len(wl), 1, 1)) ins := fmt.Sprintf(`INSERT INTO {{.Table.Name}} ("%s") VALUES (%s)`, strings.Join(wl, `","`), strmangle.Placeholders(len(wl), 1, 1))
@ -57,9 +59,11 @@ func (o *{{$tableNameSingular}}) Insert(exec boil.Executor, whitelist ... string
return errors.Wrap(err, "{{.PkgName}}: unable to insert into {{.Table.Name}}") return errors.Wrap(err, "{{.PkgName}}: unable to insert into {{.Table.Name}}")
} }
{{if eq .NoHooks false -}}
if len(returnColumns) == 0 { if len(returnColumns) == 0 {
return o.doAfterCreateHooks() return o.doAfterCreateHooks()
} }
{{- end}}
lastID, err := result.LastInsertId() lastID, err := result.LastInsertId()
if err != nil || lastID == 0 || len({{$varNameSingular}}AutoIncPrimaryKeys) != 1 { if err != nil || lastID == 0 || len({{$varNameSingular}}AutoIncPrimaryKeys) != 1 {
@ -89,5 +93,9 @@ func (o *{{$tableNameSingular}}) Insert(exec boil.Executor, whitelist ... string
} }
{{end}} {{end}}
{{if eq .NoHooks false -}}
return o.doAfterCreateHooks() return o.doAfterCreateHooks()
{{- else -}}
return nil
{{- end}}
} }

View file

@ -35,9 +35,11 @@ func (o *{{$tableNameSingular}}) UpdateP(exec boil.Executor, whitelist ... strin
// Update does not automatically update the record in case of default values. Use .Reload() // Update does not automatically update the record in case of default values. Use .Reload()
// to refresh the records. // to refresh the records.
func (o *{{$tableNameSingular}}) Update(exec boil.Executor, whitelist ... string) error { func (o *{{$tableNameSingular}}) Update(exec boil.Executor, whitelist ... string) error {
{{if eq .NoHooks false -}}
if err := o.doBeforeUpdateHooks(); err != nil { if err := o.doBeforeUpdateHooks(); err != nil {
return err return err
} }
{{- end}}
var err error var err error
var query string var query string
@ -66,7 +68,11 @@ func (o *{{$tableNameSingular}}) Update(exec boil.Executor, whitelist ... string
return errors.Errorf("failed to update single row, updated %d rows", r) return errors.Errorf("failed to update single row, updated %d rows", r)
} }
{{if eq .NoHooks false -}}
return o.doAfterUpdateHooks() return o.doAfterUpdateHooks()
{{- else -}}
return nil
{{- end}}
} }
// UpdateAllP updates all rows with matching column names, and panics on error. // UpdateAllP updates all rows with matching column names, and panics on error.

View file

@ -48,9 +48,11 @@ func (o *{{$tableNameSingular}}) Upsert(exec boil.Executor, updateOnConflict boo
query := generateUpsertQuery("{{.Table.Name}}", updateOnConflict, ret, update, conflict, whitelist) query := generateUpsertQuery("{{.Table.Name}}", updateOnConflict, ret, update, conflict, whitelist)
var err error var err error
{{if eq .NoHooks false -}}
if err := o.doBeforeUpsertHooks(); err != nil { if err := o.doBeforeUpsertHooks(); err != nil {
return err return err
} }
{{- end}}
if boil.DebugMode { if boil.DebugMode {
fmt.Fprintln(boil.DebugWriter, query) fmt.Fprintln(boil.DebugWriter, query)
@ -71,9 +73,11 @@ func (o *{{$tableNameSingular}}) Upsert(exec boil.Executor, updateOnConflict boo
return errors.Wrap(err, "{{.PkgName}}: unable to upsert for {{.Table.Name}}") return errors.Wrap(err, "{{.PkgName}}: unable to upsert for {{.Table.Name}}")
} }
{{if eq .NoHooks false -}}
if err := o.doAfterUpsertHooks(); err != nil { if err := o.doAfterUpsertHooks(); err != nil {
return err return err
} }
{{- end}}
return nil return nil
} }

View file

@ -1,3 +1,4 @@
{{- if eq .NoHooks false -}}
{{- $tableNameSingular := .Table.Name | singular | titleCase -}} {{- $tableNameSingular := .Table.Name | singular | titleCase -}}
{{- $tableNamePlural := .Table.Name | plural | titleCase -}} {{- $tableNamePlural := .Table.Name | plural | titleCase -}}
{{- $varNamePlural := .Table.Name | plural | camelCase -}} {{- $varNamePlural := .Table.Name | plural | camelCase -}}
@ -45,3 +46,4 @@ func test{{$tableNamePlural}}Hooks(t *testing.T) {
{{$varNameSingular}}BeforeCreateHooks = []{{$tableNameSingular}}Hook{} {{$varNameSingular}}BeforeCreateHooks = []{{$tableNameSingular}}Hook{}
} }
{{- end}}

View file

@ -115,6 +115,7 @@ func TestHelpers(t *testing.T) {
{{- end -}} {{- end -}}
} }
{{if eq .NoHooks false -}}
func TestHooks(t *testing.T) { func TestHooks(t *testing.T) {
{{- range $index, $table := .Tables}} {{- range $index, $table := .Tables}}
{{- if $table.IsJoinTable -}} {{- if $table.IsJoinTable -}}
@ -124,6 +125,7 @@ func TestHooks(t *testing.T) {
{{end -}} {{end -}}
{{- end -}} {{- end -}}
} }
{{- end}}
func TestInsert(t *testing.T) { func TestInsert(t *testing.T) {
{{- range $index, $table := .Tables}} {{- range $index, $table := .Tables}}