From 1c41c133ca90e0b62e7012b0d09eb109e459f6ab Mon Sep 17 00:00:00 2001 From: Arpit Jasapara Date: Wed, 5 Jun 2024 01:29:30 -0700 Subject: [PATCH 1/3] Add randIntn function --- libs/template/helpers.go | 5 +++++ libs/template/helpers_test.go | 19 +++++++++++++++++++ .../testdata/randintn/template/hello.tmpl | 1 + 3 files changed, 25 insertions(+) create mode 100644 libs/template/testdata/randintn/template/hello.tmpl diff --git a/libs/template/helpers.go b/libs/template/helpers.go index d15a801d69..578e54bdce 100644 --- a/libs/template/helpers.go +++ b/libs/template/helpers.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "math/rand" "net/url" "os" "regexp" @@ -46,6 +47,10 @@ func loadHelpers(ctx context.Context) template.FuncMap { "regexp": func(expr string) (*regexp.Regexp, error) { return regexp.Compile(expr) }, + // Alias for https://pkg.go.dev/math/rand#Intn. Returns, as an int, a non-negative pseudo-random number in the half-open interval [0,n). + "randIntn": func(n int) int { + return rand.Intn(n) + }, // A key value pair. This is used with the map function to generate maps // to use inside a template "pair": func(k string, v any) pair { diff --git a/libs/template/helpers_test.go b/libs/template/helpers_test.go index a07b26f817..88296025ef 100644 --- a/libs/template/helpers_test.go +++ b/libs/template/helpers_test.go @@ -3,6 +3,7 @@ package template import ( "context" "os" + "strconv" "strings" "testing" @@ -50,6 +51,24 @@ func TestTemplateRegexpCompileFunction(t *testing.T) { assert.Contains(t, content, "1:fool") } +func TestTemplateRandIntFunction(t *testing.T) { + ctx := context.Background() + tmpDir := t.TempDir() + + ctx = root.SetWorkspaceClient(ctx, nil) + helpers := loadHelpers(ctx) + r, err := newRenderer(ctx, nil, helpers, "./testdata/randintn/template", "./testdata/randintn/library", tmpDir) + require.NoError(t, err) + + err = r.walk() + assert.NoError(t, err) + + assert.Len(t, r.files, 1) + randInt, err := strconv.Atoi(string(r.files[0].(*inMemoryFile).content)) + assert.Less(t, randInt, 10) + assert.Empty(t, err) +} + func TestTemplateUrlFunction(t *testing.T) { ctx := context.Background() tmpDir := t.TempDir() diff --git a/libs/template/testdata/randintn/template/hello.tmpl b/libs/template/testdata/randintn/template/hello.tmpl new file mode 100644 index 0000000000..e0f0ae2e70 --- /dev/null +++ b/libs/template/testdata/randintn/template/hello.tmpl @@ -0,0 +1 @@ +{{print (randIntn 10)}} From 2418e8dccca0acef1e8aa168398872ed52993360 Mon Sep 17 00:00:00 2001 From: Arpit Jasapara Date: Wed, 5 Jun 2024 01:41:01 -0700 Subject: [PATCH 2/3] remove whitespace --- libs/template/helpers_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/template/helpers_test.go b/libs/template/helpers_test.go index 88296025ef..8f1a55c65f 100644 --- a/libs/template/helpers_test.go +++ b/libs/template/helpers_test.go @@ -64,7 +64,7 @@ func TestTemplateRandIntFunction(t *testing.T) { assert.NoError(t, err) assert.Len(t, r.files, 1) - randInt, err := strconv.Atoi(string(r.files[0].(*inMemoryFile).content)) + randInt, err := strconv.Atoi(strings.TrimSpace(string(r.files[0].(*inMemoryFile).content))) assert.Less(t, randInt, 10) assert.Empty(t, err) } From 7326135f00ac05c8dec3cc2d8d29845786be6b19 Mon Sep 17 00:00:00 2001 From: Arpit Jasapara Date: Wed, 5 Jun 2024 23:59:29 -0700 Subject: [PATCH 3/3] rename --- libs/template/helpers.go | 2 +- libs/template/helpers_test.go | 2 +- libs/template/testdata/randintn/template/hello.tmpl | 1 - libs/template/testdata/random-int/template/hello.tmpl | 1 + 4 files changed, 3 insertions(+), 3 deletions(-) delete mode 100644 libs/template/testdata/randintn/template/hello.tmpl create mode 100644 libs/template/testdata/random-int/template/hello.tmpl diff --git a/libs/template/helpers.go b/libs/template/helpers.go index 578e54bdce..b3dea329e2 100644 --- a/libs/template/helpers.go +++ b/libs/template/helpers.go @@ -48,7 +48,7 @@ func loadHelpers(ctx context.Context) template.FuncMap { return regexp.Compile(expr) }, // Alias for https://pkg.go.dev/math/rand#Intn. Returns, as an int, a non-negative pseudo-random number in the half-open interval [0,n). - "randIntn": func(n int) int { + "random_int": func(n int) int { return rand.Intn(n) }, // A key value pair. This is used with the map function to generate maps diff --git a/libs/template/helpers_test.go b/libs/template/helpers_test.go index 8f1a55c65f..c0848c8d01 100644 --- a/libs/template/helpers_test.go +++ b/libs/template/helpers_test.go @@ -57,7 +57,7 @@ func TestTemplateRandIntFunction(t *testing.T) { ctx = root.SetWorkspaceClient(ctx, nil) helpers := loadHelpers(ctx) - r, err := newRenderer(ctx, nil, helpers, "./testdata/randintn/template", "./testdata/randintn/library", tmpDir) + r, err := newRenderer(ctx, nil, helpers, "./testdata/random-int/template", "./testdata/random-int/library", tmpDir) require.NoError(t, err) err = r.walk() diff --git a/libs/template/testdata/randintn/template/hello.tmpl b/libs/template/testdata/randintn/template/hello.tmpl deleted file mode 100644 index e0f0ae2e70..0000000000 --- a/libs/template/testdata/randintn/template/hello.tmpl +++ /dev/null @@ -1 +0,0 @@ -{{print (randIntn 10)}} diff --git a/libs/template/testdata/random-int/template/hello.tmpl b/libs/template/testdata/random-int/template/hello.tmpl new file mode 100644 index 0000000000..46dc63fb6c --- /dev/null +++ b/libs/template/testdata/random-int/template/hello.tmpl @@ -0,0 +1 @@ +{{print (random_int 10)}}