mirror of
https://github.com/open-policy-agent/opa.git
synced 2026-08-24 09:15:31 -06:00
Escape keyword strings in ast.Ref#String
Previously refs like p["not"] would be represented as p.not which does not parse because 'not' is an invalid variable name. This change ensures that ref string operands are escape if they are keywords.
This commit is contained in:
+1
-1
@@ -910,7 +910,7 @@ func (ref Ref) String() string {
|
||||
switch p := p.Value.(type) {
|
||||
case String:
|
||||
str := string(p)
|
||||
if varRegexp.MatchString(str) && len(buf) > 0 {
|
||||
if varRegexp.MatchString(str) && len(buf) > 0 && !IsKeyword(str) {
|
||||
buf = append(buf, "."+str)
|
||||
} else {
|
||||
buf = append(buf, "["+p.String()+"]")
|
||||
|
||||
@@ -311,6 +311,7 @@ func TestTermString(t *testing.T) {
|
||||
assertToString(t, RefTerm(VarTerm("foo"), StringTerm("bar")).Value, "foo.bar")
|
||||
assertToString(t, RefTerm(VarTerm("foo"), StringTerm("bar"), VarTerm("i"), IntNumberTerm(0), StringTerm("baz")).Value, "foo.bar[i][0].baz")
|
||||
assertToString(t, RefTerm(VarTerm("foo"), BooleanTerm(false), NullTerm(), StringTerm("bar")).Value, "foo[false][null].bar")
|
||||
assertToString(t, RefTerm(VarTerm("p"), StringTerm("not")).Value, `p["not"]`)
|
||||
assertToString(t, ArrayTerm().Value, "[]")
|
||||
assertToString(t, ObjectTerm().Value, "{}")
|
||||
assertToString(t, SetTerm().Value, "set()")
|
||||
|
||||
Reference in New Issue
Block a user