Reserve the reference prefix in authored names, and report one compile error

This commit is contained in:
Mikael Göransson
2026-08-27 23:46:44 +02:00
committed by lilleman-tw
parent af0c99809c
commit e14f3a4145
3 changed files with 17 additions and 5 deletions
+5 -1
View File
@@ -67,6 +67,10 @@ func loadDir(dir string) (*group, error) {
if !strings.HasSuffix(e.Name(), ".json") { if !strings.HasSuffix(e.Name(), ".json") {
continue continue
} }
name := strings.TrimSuffix(e.Name(), ".json")
if isRef(name) {
return nil, fmt.Errorf("%s: category %q starts with %q, which is reserved for {..path} bindings", full, name, refPrefix)
}
b, err := os.ReadFile(full) b, err := os.ReadFile(full)
if err != nil { if err != nil {
return nil, err return nil, err
@@ -79,7 +83,7 @@ func loadDir(dir string) (*group, error) {
if err != nil { if err != nil {
return nil, fmt.Errorf("%s: %w", full, err) return nil, fmt.Errorf("%s: %w", full, err)
} }
g.children[strings.TrimSuffix(e.Name(), ".json")] = n g.children[name] = n
} }
return g, nil return g, nil
} }
+11 -2
View File
@@ -3,6 +3,7 @@ package fakes
import ( import (
"fmt" "fmt"
"math" "math"
"sort"
) )
// node is a compiled template element: literal, choice, or template. Compiling // node is a compiled template element: literal, choice, or template. Compiling
@@ -106,11 +107,19 @@ func compileTemplate(m map[string]any) (node, error) {
} }
} }
t := &template{format: format, fields: make(map[string]node, len(m)), repeat: repeat, separator: sep} t := &template{format: format, fields: make(map[string]node, len(m)), repeat: repeat, separator: sep}
for k, v := range m { keys := make([]string, 0, len(m))
for k := range m {
keys = append(keys, k)
}
sort.Strings(keys) // so which of several bad fields is reported does not vary
for _, k := range keys {
if k == "format" || k == "weight" || k == "repeat" || k == "separator" { if k == "format" || k == "weight" || k == "repeat" || k == "separator" {
continue continue
} }
n, err := compile(v) if isRef(k) {
return nil, fmt.Errorf("field %q starts with %q, which is reserved for {..path} bindings", k, refPrefix)
}
n, err := compile(m[k])
if err != nil { if err != nil {
return nil, fmt.Errorf("field %q: %w", k, err) return nil, fmt.Errorf("field %q: %w", k, err)
} }
+1 -2
View File
@@ -38,8 +38,7 @@ func linkRefs(root map[string]node) error {
// walkNodes calls fn once per contained node, passing the dot path that reaches it, // walkNodes calls fn once per contained node, passing the dot path that reaches it,
// visiting keys in sorted order so which of several broken nodes gets reported does // visiting keys in sorted order so which of several broken nodes gets reported does
// not depend on map iteration. fn runs before a node's children, so a binding it // not depend on map iteration.
// adds to a template's fields is walked too.
func walkNodes(root map[string]node, fn func(path string, n node) error) error { func walkNodes(root map[string]node, fn func(path string, n node) error) error {
seen := map[node]bool{} seen := map[node]bool{}
var visit func(string, node) error var visit func(string, node) error