Skip to content
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

Remove jsonOptions from AST nodes and terms #7281

Merged
merged 1 commit into from
Jan 22, 2025

Conversation

anderseknert
Copy link
Member

This was originally added to allow Regal to get a serialized AST that included location data wherever that was possible. Regal is however no longer using OPA's JSON serialization but its own custom encoder. Attaching these options to every AST node and term comes with a cost attached, and without any known users of this feature vs. the many users who care about resource utilization, this feels like an easy choice.

While it seems unlikely to be users depending on this functionality — in case someone needs it, the options for serializing AST nodes to JSON can now be set globally instead. Global state is always awkward, but since JSON marshalling methods only have access to the node being marshalled and of course, global state, there's not a whole lot of options if we intend to keep this feature.

Copy link
Contributor

@charlieegan3 charlieegan3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me. I initially expected us to deprecate this and then remove it later. However, this way, we address the issue with jsonOptions everywhere immediately while keeping functionality via another route for those who might need it.

Copy link
Contributor

@johanfylling johanfylling left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

lock sync.RWMutex
}

var options = &configuredJSONOptions{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason why this is a pointer?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The lock on the struct. If the struct isn't a pointer then the lock would have to be, and the "result" would be the same more or less.

astJSON.SetOptions(astJSON.Options{
MarshalOptions: astJSON.MarshalOptions{
IncludeLocation: astJSON.NodeToggle{
// Annotation location data is only included if includeAnnotations is set
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cpy/paste?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, thanks, I'll fix that.

AnnotationsRef: true,
},
},
})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clearly, this is needed to get the test to pass, but I'm missing where this defaulted to true before.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The true in File(rootDir, true) is for including annotations. I had put a SetOptions call inside of that loader but removed it now.. this should only have impact on serialization.

}

// GetOptions returns (a copy of) the global options for marshalling AST nodes to JSON
func GetOptions() Options {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider renaming these to something like GlobalOptions() and SetGlobalOptions() to hammer the "global" part home.
If options, above, doesn't need to be a pointer, we could even make that a public GlobalOptions var and attach these functions as methods?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it's just a matter of preference, but to me, astJSON.GlobalOptions.Set() looks slightly worse 😅 I think I went with this naming mostly to try and follow the previous convention. I don't feel strongly about it, and even less so considering this mostly will go unused :)

foo := "bar"
`
assertParseModuleJSONOptions(t, `foo := "bar"`, module, parserOpts)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no point in keeping these tests but using the global options instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really — these tests assert that the parser specifically would use the options it got to propagate them on each node parsed. Since we use a global option now, the parser doesn't need to be involved at all in this.

func VarHead(name Var, location *Location, jsonOpts *astJSON.Options) *Head {
// VarHead creates a head object, initializes its Name and Location and returns the new head.
// NOTE: The JSON options argument is no longer used, and kept only for backwards compatibility.
func VarHead(name Var, location *Location, _ *astJSON.Options) *Head {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might go against what might be otherwise be considered idiomatic go, but we could also make the *astJSON.Options arg variadic, so new code isn't required to use it, and maybe make it easier to drop in the future.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tbh, I'd be more inclined to just drop the last argument directly. Even we don't use this ever outside of the parser, so it's hard to think there's external code depending on this... but i guess we can do that if/when we drop this altogether.

Copy link

netlify bot commented Jan 22, 2025

Deploy Preview for openpolicyagent ready!

Name Link
🔨 Latest commit 10b4bc4
🔍 Latest deploy log https://app.netlify.com/sites/openpolicyagent/deploys/6790b7d785bfbc0008dfefa7
😎 Deploy Preview https://deploy-preview-7281--openpolicyagent.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link
Contributor

@johanfylling johanfylling left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

This was originally added to allow Regal to get a serialized AST that included
location data wherever that was possible. Regal is however no longer using OPA's
JSON serialization but its own custom encoder. Attaching these options to every
AST node and term comes with a cost attached, and without any known users of this
feature vs. the many users who care about resource utilization, this feels like
an easy choice.

While it seems unlikely to be users depending on this functionality — in case
someone needs it, the options for serializing AST nodes to JSON can now be set
globally instead. Global state is always awkward, but since JSON marshalling
methods only have access to the node being marshalled and of course, global
state, there's not a whole lot of options if we intend to keep this feature.

Signed-off-by: Anders Eknert <[email protected]>
@anderseknert anderseknert enabled auto-merge (squash) January 22, 2025 09:18
@anderseknert anderseknert merged commit 0b94dd8 into open-policy-agent:main Jan 22, 2025
28 checks passed
@anderseknert anderseknert deleted the no-json-options branch January 22, 2025 12:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants