Closed fengb closed 3 years ago
Trying to read stuff like this causes major compiler bugs:
.{
.user_id = json.Path.number(u64, "@.user.id"),
.session_id = json.Path.stringAlloc(allocator, "@.session_id"),
.app_name = json.Path.stringBuffer(buffer, "@.application.name"),
}
I think this is the same problem Alex Nask ran into: the compiler gets confused because there's a mixture of comptime and runtime: allocator
is runtime known but it's trying to be stored in a comptime context (default_value)
Alternate syntax:
const match = root.pathMatch(std.testing.allocator, struct {
"@.foo": bool,
"@.bar": u32,
"@.baz": []const u8,
});
This one cannot use scoped runtime values (e.g. buffers), but it seems to behave a lot better. A FixedBufferAllocator is actually more flexible so maybe this isn't a big loss.
Oh hey this is working now!
As suggested by @kristoff-it, it'd be really cool if we could have a declarative syntax. We could parse a JSON path and generate the necessary plumbing to read it.
Unfortunately due to streams being stateful, this logic must be generated simultaneously (at least per single array/object). Building a struct seems like the best option:
This should generate something similar to: