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

jsonnetfmt cli tool could support a canonical mode #721

Open
netomi opened this issue Aug 11, 2023 · 0 comments
Open

jsonnetfmt cli tool could support a canonical mode #721

netomi opened this issue Aug 11, 2023 · 0 comments

Comments

@netomi
Copy link

netomi commented Aug 11, 2023

We use jsonnet to support an infrastructure as code approach to provision resources on GitHub.
For that purpose, a jsonnet file can be created that defines all the resources an organization should have on GitHub using some template functions to define defaults that can be overridden as needed.

Here is an example of such a configuration: https://github.com/adoptium/.eclipsefdn/blob/main/otterdog/adoptium.jsonnet

Now we also want a way to compare such a user edited configuration with a canonical version to highlight to the user which settings can be omitted (as they are already defined in the template function) and are thus redundant.

However, a simple diff will also outline irrelevant differences, e.g. due to different ordering of object fields or array elements. As a workaround for our relatively simple jsonnet files, we implemented a hacky way to produce a canonical version of such a jsonnet file.

An idea to make this cleaner would be to utilize the existing jsonnetfmt tool and adding an additional AST pass that sorts object fields and array elements according to some criteria, while outputting anything just like always.

The criteria that we came up so far was the following:

  • keep locals and asserts in objects untouched and stable and make sure they are before any object fields
  • convert each object field and array element into its string representation by stripping all fodder when sorting them

That approach was working well with our type of jsonnet files, which do not use that many jsonnet features.

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

No branches or pull requests

1 participant