A while ago I was looking for a way to use ZON as a configuration fie format, and found none. The parsing itself was there (std.zig.Ast.parse()
), but getting the values from the Ast
required a considerable amount of work.
Well, since I did that work, why not share it with the world? So, here it is -
https://github.com/Durobot/zon_get_fields.
The thing is not optimal, since each call of a getFieldVal*()
function has to split the provided path and walk the AST, but at least it works.
Also, there’s this strange thing with negative field values represented by two tokens in AST, the first being “-”, and the next containing the digits. I hope the way I’m reading them (getting “-”, and then getting the next token, here) is correct in the long run, but honestly, I don’t know, and it doesn’t sit well with me.
Update 2023-12-31:
- Switched from several
getFieldVal*
functions to one public function -getFieldVal
; - Added support for proper parsing of character literals (although not Unicode literals at this time);
- Updated example code in README.
Happy New Year everyone!
Update 2024-01-25: Added a new function (template), which fills your arbitrary struct with values from the provided AST - pub fn zonToStruct
.
Update 2024-01-28: Unicode character literals are now supported.
Update 2024-02-22: pub fn zonToStruct
now returns an auto-generated struct with fields that indicate whether their corresponding fields in the target struct were filled or not. Previously there was no way to tell.
Update 2024-03-05: Now with Zig package manager support!