Skip to content

Commit

Permalink
Simplify template parsing and generation
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenafamo committed Dec 12, 2024
1 parent 1c4d4a0 commit 3c5dba8
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 343 deletions.
31 changes: 31 additions & 0 deletions gen/bobgen-mysql/templates/models/100_blocks.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,34 @@ func (s {{$tAlias.UpSingular}}Setter) UpdateMod() bob.Mod[*dialect.UpdateQuery]
return um.Set(s.Expressions("{{$table.Name}}")...)
}
{{- end}}

{{define "one_update" -}}
{{$table := .Table}}
{{$tAlias := .Aliases.Table $table.Key -}}
{{$.Importer.Import (printf "github.com/stephenafamo/bob/dialect/%s/um" $.Dialect)}}
// Update uses an executor to update the {{$tAlias.UpSingular}}
func (o *{{$tAlias.UpSingular}}) Update(ctx context.Context, exec bob.Executor, s *{{$tAlias.UpSingular}}Setter) error {
_, err := {{$tAlias.UpPlural}}.Update(s.UpdateMod(), um.Where(o.pkEQ())).Exec(ctx, exec)
if err != nil {
return err
}

s.Overwrite(o)

return nil
}
{{- end}}

{{define "slice_update" -}}
{{$table := .Table}}
{{$tAlias := .Aliases.Table $table.Key -}}
func (o {{$tAlias.UpSingular}}Slice) UpdateAll(ctx context.Context, exec bob.Executor, vals {{$tAlias.UpSingular}}Setter) error {
_, err := {{$tAlias.UpPlural}}.Update(vals.UpdateMod(), o.UpdateMod()).Exec(ctx, exec)

for i := range o {
vals.Overwrite(o[i])
}

return err
}
{{- end}}
Original file line number Diff line number Diff line change
Expand Up @@ -18,37 +18,3 @@ func (e *UniqueConstraintError) Is(target error) bool {
return err.Number == 1062 && strings.Contains(err.Message, e.s)
}
{{end -}}


{{define "one_update" -}}
{{$table := .Table}}
{{$tAlias := .Aliases.Table $table.Key -}}
{{$.Importer.Import (printf "github.com/stephenafamo/bob/dialect/%s/um" $.Dialect)}}
// Update uses an executor to update the {{$tAlias.UpSingular}}
func (o *{{$tAlias.UpSingular}}) Update(ctx context.Context, exec bob.Executor, s *{{$tAlias.UpSingular}}Setter) error {
_, err := {{$tAlias.UpPlural}}.Update(s.UpdateMod(), um.Where(o.pkEQ())).Exec(ctx, exec)
if err != nil {
return err
}

s.Overwrite(o)

return nil
}
{{- end}}


{{define "slice_update" -}}
{{$table := .Table}}
{{$tAlias := .Aliases.Table $table.Key -}}
func (o {{$tAlias.UpSingular}}Slice) UpdateAll(ctx context.Context, exec bob.Executor, vals {{$tAlias.UpSingular}}Setter) error {
_, err := {{$tAlias.UpPlural}}.Update(vals.UpdateMod(), o.UpdateMod()).Exec(ctx, exec)

for i := range o {
vals.Overwrite(o[i])
}

return err
}
{{- end}}

28 changes: 28 additions & 0 deletions gen/bobgen-sqlite/templates/models/100_blocks.go.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{{define "setter_insert_mod" -}}
{{$.Importer.Import "io"}}
{{$.Importer.Import "github.com/stephenafamo/bob"}}
{{$table := .Table}}
{{$tAlias := .Aliases.Table $table.Key -}}
func (s *{{$tAlias.UpSingular}}Setter) Apply(q *dialect.InsertQuery) {
q.AppendHooks(func(ctx context.Context, exec bob.Executor) (context.Context, error) {
return {{$tAlias.UpPlural}}.BeforeInsertHooks.RunHooks(ctx, exec, s)
})

if len(q.Table.Columns) == 0 {
q.Table.Columns = s.SetColumns()
}

q.AppendValues(bob.ExpressionFunc(func(ctx context.Context, w io.Writer, d bob.Dialect, start int) ([]any, error){
vals := make([]bob.Expression, 0, {{len $table.NonGeneratedColumns}})
{{range $index, $column := $table.NonGeneratedColumns -}}
{{$colAlias := $tAlias.Column $column.Name -}}
if !s.{{$colAlias}}.IsUnset() {
vals = append(vals, {{$.Dialect}}.Arg(s.{{$colAlias}}))
}

{{end -}}

return bob.ExpressSlice(ctx, w, d, start, vals, "", ", ", "")
}))
}
{{- end}}
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,6 @@ var (
)
{{end -}}

{{define "setter_insert_mod" -}}
{{$.Importer.Import "io"}}
{{$.Importer.Import "github.com/stephenafamo/bob"}}
{{$table := .Table}}
{{$tAlias := .Aliases.Table $table.Key -}}
func (s *{{$tAlias.UpSingular}}Setter) Apply(q *dialect.InsertQuery) {
q.AppendHooks(func(ctx context.Context, exec bob.Executor) (context.Context, error) {
return {{$tAlias.UpPlural}}.BeforeInsertHooks.RunHooks(ctx, exec, s)
})

if len(q.Table.Columns) == 0 {
q.Table.Columns = s.SetColumns()
}

q.AppendValues(bob.ExpressionFunc(func(ctx context.Context, w io.Writer, d bob.Dialect, start int) ([]any, error){
vals := make([]bob.Expression, 0, {{len $table.NonGeneratedColumns}})
{{range $index, $column := $table.NonGeneratedColumns -}}
{{$colAlias := $tAlias.Column $column.Name -}}
if !s.{{$colAlias}}.IsUnset() {
vals = append(vals, {{$.Dialect}}.Arg(s.{{$colAlias}}))
}

{{end -}}

return bob.ExpressSlice(ctx, w, d, start, vals, "", ", ", "")
}))
}
{{- end}}

{{define "unique_constraint_error_detection_method" -}}
func (e *UniqueConstraintError) Is(target error) bool {
{{if not (eq $.DriverName "modernc.org/sqlite" "github.com/mattn/go-sqlite3")}}
Expand Down
25 changes: 6 additions & 19 deletions gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,24 +174,15 @@ func generate[T, C, I any](s *State[C], data *TemplateData[T, C, I], goVersion s
// set the package name for this output
data.PkgName = o.PkgName

templates, err := o.initTemplates(s.CustomTemplateFuncs, s.Config.NoTests)
if err != nil {
if err := o.initTemplates(s.CustomTemplateFuncs); err != nil {
return fmt.Errorf("unable to initialize templates: %w", err)
}

tplCount := 0
if o.templates != nil {
tplCount += len(o.templates.Templates())
}
if o.testTemplates != nil {
tplCount += len(o.testTemplates.Templates())
}
if tplCount == 0 {
if o.numTemplates() == 0 {
continue
}

err = o.initOutFolders(templates, s.Config.Wipe)
if err != nil {
if err := o.initOutFolders(s.Config.Wipe); err != nil {
return fmt.Errorf("unable to initialize the output folders: %w", err)
}

Expand All @@ -209,23 +200,19 @@ func generate[T, C, I any](s *State[C], data *TemplateData[T, C, I], goVersion s
}
}

var regularDirExtMap, testDirExtMap dirExtMap
regularDirExtMap = groupTemplates(o.templates)
if !s.Config.NoTests {
testDirExtMap = groupTemplates(o.testTemplates)
}
dirExtMap := groupTemplates(o.tableTemplates)

for _, table := range data.Tables {
data.Table = table

// Generate the regular templates
if err := generateOutput(o, regularDirExtMap, data, goVersion); err != nil {
if err := generateOutput(o, dirExtMap, data, goVersion); err != nil {
return fmt.Errorf("unable to generate output: %w", err)
}

// Generate the test templates
if !s.Config.NoTests {
if err := generateTestOutput(o, testDirExtMap, data, goVersion); err != nil {
if err := generateTestOutput(o, dirExtMap, data, goVersion); err != nil {
return fmt.Errorf("unable to generate test output: %w", err)
}
}
Expand Down
Loading

0 comments on commit 3c5dba8

Please sign in to comment.