Hi! I just started learning some Zig the other night and have been doing small examples since then, and I find it very fun to learn!
Yesterday I started an example where I want to convert a json.Value to another type using the json.parseFromValue function. I get it work, partially. It works for basic types such as u16 and bool, but I cannot make it work with []const u8 fields.
This is not the first error message I get when running your test. The paramsTo method claims to return !std.testing, which doesn’t make sense in the first place because std.testing is a namespace, not meant to be used as a type. paramsTo returns param_type, so the return type should be !param_type.
Now, the reason why your test fails is because you call res.deinit() before the value is returned from paramsTo, which means you are returning a pointer ([]const u8) to already freed memory (this is why you get a aa byte, because free sets the freed data to undefined, which in debug mode writes 0xaa bytes).
The reason you are getting the failure on the string specifically is because the int/bool value are copied, whereas only the pointer/length of the string is copied, the actual memory containing the characters is not. You should be returning res without calling deinit and letting the caller free the memory after it is used:
Regarding !t that was what I first called the param_type but I forgot to change it when I copied it over here
That makes so much sense. Thank you so much for the help!
I guess it takes a while to get used to manage the memory like you do in zig.
I come from a Go/Rust background so using defer like I did was more of a reflex than a conscious thing – I learned something valuable because of this.