Reject a standalone weight on key presence, whatever its value
This commit is contained in:
+1
-1
@@ -91,7 +91,7 @@ func TestNewErrors(t *testing.T) {
|
|||||||
},
|
},
|
||||||
"an option name used as a token": {
|
"an option name used as a token": {
|
||||||
map[string]string{"a": `{"format":"{weight}"}`},
|
map[string]string{"a": `{"format":"{weight}"}`},
|
||||||
`"weight" is an option, never a field`,
|
`"weight" is an option and can never be a field`,
|
||||||
},
|
},
|
||||||
"category name with a dot": {
|
"category name with a dot": {
|
||||||
map[string]string{"a.b": `["1"]`},
|
map[string]string{"a.b": `["1"]`},
|
||||||
|
|||||||
@@ -51,13 +51,11 @@ type template struct {
|
|||||||
func (*template) isNode() {}
|
func (*template) isNode() {}
|
||||||
|
|
||||||
// compile converts parsed JSON into a node tree, validating structure up front.
|
// compile converts parsed JSON into a node tree, validating structure up front.
|
||||||
// Only a choice's items carry a weight, so a numeric one here would be inert.
|
// Only a choice's items carry a weight, so one here would be inert whatever its type.
|
||||||
func compile(v any) (node, error) {
|
func compile(v any) (node, error) {
|
||||||
if m, ok := v.(map[string]any); ok {
|
if m, ok := v.(map[string]any); ok {
|
||||||
if w, weighted := m["weight"]; weighted {
|
if _, weighted := m["weight"]; weighted {
|
||||||
if _, isNumber := w.(float64); isNumber {
|
return nil, fmt.Errorf("weight only skews a choice's items, so it has no effect here; it is an option and can never be a field")
|
||||||
return nil, fmt.Errorf("weight only skews a choice's items, so it has no effect here")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return compileItem(v)
|
return compileItem(v)
|
||||||
|
|||||||
+1
-1
@@ -144,7 +144,7 @@ func checkTokens(format string, fields map[string]node) error {
|
|||||||
}
|
}
|
||||||
if _, ok := fields[name]; !ok {
|
if _, ok := fields[name]; !ok {
|
||||||
if isOption(name) {
|
if isOption(name) {
|
||||||
return fmt.Errorf("token {%s}: %q is an option, never a field", t.body, name)
|
return fmt.Errorf("token {%s}: %q is an option and can never be a field", t.body, name)
|
||||||
}
|
}
|
||||||
return fmt.Errorf("token {%s}: no field %q", t.body, name)
|
return fmt.Errorf("token {%s}: no field %q", t.body, name)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user