-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ast: Marshal non-string object keys when converting to interface{} #2577
ast: Marshal non-string object keys when converting to interface{} #2577
Conversation
Previously, when OPA attempted to convert an ast.Object to interface{} it would error if the ast.Object contained any keys that were not ast.String values. This behaviour was implemented because JSON only supports object keys as strings. This commit changes the conversion implementation to simply JSON marshal non-string object keys when they are encountered. This way we avoid runtime errors which can be difficult to debug. Fixes open-policy-agent#516 Signed-off-by: Torin Sandall <torinsandall@gmail.com>
cc @timothyhinrichs @srenatus @jaspervdj-luminal tagging a few folks on this change to get thoughts. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think of the options we have this seems like the best approach. IMO I much prefer this over raising an error.
Seems good to me. Gives us more flexibility inside of Rego AND moves us toward fewer runtime errors. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also like this, makes it easy to debug what the problematic keys were.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking of some potential trouble in unmarshaling, but this is not what's happening here. LGTM :)
} | ||
vi, err := ValueToInterface(v.Value, resolver) | ||
if err != nil { | ||
return err | ||
} | ||
buf[asStr] = vi | ||
buf[str] = vi |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In case of a clash ({"1":0, 1:0}
), the key processed second will overwrite the one processed first, right?
I see no solid solution here, only ways to make it less likely, like marshaling the example thing to {"[1]":0,"1":0}
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tried it briefly to verify:
> { 1: true, "1": false, "{}": "foo", {}: "bar" }
{
"1": false,
"{}": "bar"
}
>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice find. Agreed, there's no good solution here. I think this is still better than having policy evaluation return an error. I'm going to go ahead and merge.
Previously, when OPA attempted to convert an ast.Object to interface{}
it would error if the ast.Object contained any keys that were not
ast.String values. This behaviour was implemented because JSON only
supports object keys as strings.
This commit changes the conversion implementation to simply JSON
marshal non-string object keys when they are encountered. This way
we avoid runtime errors which can be difficult to debug.
Fixes #516
Signed-off-by: Torin Sandall torinsandall@gmail.com