From 5bcdf93f22bda6b3d4244c14fb7c9d84c4714227 Mon Sep 17 00:00:00 2001 From: Shreyas Goenka Date: Mon, 24 Jul 2023 01:05:47 +0200 Subject: [PATCH 1/2] Add regexp compile helper function for templates --- libs/template/helpers.go | 5 ++++ libs/template/helpers_test.go | 25 +++++++++++++++++++ .../regexp-compile/template/hello.tmpl | 5 ++++ 3 files changed, 35 insertions(+) create mode 100644 libs/template/helpers_test.go create mode 100644 libs/template/testdata/regexp-compile/template/hello.tmpl diff --git a/libs/template/helpers.go b/libs/template/helpers.go index 271fd539bc4..ec05888a45a 100644 --- a/libs/template/helpers.go +++ b/libs/template/helpers.go @@ -2,6 +2,7 @@ package template import ( "fmt" + "regexp" "text/template" ) @@ -17,4 +18,8 @@ var helperFuncs = template.FuncMap{ "fail": func(format string, args ...any) (any, error) { return nil, ErrFail{fmt.Sprintf(format, args...)} }, + // Alias for https://pkg.go.dev/regexp#Compile. Allows usage of all methods of regexp.Regexp + "regexpCompile": func(expr string) (*regexp.Regexp, error) { + return regexp.Compile(expr) + }, } diff --git a/libs/template/helpers_test.go b/libs/template/helpers_test.go new file mode 100644 index 00000000000..fbb66ae2ac9 --- /dev/null +++ b/libs/template/helpers_test.go @@ -0,0 +1,25 @@ +package template + +import ( + "context" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestTemplateRegexpCompileFunction(t *testing.T) { + ctx := context.Background() + tmpDir := t.TempDir() + + r, err := newRenderer(ctx, nil, "./testdata/regexp-compile/template", "./testdata/regexp-compile/library", tmpDir) + require.NoError(t, err) + + err = r.walk() + assert.NoError(t, err) + + assert.Len(t, r.files, 1) + content := string(r.files[0].content) + assert.Contains(t, content, "0:food") + assert.Contains(t, content, "1:fool") +} diff --git a/libs/template/testdata/regexp-compile/template/hello.tmpl b/libs/template/testdata/regexp-compile/template/hello.tmpl new file mode 100644 index 00000000000..47f48dffc41 --- /dev/null +++ b/libs/template/testdata/regexp-compile/template/hello.tmpl @@ -0,0 +1,5 @@ +{{with (regexpCompile "foo.?")}} +{{range $index, $element := (.FindAllString "seafood fool" -1) }} +{{print $index ":" $element}} +{{end}} +{{end}} From 4189bf328d22c0ac16bcf9ab722e3eeb67ad152a Mon Sep 17 00:00:00 2001 From: Shreyas Goenka Date: Tue, 25 Jul 2023 13:29:24 +0200 Subject: [PATCH 2/2] address comment --- libs/template/helpers.go | 2 +- libs/template/testdata/regexp-compile/template/hello.tmpl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/libs/template/helpers.go b/libs/template/helpers.go index ec05888a45a..342b3811df3 100644 --- a/libs/template/helpers.go +++ b/libs/template/helpers.go @@ -19,7 +19,7 @@ var helperFuncs = template.FuncMap{ return nil, ErrFail{fmt.Sprintf(format, args...)} }, // Alias for https://pkg.go.dev/regexp#Compile. Allows usage of all methods of regexp.Regexp - "regexpCompile": func(expr string) (*regexp.Regexp, error) { + "regexp": func(expr string) (*regexp.Regexp, error) { return regexp.Compile(expr) }, } diff --git a/libs/template/testdata/regexp-compile/template/hello.tmpl b/libs/template/testdata/regexp-compile/template/hello.tmpl index 47f48dffc41..5ea55d79569 100644 --- a/libs/template/testdata/regexp-compile/template/hello.tmpl +++ b/libs/template/testdata/regexp-compile/template/hello.tmpl @@ -1,4 +1,4 @@ -{{with (regexpCompile "foo.?")}} +{{with (regexp "foo.?")}} {{range $index, $element := (.FindAllString "seafood fool" -1) }} {{print $index ":" $element}} {{end}}