Skip to content

Commit

Permalink
Merge pull request #325 from stephenafamo/template-restructure
Browse files Browse the repository at this point in the history
Refactor template parsing and organisation
  • Loading branch information
stephenafamo authored Dec 12, 2024
2 parents 1c4d4a0 + 3bbb0d7 commit 3723387
Show file tree
Hide file tree
Showing 35 changed files with 258 additions and 433 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Collation in `clause.OrderDef` is now a string not an expression and is always quoted
- Calling `UpdateAll`, `DeleteAll` and `ReloadAll` on an empty model slice now returns nil without running any queries.
- Generated files now end with `.bob.go` instead of `.go` and are always cleaned up before generating new files. Singleton templates are now required to have a `.bob.go.tpl` extension.
- The expected structure for templates have been changed:
- Previously, singleton templates should be kept in a `singleton` folder. Now, any template not inside a folder is considered a singleton template.
- Previoulsy, templates in the root folder are merged and run for each table. Now, this will happen to templates in the `table/` folder.
- Previoulsy, the entire file tree and every subdirectory is walked to find templates. Now only templates in the root folder and the `table/` folder are considered.

### Deprecated

Expand Down
20 changes: 20 additions & 0 deletions gen/bobgen-mysql/templates/models/bob_mysql_blocks.bob.go.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{{- define "helpers/where_variables"}}
{{$.Importer.Import (printf "github.com/stephenafamo/bob/dialect/%s/dialect" $.Dialect)}}
var (
SelectWhere = Where[*dialect.SelectQuery]()
UpdateWhere = Where[*dialect.UpdateQuery]()
DeleteWhere = Where[*dialect.DeleteQuery]()
)
{{- end -}}

{{define "unique_constraint_error_detection_method" -}}
{{$.Importer.Import "strings"}}
{{$.Importer.Import "mysqlDriver" "github.com/go-sql-driver/mysql"}}
func (e *UniqueConstraintError) Is(target error) bool {
err, ok := target.(*mysqlDriver.MySQLError)
if !ok {
return false
}
return err.Number == 1062 && strings.Contains(err.Message, e.s)
}
{{end -}}

This file was deleted.

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}}
32 changes: 32 additions & 0 deletions gen/bobgen-sqlite/templates/models/bob_sqlite_blocks.bob.go.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{{- define "helpers/join_variables"}}
var (
SelectJoins = getJoins[*dialect.SelectQuery]
UpdateJoins = getJoins[*dialect.UpdateQuery]
)
{{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")}}
return false
{{else}}
{{$errType := ""}}
{{$codeGetter := ""}}
{{$.Importer.Import "strings"}}
{{if eq $.DriverName "modernc.org/sqlite"}}
{{$.Importer.Import "sqliteDriver" $.DriverName}}
{{$errType = "*sqliteDriver.Error"}}
{{$codeGetter = "Code()"}}
{{else}}
{{$.Importer.Import $.DriverName}}
{{$errType = "sqlite3.Error"}}
{{$codeGetter = "ExtendedCode"}}
{{end}}
err, ok := target.({{$errType}})
if !ok {
return false
}
return err.{{$codeGetter}} == 2067 && strings.Contains(err.Error(), e.s)
{{end}}
}
{{end -}}
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
{{- define "helpers/join_variables"}}
var (
SelectJoins = getJoins[*dialect.SelectQuery]
UpdateJoins = getJoins[*dialect.UpdateQuery]
)
{{end -}}

{{define "setter_insert_mod" -}}
{{$.Importer.Import "io"}}
{{$.Importer.Import "github.com/stephenafamo/bob"}}
Expand Down Expand Up @@ -33,29 +26,3 @@ func (s *{{$tAlias.UpSingular}}Setter) Apply(q *dialect.InsertQuery) {
}))
}
{{- 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")}}
return false
{{else}}
{{$errType := ""}}
{{$codeGetter := ""}}
{{$.Importer.Import "strings"}}
{{if eq $.DriverName "modernc.org/sqlite"}}
{{$.Importer.Import "sqliteDriver" $.DriverName}}
{{$errType = "*sqliteDriver.Error"}}
{{$codeGetter = "Code()"}}
{{else}}
{{$.Importer.Import $.DriverName}}
{{$errType = "sqlite3.Error"}}
{{$codeGetter = "ExtendedCode"}}
{{end}}
err, ok := target.({{$errType}})
if !ok {
return false
}
return err.{{$codeGetter}} == 2067 && strings.Contains(err.Error(), e.s)
{{end}}
}
{{end -}}
38 changes: 6 additions & 32 deletions gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,61 +174,35 @@ 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)
}

// assign reusable scratch buffers to provided Output
o.templateByteBuffer = templateByteBuffer
o.templateHeaderByteBuffer = templateHeaderByteBuffer

if err := generateSingletonOutput(o, data, goVersion); err != nil {
if err := generateSingletonOutput(o, data, goVersion, s.Config.NoTests); err != nil {
return fmt.Errorf("singleton template output: %w", err)
}

if !s.Config.NoTests {
if err := generateSingletonTestOutput(o, data, goVersion); err != nil {
return fmt.Errorf("unable to generate singleton test template output: %w", err)
}
}

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, s.Config.NoTests); 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 {
return fmt.Errorf("unable to generate test output: %w", err)
}
}
}
}

Expand Down
Loading

0 comments on commit 3723387

Please sign in to comment.