From 2ad9a2cdaee0124d93387a5820ae0422d19ca3ec Mon Sep 17 00:00:00 2001 From: kozmod Date: Thu, 16 Feb 2023 15:37:22 +0300 Subject: [PATCH] [#61] add `{{ random.ASCII 8 }}` for templates --- Readme.md | 12 +++++++----- internal/entity/entity.go | 1 + internal/entity/entity_test.go | 11 +++++++++++ internal/entity/rand.go | 24 ++++++++++++++++++++++++ 4 files changed, 43 insertions(+), 5 deletions(-) diff --git a/Readme.md b/Readme.md index 3f25768..6f4dfb7 100644 --- a/Readme.md +++ b/Readme.md @@ -360,10 +360,11 @@ dirs: #### Custom template functions -| Function | args | Description | -|:------------------|:------------:|:------------------------------------------------------------------------------| -| `random.Alpha` | length `int` | Generates a random alphabetical `(A-Z, a-z)` string of a desired length. | -| `random.AlphaNum` | length `int` | Generates a random alphanumeric `(0-9, A-Z, a-z)` string of a desired length. | +| Function | args | Description | +|:------------------|:------------:|:----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| `random.Alpha` | length `int` | Generates a random alphabetical `(A-Z, a-z)` string of a desired length. | +| `random.AlphaNum` | length `int` | Generates a random alphanumeric `(0-9, A-Z, a-z)` string of a desired length. | +| `random.ASCII` | length `int` | Generates a random string of a desired length, containing the set of printable characters from the 7-bit ASCII set. This includes space (’ ‘), but no other whitespace character. | Custom template functions adds the elements of the argument map to the template's [function map]](https://pkg.go.dev/text/template#hdr-Functions). @@ -495,6 +496,7 @@ out: 2 directories, 6 files ``` + `cmd` action maintains "short" declaration syntax ```yaml @@ -504,6 +506,7 @@ cmd: - pwd - ls -a ``` + ```console % progen -v -dr 2023-02-15 17:56:58 INFO application working directory: /Users/user_1/GoProjects/progen @@ -512,7 +515,6 @@ cmd: 2023-02-15 17:56:58 INFO execute [dir: .]: ls -a ``` - #### 'Pipe mode' The `cmd.pipe` option set "pipe mode" for `cmd.exec` section: diff --git a/internal/entity/entity.go b/internal/entity/entity.go index f181ac7..b310202 100644 --- a/internal/entity/entity.go +++ b/internal/entity/entity.go @@ -39,6 +39,7 @@ const ( EqualsSign = "=" LessThan = "<" SpacedPipe = " | " + Tilda = "~" NewLine = '\n' ) diff --git a/internal/entity/entity_test.go b/internal/entity/entity_test.go index 33a6694..056b095 100644 --- a/internal/entity/entity_test.go +++ b/internal/entity/entity_test.go @@ -84,6 +84,17 @@ func Test_RandomFn(t *testing.T) { assert.Regexp(t, "^[[:alnum:]]*$", s) }) }) + t.Run("ASCII", func(t *testing.T) { + t.Run("generate_zero", func(t *testing.T) { + s := f.ASCII(0) + assert.Empty(t, s) + }) + t.Run("generate_50", func(t *testing.T) { + s := f.ASCII(50) + assert.Len(t, s, 50) + assert.Regexp(t, "^[[:print:]]*$", s) + }) + }) } func Test_MissingKye(t *testing.T) { diff --git a/internal/entity/rand.go b/internal/entity/rand.go index 614deb0..e3ec1c5 100644 --- a/internal/entity/rand.go +++ b/internal/entity/rand.go @@ -3,6 +3,7 @@ package entity import ( "hash/maphash" "math/rand" + "unicode" ) // Default set, matches "[a-zA-Z0-9_.-]" @@ -16,6 +17,8 @@ const ( ) var ( + _lettersASCII string + mapHashSrc = rand.New(rand.NewSource(int64(new(maphash.Hash).Sum64()))) TemplateFnsMap = map[string]any{ @@ -23,6 +26,21 @@ var ( } ) +func init() { + var ( + lower = int32(Space[0]) + upper = int32(Tilda[0]) + ) + + chars := make([]rune, 0, int(upper)-int(lower)) + for r := lower; r <= upper; r++ { + if unicode.IsGraphic(r) { + chars = append(chars, r) + } + } + _lettersASCII = string(chars) +} + // RandomFn has to generate random string value type RandomFn struct{} @@ -36,6 +54,12 @@ func (RandomFn) AlphaNum(n int) string { return randomString(n, _lettersAlphaNum) } +// ASCII Generates a random string of a desired length, containing the set of printable characters from the 7-bit ASCII set. +// This includes space (’ ‘), but no other whitespace character +func (RandomFn) ASCII(n int) string { + return randomString(n, _lettersASCII) +} + func randomString(n int, set string) string { src := mapHashSrc b := make([]byte, n)