zig-toml icon indicating copy to clipboard operation
zig-toml copied to clipboard

Can't parse HashMap

Open sirenkovladd opened this issue 1 year ago • 4 comments

this is an example of toml

[tasks.a]
cmd = "echo 123"

[tasks.b]
cmd = "echo 123"

I need a possible way to parse this tasks, I can't predict the keys

Thank you

sirenkovladd avatar Jun 04 '24 05:06 sirenkovladd

This is a good point!

Arbitrary HashMaps are not supported yet, e.g. HashMap(string, MyValueType...). I do not know yet how to map toml.Value to any possible MyValueType. I'm open to suggestions.

I have just pushed a commit that enables structs to have a field of type toml.Table. Here is an example from the tests.

sam701 avatar Jun 04 '24 07:06 sam701

I was researching how the json parser works and I really liked the approach how it was done in the kernel here you can find an example of how to use https://github.com/ziglang/zig/blob/master/lib/std/json/hashmap_test.zig

and here is exactly how it checks what the hashmap should be https://github.com/ziglang/zig/blob/master/lib/std/json/static.zig#L321-L323

the main idea is that it checks whether the structure has a key function (jsonParse) and if so, simply passes it the parameters

I'm not one hundred percent sure yet, but with a quick glance, it seemed that if you wrote your own scanner, and you can pass it to json functions and most of the code from this library will simply disappear

sirenkovladd avatar Jun 04 '24 08:06 sirenkovladd

Anyway, thank you for the quick fix

sirenkovladd avatar Jun 04 '24 08:06 sirenkovladd

Thanks for pointing me to the "official" json parser. 👍 I see they changed quite a bit. I will look into.

sam701 avatar Jun 04 '24 08:06 sam701

Finally implemented.

@sirenkovladd Thanks again for the helpful links! The implementation was simpler than I initially thought.

sam701 avatar Oct 18 '24 20:10 sam701