Add repeat/separator templates; validate at compile time; support Go 1.22+ via GO_VERSION

This commit is contained in:
lilleman
2026-06-08 21:11:58 +02:00
parent 72154502d7
commit 3e1cd3ee2a
8 changed files with 241 additions and 139 deletions
+25 -1
View File
@@ -23,7 +23,7 @@ format control we needed.
go get github.com/Timewave-AB/fakes
```
Requires Go 1.26+.
Requires Go 1.22+ (for `math/rand/v2`).
## Usage
@@ -119,6 +119,22 @@ within a choice:
]
```
Only template (object) nodes carry `weight` — a bare string or nested array in a
choice always counts as `1`. Weights are checked when you create the faker: a
negative, non-numeric, or all-zero set is rejected at `New`, so a typo fails
fast instead of silently skewing output.
**Repeat.** A template node may carry a `repeat` (default `1`) to render its
`format` that many times — each render an independent pick — joined by
`separator` (default `""`):
```json
{ "format": "{word}", "repeat": 3, "separator": " ", "word": ["foo", "bar", "baz"] }
```
This yields e.g. `bar foo baz`. `repeat` must be a positive integer and
`separator` a string, both checked at `New`.
**Format string.** Every character is literal except:
| Token | Expands to |
@@ -193,6 +209,14 @@ docker compose run --rm --user "$(id -u):$(id -g)" tidy # go mod tidy
`docker build .` runs `go vet` and the tests, so it works as a CI gate too.
Tests run against the latest Go by default. Set `GO_VERSION` to check the lowest
supported version too:
```sh
GO_VERSION=1.22 docker compose run --rm test # lowest supported
docker compose run --rm test # latest
```
## Layout
```