Spend the struct cap on embedded structs too, word it for any struct field, and route path advice through the name check
Tests / vet + fmt + tests (pull_request) Successful in 1m2s
Tests / vet + fmt + tests (push) Successful in 6s

This commit was merged in pull request #14.
This commit is contained in:
2026-09-15 17:50:42 +02:00
parent c212c6bef4
commit 3cd3f8f317
4 changed files with 47 additions and 18 deletions
+13 -2
View File
@@ -65,7 +65,7 @@ func IsTemplate(arg string) (bool, error) {
func isTemplate(arg string) (bool, error) {
if strings.ContainsRune(arg, '{') || (isJSONStart(strings.TrimSpace(arg)) && json.Valid([]byte(arg))) {
if path, lone := loneReference(arg); lone {
return false, fmt.Errorf("%s is the path %s written as a template; write %s", arg, path, path)
return false, pathAdvice(path, fmt.Sprintf("%s is the path %s written as a template", arg, path))
}
return true, nil
}
@@ -73,7 +73,7 @@ func isTemplate(arg string) (bool, error) {
return false, fmt.Errorf("%q holds a %q, which no path may, and it is not valid JSON, so it names no template either", arg, arg[i:i+1])
}
if path := strings.TrimLeft(arg, "/"); path != arg && path != "" {
return false, fmt.Errorf("path %s starts with /, and every path starts at the root already; write %s", arg, path)
return false, pathAdvice(path, fmt.Sprintf("path %s starts with /, and every path starts at the root already", arg))
}
return false, nil
}
@@ -82,6 +82,14 @@ func isJSONStart(arg string) bool {
return strings.HasPrefix(arg, "[") || strings.HasPrefix(arg, `"`)
}
// pathAdvice refuses a spelling of path, naming path to write, or why no name can spell it.
func pathAdvice(path, refusal string) error {
if err := checkPathNames(path); err != nil {
return err
}
return fmt.Errorf("%s; write %s", refusal, path)
}
// loneReference is the path a template spells when the value it holds — a format string, a
// JSON string, or an object holding only a format — is one reference token and nothing else.
func loneReference(arg string) (string, bool) {
@@ -101,6 +109,9 @@ func loneReference(arg string) (string, bool) {
if units[0].kind != 'b' || !isRef(body) || strings.ContainsAny(body, "|(") {
return "", false
}
if strings.HasPrefix(body, "/") {
body = "/" + strings.TrimLeft(body, "/")
}
_, path, err := refShape(body)
return path, err == nil
}