diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-01-19 03:17:52 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-01-19 03:17:52 +0900 |
| commit | 1513ca1a19d4758b3c166ac14040d5d3f7193218 (patch) | |
| tree | 58b6f32847cfa4bfbad72a59a5e6fb4283b02c66 /src/root.zig | |
| parent | 13128e5e506c7b50716bd5d587135c63d7c8a278 (diff) | |
| download | zgjq-1513ca1a19d4758b3c166ac14040d5d3f7193218.tar.gz zgjq-1513ca1a19d4758b3c166ac14040d5d3f7193218.tar.zst zgjq-1513ca1a19d4758b3c166ac14040d5d3f7193218.zip | |
allow query to return multiple values
Diffstat (limited to 'src/root.zig')
| -rw-r--r-- | src/root.zig | 25 |
1 files changed, 23 insertions, 2 deletions
diff --git a/src/root.zig b/src/root.zig index b89b54f..9c22deb 100644 --- a/src/root.zig +++ b/src/root.zig @@ -14,15 +14,36 @@ pub fn run(allocator: std.mem.Allocator, input: []const u8, query: []const u8) ! const parsed = try jv.parse(allocator, input); defer parsed.deinit(); const json = parsed.value; - const result = try jq.execute(allocator, instrs, json); + + var runtime = try jq.Runtime.init(allocator, instrs, json); + defer runtime.deinit(); + const result = try runtime.next() orelse return error.NoResult; const output = try jv.stringify(allocator, result); return output; } fn testRun(expected: []const u8, allocator: std.mem.Allocator, input: []const u8, query: []const u8) !void { - const result = try run(allocator, input, query); + var compile_allocator = std.heap.ArenaAllocator.init(allocator); + defer compile_allocator.deinit(); + var reader = std.Io.Reader.fixed(query); + const tokens = try jq.tokenize(compile_allocator.allocator(), &reader); + const ast = try jq.parse(compile_allocator.allocator(), tokens); + const instrs = try jq.compile(allocator, compile_allocator.allocator(), ast); + defer allocator.free(instrs); + + const parsed = try jv.parse(allocator, input); + defer parsed.deinit(); + const json = parsed.value; + + var runtime = try jq.Runtime.init(allocator, instrs, json); + defer runtime.deinit(); + + const result_value = try runtime.next() orelse return error.NoResult; + const result = try jv.stringify(allocator, result_value); defer allocator.free(result); try std.testing.expectEqualStrings(expected, result); + + try std.testing.expectEqual(null, try runtime.next()); } test "identity filter" { |
