From f80c6381c7f8ea7614dc1aebb710c754d45262ee Mon Sep 17 00:00:00 2001 From: Martin Rode Date: Tue, 28 Sep 2021 09:26:21 +0200 Subject: [PATCH] template functions: add sprig template library v3 --- README.md | 2 ++ go.mod | 4 ++++ pkg/lib/template/template_loader.go | 2 ++ 3 files changed, 8 insertions(+) diff --git a/README.md b/README.md index 27030ac..040146c 100644 --- a/README.md +++ b/README.md @@ -1246,6 +1246,8 @@ The content of the request and response file are execatly the same as if you wou # Template functions +> **apitest** supports the [Sprig template](http://masterminds.github.io/sprig/) function library in v3. Internally provided functions like `add` overwrite the `Sprig` function. + As described before, if you use an external file you can make use of so called template functions. What they are and how they work for the apitesting tool is described in the following part. Template Functions are invoked using the tags `{{ }}` and upon returning substitutes the function call with diff --git a/go.mod b/go.mod index 5b0ecd8..06c4081 100644 --- a/go.mod +++ b/go.mod @@ -3,9 +3,13 @@ module github.com/programmfabrik/apitest go 1.13 require ( + github.com/Masterminds/sprig/v3 v3.2.2 github.com/clbanning/mxj v1.8.4 github.com/fsnotify/fsnotify v1.5.1 // indirect + github.com/huandu/xstrings v1.3.2 // indirect + github.com/imdario/mergo v0.3.12 // indirect github.com/mattn/go-sqlite3 v1.14.8 + github.com/mitchellh/copystructure v1.2.0 // indirect github.com/moul/http2curl v1.0.0 github.com/pkg/errors v0.9.1 github.com/programmfabrik/go-test-utils v0.0.0-20191114143449-b8e16b04adb1 diff --git a/pkg/lib/template/template_loader.go b/pkg/lib/template/template_loader.go index 70e8874..3b1a6de 100644 --- a/pkg/lib/template/template_loader.go +++ b/pkg/lib/template/template_loader.go @@ -15,6 +15,7 @@ import ( "strings" "text/template" + "github.com/Masterminds/sprig/v3" "github.com/pkg/errors" "github.com/programmfabrik/apitest/pkg/lib/datastore" "golang.org/x/mod/semver" @@ -445,6 +446,7 @@ func (loader *Loader) Render( tmpl, err := template. New("tmpl"). Delims(loader.Delimiters.Left, loader.Delimiters.Right). + Funcs(sprig.TxtFuncMap()). Funcs(funcMap). Parse(string(tmplBytes)) if err != nil {