Resolve architect findings: gate [ on json.Valid, fix the root-relative README claim, test isTemplate
Tests / vet + fmt + tests (pull_request) Failing after 16s

This commit is contained in:
2026-09-03 17:40:29 +02:00
parent e08435afa7
commit 031a3ec7d6
3 changed files with 26 additions and 6 deletions
+4 -3
View File
@@ -42,9 +42,10 @@ A path names a category, or a field inside one: each dot segment descends one
level — folders, then the category (a JSON file), then fields. An argument that is level — folders, then the category (a JSON file), then fields. An argument that is
a JSON object or array, or that carries a `{` token, is instead an **inline a JSON object or array, or that carries a `{` token, is instead an **inline
template**: a format string or a JSON value compiled and rendered on the spot. Its template**: a format string or a JSON value compiled and rendered on the spot. Its
tokens reach the data by reference — `{/sv_SE.person.last}` from the root, `{.name}` tokens reach the data by reference from the root `{/sv_SE.person.last}` (or
and `{..name}` relative to it — so `--data-path` categories are available too. A `{.name}`, which means the same here), so shipped and `--data-path` categories are
path never contains a brace, so the two cannot collide (see available — and `{..name}` is rejected, an inline template having no folder to step
up from. A path never contains a brace, so the two cannot collide (see
[Decisions](#decisions)). [Decisions](#decisions)).
| Flag | | | Flag | |
+5 -3
View File
@@ -9,6 +9,7 @@ package main
import ( import (
"bufio" "bufio"
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"io" "io"
@@ -243,13 +244,14 @@ func (in invocation) write(f *fejkdata.Generator, w io.Writer) error {
} }
// isTemplate reports whether an argument is an inline template rather than a // isTemplate reports whether an argument is an inline template rather than a
// path: a JSON object or array, or a format string carrying a { token. A path can // path: a format string carrying a { token (a path can never contain a brace),
// never contain a brace, so the two never collide. // or a JSON object or array. A [ can begin a real category name, so a [
// counts as a template only when the whole argument is valid JSON.
func isTemplate(arg string) bool { func isTemplate(arg string) bool {
if strings.ContainsRune(arg, '{') { if strings.ContainsRune(arg, '{') {
return true return true
} }
return strings.HasPrefix(arg, "[") return strings.HasPrefix(arg, "[") && json.Valid([]byte(arg))
} }
// renderArg renders one positional argument: an inline template, or a path. // renderArg renders one positional argument: an inline template, or a path.
+17
View File
@@ -301,6 +301,23 @@ func TestRunShippedDataByDefault(t *testing.T) {
} }
} }
func TestIsTemplate(t *testing.T) {
for arg, want := range map[string]bool{
"sv_SE.person": false,
"person.last": false,
"[abc]": false, // a [ can open a real category name; not JSON
"[abc].field": false,
"name: {x}": true, // a { token: a path can never carry a brace
`{"format":"x"}`: true,
`["a","b"]`: true, // a JSON array carries no brace
`[1, 2]`: true,
} {
if got := isTemplate(arg); got != want {
t.Errorf("isTemplate(%q) = %v, want %v", arg, got, want)
}
}
}
func TestRunInlineTemplate(t *testing.T) { func TestRunInlineTemplate(t *testing.T) {
code, out, errb := runOut("--seed", "1", "name: {/sv_SE.person.last}") code, out, errb := runOut("--seed", "1", "name: {/sv_SE.person.last}")
if code != 0 || !strings.HasPrefix(out, "name: ") || strings.Contains(out, "{") { if code != 0 || !strings.HasPrefix(out, "name: ") || strings.Contains(out, "{") {