Skip to content

Commit

Permalink
fix(generate): Adding helpers to be generated when generating templat…
Browse files Browse the repository at this point in the history
…es (#53)

* Adding helper templates to be rendered

* Updating examples
  • Loading branch information
Jacobbrewer1 authored Dec 3, 2024
1 parent 555a73f commit d459496
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 204 deletions.
122 changes: 0 additions & 122 deletions example/models/helpers.go

This file was deleted.

15 changes: 0 additions & 15 deletions example/models/metrics.go

This file was deleted.

42 changes: 0 additions & 42 deletions helpers/db.go

This file was deleted.

12 changes: 0 additions & 12 deletions helpers/errors.go

This file was deleted.

94 changes: 93 additions & 1 deletion pkg/generation/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"text/template"

"github.com/Masterminds/sprig"
Expand Down Expand Up @@ -53,6 +55,91 @@ func RenderWithTemplates(fs embed.FS, tables []*models.Table, outputLoc string,
}
}

if err := renderHelpers(fs, outputLoc, fileExtensionPrefix); err != nil {
return fmt.Errorf("error rendering helpers: %w", err)
}

return nil
}

func renderHelpers(fs embed.FS, outputLoc string, fileExtensionPrefix string) error {
wg := new(sync.WaitGroup)
errs := new(sync.Map)

wg.Add(1)
go func() {
defer wg.Done()
tmpl, err := template.New("db.tmpl").Funcs(sprig.TxtFuncMap()).Funcs(Helpers).ParseFS(fs, "templates/db.tmpl")
if err != nil {
errs.Store("error parsing db template", err)
return
}

if err := generate(&templateInfo{
OutputDir: outputLoc,
}, tmpl, outputLoc, fileExtensionPrefix); err != nil {
errs.Store("error generating db template", err)
}
}()

wg.Add(1)
go func() {
defer wg.Done()
tmpl, err := template.New("errors.tmpl").Funcs(sprig.TxtFuncMap()).Funcs(Helpers).ParseFS(fs, "templates/errors.tmpl")
if err != nil {
errs.Store("error parsing errors template", err)
return
}

if err := generate(&templateInfo{
OutputDir: outputLoc,
}, tmpl, outputLoc, fileExtensionPrefix); err != nil {
errs.Store("error generating helpers template", err)
}
}()

wg.Add(1)
go func() {
defer wg.Done()
tmpl, err := template.New("helpers.tmpl").Funcs(sprig.TxtFuncMap()).Funcs(Helpers).ParseFS(fs, "templates/helpers.tmpl")
if err != nil {
errs.Store("error parsing helpers template", err)
return
}

if err := generate(&templateInfo{
OutputDir: outputLoc,
}, tmpl, outputLoc, fileExtensionPrefix); err != nil {
errs.Store("error generating helpers template", err)
}
}()

wg.Add(1)
go func() {
defer wg.Done()
tmpl, err := template.New("metrics.tmpl").Funcs(sprig.TxtFuncMap()).Funcs(Helpers).ParseFS(fs, "templates/metrics.tmpl")
if err != nil {
errs.Store("error parsing metrics template", err)
return
}

if err := generate(&templateInfo{
OutputDir: outputLoc,
}, tmpl, outputLoc, fileExtensionPrefix); err != nil {
errs.Store("error generating metrics template", err)
}
}()

wg.Wait()

errs.Range(func(key, value interface{}) bool {
if value != nil {
slog.Error(key.(string), slog.String("error", value.(error).Error()))
}

return true
})

return nil
}

Expand All @@ -66,7 +153,12 @@ func generate(t *templateInfo, tmpl *template.Template, outputLoc string, fileEx
ext = fileExtensionPrefix + ext
}

fn := filepath.Join(outputLoc, xstrings.ToSnakeCase(t.Table.Name)+ext)
filename := strings.ReplaceAll(tmpl.Name(), ".tmpl", "")
if t.Table != nil {
filename = xstrings.ToSnakeCase(t.Table.Name)
}

fn := filepath.Join(outputLoc, filename+ext)
if err := os.MkdirAll(filepath.Dir(fn), 0750); err != nil {
return err
}
Expand Down
8 changes: 5 additions & 3 deletions example/models/db.go → templates/db.tmpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
package models
// Package models contains the database interaction model code
//
// GENERATED BY GOSCHEMA. DO NOT EDIT.
package {{ .OutputDir | base | snakecase }}

import (
"database/sql"
Expand All @@ -20,7 +23,6 @@ type DB interface {
Exec(string, ...any) (sql.Result, error)
Query(string, ...any) (*sql.Rows, error)
QueryRow(string, ...any) *sql.Row
// Additional sqlx methods we like
Get(dest any, query string, args ...interface{}) error
Select(dest any, query string, args ...interface{}) error
}
Expand All @@ -40,4 +42,4 @@ var DBLog = func(string, ...any) {}
// XOLog is a compat shim for DBLog
var XOLog = func(msg string, args ...any) {
DBLog(msg, args...)
}
}
7 changes: 5 additions & 2 deletions example/models/errors.go → templates/errors.tmpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
package models
// Package models contains the database interaction model code
//
// GENERATED BY GOSCHEMA. DO NOT EDIT.
package {{ .OutputDir | base | snakecase }}

import "errors"

Expand All @@ -9,4 +12,4 @@ var ErrNoAffectedRows = errors.New("no affected rows")
var ErrDuplicate = errors.New("duplicate entry")

// ErrConstraintViolation is returned if a constraint is violated
var ErrConstraintViolation = errors.New("constraint violation")
var ErrConstraintViolation = errors.New("constraint violation")
5 changes: 4 additions & 1 deletion helpers/helpers.go → templates/helpers.tmpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
package helpers
// Package models contains the database interaction model code
//
// GENERATED BY GOSCHEMA. DO NOT EDIT.
package {{ .OutputDir | base | snakecase }}

import (
"fmt"
Expand Down
10 changes: 4 additions & 6 deletions helpers/metrics.go → templates/metrics.tmpl
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package helpers

import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
// Package models contains the database interaction model code
//
// GENERATED BY GOSCHEMA. DO NOT EDIT.
package {{ .OutputDir | base | snakecase }}

// DatabaseLatency is the duration of database queries.
var DatabaseLatency = promauto.NewHistogramVec(
Expand Down

0 comments on commit d459496

Please sign in to comment.