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

Is there any way to traverse each field of a zon? #4

Closed
jinzhongjia opened this issue Jun 5, 2024 · 4 comments
Closed

Is there any way to traverse each field of a zon? #4

jinzhongjia opened this issue Jun 5, 2024 · 4 comments

Comments

@jinzhongjia
Copy link

I want to parse the build.zig.zon file and get all the dependencies

@Durobot
Copy link
Owner

Durobot commented Jun 20, 2024

I can't think of a simple way to do that with or without zon_get_fields.

zon_get_fields is based on the assumption that the user (you) knows the structure of the ZON text they want to read / parse / get values from.

build.zig.zon's .dependencies, on the other hand, can contain unknown number of arbitrarily named nested structures.
See here, for example - .dependencies contains two nested structs, .libz and .libmp3lame, but these names can be pretty much anything.

If I were to solve this problem, I'd take a look at the way fn walkAst works, and I would write a similar function, but instead of trying to return the value of the field specified by its path_itr parameter, this function would enumerate its nested fields (structs). Knowing their names you would then be able to call getFieldVal to get their url and hash fields.

This, of course, would require some work on your part.
Sorry to disappoint, but that's the way it is, currently.

@Durobot
Copy link
Owner

Durobot commented Jun 21, 2024

Here's a quick and dirty solution I came up with:
https://gist.github.com/Durobot/985edc8b04718c79b0ca3469af9380b1
This just prints out the names of the fields that sit at the end of the path you provide to enumerateChildren ("abc" and "def").

It works when "dependencies" is either a struct, or an array of structs (see the code that is commented out in pub fn main()).

Feel free to do with this code whatever you want.

Hope this helps.

@jinzhongjia
Copy link
Author

nice work

@Durobot
Copy link
Owner

Durobot commented Jul 1, 2024

@jinzhongjia - I'm going to close this issue, if this is OK with you.

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

2 participants