From 9baa77c752738aedd706206f7d77c66204cf67f6 Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Thu, 8 Oct 2026 12:28:52 +0200 Subject: [PATCH] Drop the CLI's no-data hint, keep the number underflow rule in grammar/number.go, and name the JSON a template may be --- README.md | 2 +- cmd/fejkdata/main.go | 16 ++-------------- internal/grammar/calc.go | 8 -------- internal/grammar/number.go | 10 ++++++++++ 4 files changed, 13 insertions(+), 23 deletions(-) create mode 100644 internal/grammar/number.go diff --git a/README.md b/README.md index 67c891c..8b43c3a 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ born {date(1990-01-01,2010-12-31,'2006-01-02')} EOF ``` -fejkdata renders the template on its stdin: a format string, or any JSON value. Its `{…}` tokens reach the data by reference from the root, +fejkdata renders the template on its stdin: a format string, or a JSON object, array, string or `null`. Its `{…}` tokens reach the data by reference from the root, `{/sv_SE.person.last}`, so shipped and `--data-path` categories are alike available, and everything outside them prints as written. A reference names a category, or a field inside one: each dot segment descends one level — folders, then the category (a JSON diff --git a/cmd/fejkdata/main.go b/cmd/fejkdata/main.go index 3726e3f..57522b2 100644 --- a/cmd/fejkdata/main.go +++ b/cmd/fejkdata/main.go @@ -35,7 +35,7 @@ fejkdata <<'EOF' born {date(1990-01-01,2010-12-31,'2006-01-02')} EOF -A template is a format string, or any JSON value. Its {…} tokens +A template is a format string, or a JSON object, array, string or null. Its {…} tokens reach the data by reference from the root, {/sv_SE.person.last}, whether the data is shipped or layered with --data-path. A reference names a category or a dotted path into one ({/sv_SE.person.last}), a table's row by key or name @@ -606,7 +606,7 @@ func run(args []string, stdin io.Reader, stdout, stderr io.Writer) int { return 0 } if err := in.write(f, src, stdout); err != nil { - return fail(stderr, in.noDataCause(err)) + return fail(stderr, err) } return 0 } @@ -622,18 +622,6 @@ func readStdin(stdin io.Reader) (input, error) { return parseInput(string(raw)), nil } -// noDataCause adds the flags that left the generator without data to an error saying none is -// loaded. -func (in invocation) noDataCause(err error) error { - if !in.noShipped || len(in.dirs) > 0 || !strings.HasSuffix(err.Error(), "no data is loaded") { - return err - } - if te, isInput := err.(inputError); isInput { - return inputError{fmt.Errorf("%w: --no-shipped-data and no --data-path", te.error)} - } - return fmt.Errorf("%w: --no-shipped-data and no --data-path", err) -} - func fail(stderr io.Writer, err error) int { var te inputError if errors.As(err, &te) { diff --git a/internal/grammar/calc.go b/internal/grammar/calc.go index 341936b..8ecca45 100644 --- a/internal/grammar/calc.go +++ b/internal/grammar/calc.go @@ -4,7 +4,6 @@ import ( "fmt" "slices" "strconv" - "strings" "unicode" ) @@ -178,10 +177,3 @@ func (p *calcParser) ident() (CalcNode, error) { } return CalcVar{name, at}, nil } - -// Underflows reports a number written as text that reads as f = 0 though a digit before its -// exponent is not 0, as 1e-400 does: no float64 holds the number written. -func Underflows(text string, f float64) bool { - mantissa, _, _ := strings.Cut(strings.ToLower(text), "e") - return f == 0 && strings.ContainsAny(mantissa, "123456789") -} diff --git a/internal/grammar/number.go b/internal/grammar/number.go new file mode 100644 index 0000000..9650e5f --- /dev/null +++ b/internal/grammar/number.go @@ -0,0 +1,10 @@ +package grammar + +import "strings" + +// Underflows reports a number written as text that reads as f = 0 though a digit before its +// exponent is not 0, as 1e-400 does: no float64 holds the number written. +func Underflows(text string, f float64) bool { + mantissa, _, _ := strings.Cut(strings.ToLower(text), "e") + return f == 0 && strings.ContainsAny(mantissa, "123456789") +}