From 8d85ea6194272f308fb130c8e47f2efcbf2e565a Mon Sep 17 00:00:00 2001 From: Roel Arents <2691308+roelarents@users.noreply.github.com> Date: Mon, 9 Dec 2024 11:41:49 +0100 Subject: [PATCH] use openapi engine's template functions also in etl (#14) PDOK-17220 --- internal/engine/openapi.go | 2 +- internal/engine/template.go | 2 +- internal/engine/templatefuncs.go | 9 +++++++-- internal/etl/testdata/config.yaml | 2 +- internal/etl/transform/transform.go | 3 ++- 5 files changed, 12 insertions(+), 6 deletions(-) diff --git a/internal/engine/openapi.go b/internal/engine/openapi.go index e92a4d3..6a4d3fb 100644 --- a/internal/engine/openapi.go +++ b/internal/engine/openapi.go @@ -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 { diff --git a/internal/engine/template.go b/internal/engine/template.go index aaf03e4..db30a97 100644 --- a/internal/engine/template.go +++ b/internal/engine/template.go @@ -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] diff --git a/internal/engine/templatefuncs.go b/internal/engine/templatefuncs.go index 0521746..fd84d58 100644 --- a/internal/engine/templatefuncs.go +++ b/internal/engine/templatefuncs.go @@ -19,7 +19,7 @@ import ( ) var ( - globalTemplateFuncs texttemplate.FuncMap + GlobalTemplateFuncs texttemplate.FuncMap linkRegex = regexp.MustCompile(`^https?://\S+$`) ) @@ -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 @@ -122,3 +123,7 @@ func isLink(v any) bool { } return false } + +func firstUpper(s string) string { + return strings.ToUpper(s[0:1]) + s[1:] +} diff --git a/internal/etl/testdata/config.yaml b/internal/etl/testdata/config.yaml index 43df256..f484a2a 100644 --- a/internal/etl/testdata/config.yaml +++ b/internal/etl/testdata/config.yaml @@ -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 }}" diff --git a/internal/etl/transform/transform.go b/internal/etl/transform/transform.go index 2eb6d07..8983e31 100644 --- a/internal/etl/transform/transform.go +++ b/internal/etl/transform/transform.go @@ -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 ) @@ -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 }