Skip to content

Commit

Permalink
use openapi engine's template functions also in etl (#14)
Browse files Browse the repository at this point in the history
PDOK-17220
  • Loading branch information
roelarents authored Dec 9, 2024
1 parent 4ce641d commit 8d85ea6
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 6 deletions.
2 changes: 1 addition & 1 deletion internal/engine/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func newOpenAPIRouter(doc *openapi3.T) routers.Router {
func renderOpenAPITemplate(config *gomagpieconfig.Config, fileName string, params any) []byte {
file := filepath.Clean(fileName)
files := []string{problems, file} // add problems template too since it's an "include" template
parsed := texttemplate.Must(texttemplate.New(filepath.Base(file)).Funcs(globalTemplateFuncs).ParseFiles(files...))
parsed := texttemplate.Must(texttemplate.New(filepath.Base(file)).Funcs(GlobalTemplateFuncs).ParseFiles(files...))

var rendered bytes.Buffer
if err := parsed.Execute(&rendered, &TemplateData{Config: config, Params: params}); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/engine/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func (t *Templates) renderNonHTMLTemplate(parsed *texttemplate.Template, params
}

func (t *Templates) createTemplateFuncs(lang language.Tag) map[string]any {
return combineFuncMaps(globalTemplateFuncs, texttemplate.FuncMap{
return combineFuncMaps(GlobalTemplateFuncs, texttemplate.FuncMap{
// create func just-in-time based on TemplateKey
"i18n": func(messageID string) htmltemplate.HTML {
localizer := t.localizers[lang]
Expand Down
9 changes: 7 additions & 2 deletions internal/engine/templatefuncs.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
)

var (
globalTemplateFuncs texttemplate.FuncMap
GlobalTemplateFuncs texttemplate.FuncMap
linkRegex = regexp.MustCompile(`^https?://\S+$`)
)

Expand All @@ -33,9 +33,10 @@ func init() {
"bytessize": bytesSize,
"isdate": isDate,
"islink": isLink,
"firstupper": firstUpper,
}
sprigFuncs := sprig.FuncMap() // we also support https://github.com/go-task/slim-sprig functions
globalTemplateFuncs = combineFuncMaps(customFuncs, sprigFuncs)
GlobalTemplateFuncs = combineFuncMaps(customFuncs, sprigFuncs)
}

// combine given FuncMaps
Expand Down Expand Up @@ -122,3 +123,7 @@ func isLink(v any) bool {
}
return false
}

func firstUpper(s string) string {
return strings.ToUpper(s[0:1]) + s[1:]
}
2 changes: 1 addition & 1 deletion internal/etl/testdata/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ collections:
- component_thoroughfarename
- component_postaldescriptor
- component_addressareaname
displayNameTemplate: "{{ .component_thoroughfarename }} - {{ .component_addressareaname }}"
displayNameTemplate: "{{ .component_thoroughfarename }} - {{ .component_addressareaname | firstupper }}"
etl:
suggestTemplates:
- "{{ .component_thoroughfarename }} {{ .component_addressareaname }}"
Expand Down
3 changes: 2 additions & 1 deletion internal/etl/transform/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"text/template"

"github.com/PDOK/gomagpie/config"
"github.com/PDOK/gomagpie/internal/engine"
"github.com/go-spatial/geom"
pggeom "github.com/twpayne/go-geom" // this lib has a large overlap with github.com/go-spatial/geom but we need it to integrate with postgres
)
Expand Down Expand Up @@ -76,7 +77,7 @@ func (t Transformer) Transform(records []RawRecord, collection config.GeoSpatial
}

func (t Transformer) renderTemplate(templateFromConfig string, fieldValuesByName map[string]any) (string, error) {
parsedTemplate, err := template.New("").Parse(templateFromConfig)
parsedTemplate, err := template.New("").Funcs(engine.GlobalTemplateFuncs).Parse(templateFromConfig)
if err != nil {
return "", err
}
Expand Down

0 comments on commit 8d85ea6

Please sign in to comment.