Add inline templates to the CLI and library #4

Merged
lilleman merged 23 commits from cli-inline-template into main 2026-09-03 21:20:26 +02:00
3 changed files with 8 additions and 5 deletions
Showing only changes of commit 44601c90ab - Show all commits
+1 -1
View File
@@ -133,7 +133,7 @@ func transformArg(fields map[string]node, a []string) error {
_, _, err := refShape(leaf) _, _, err := refShape(leaf)
return err return err
} }
return checkArm(leaf, fields) return checkArm(leaf, fields, false)
} }
func transformOperand(a []string) []string { func transformOperand(a []string) []string {
+2 -1
View File
@@ -31,7 +31,8 @@ An argument containing a { token, or a JSON object, array or string, is a
template; any other argument is a path (a path never contains a brace, a bracket template; any other argument is a path (a path never contains a brace, a bracket
or a quote). Templates reach the data by reference from the root — or a quote). Templates reach the data by reference from the root —
{/sv_SE.person.last} — whether the data is shipped or layered with --data-path. An {/sv_SE.person.last} — whether the data is shipped or layered with --data-path. An
argument carrying one of those characters but no valid JSON names neither. argument carrying a bracket, a closing brace or a quote but no valid JSON names
neither.
-d, --data-path D a data directory to layer over the shipped data (repeatable; last wins on a clash) -d, --data-path D a data directory to layer over the shipped data (repeatable; last wins on a clash)
-h, --help print this help, then exit -h, --help print this help, then exit
+5 -3
View File
@@ -140,7 +140,7 @@ func checkTokens(format string, fields map[string]node) error {
} }
continue // its target is checked at New (see linkRefs) continue // its target is checked at New (see linkRefs)
} }
if err := checkArm(name, fields); err != nil { if err := checkArm(name, fields, len(names) == 1); err != nil {
return fmt.Errorf("token {%s}: %w", t.body, err) return fmt.Errorf("token {%s}: %w", t.body, err)
} }
} }
@@ -151,7 +151,9 @@ func checkTokens(format string, fields map[string]node) error {
} }
// checkArm validates one sibling name or path against a template's fields. // checkArm validates one sibling name or path against a template's fields.
func checkArm(name string, fields map[string]node) error { // wholeToken says the name is the token's entire body, so {/name} would render
// the same value and can be offered as the reference spelling.
func checkArm(name string, fields map[string]node, wholeToken bool) error {
a := splitArm(name, nil) a := splitArm(name, nil)
if err := checkSegments(a); err != nil { if err := checkSegments(a); err != nil {
return err return err
@@ -166,7 +168,7 @@ func checkArm(name string, fields map[string]node) error {
} }
if len(fields) == 0 { if len(fields) == 0 {
hint := "" hint := ""
if hintableRef(name) { if wholeToken && hintableRef(name) {
hint = fmt.Sprintf(" — write {/%s} to reference the data", name) hint = fmt.Sprintf(" — write {/%s} to reference the data", name)
} }
return fmt.Errorf("no field %q; a token names a sibling field, and this template has none%s", a.key, hint) return fmt.Errorf("no field %q; a token names a sibling field, and this template has none%s", a.key, hint)