Thanks for your reply.
I tried moving from MultiArrayList
to ArrayList
however, the step to convert to JSON is now failing with this:
run
└─ run zig-playground
└─ zig build-exe zig-playground Debug native 4 errors
/nix/store/vm8x0fdp3gn99adhig5g0z9ri40hrq3w-zig-0.13.0/lib/zig/std/json/stringify.zig:552:52: error: cannot load opaque type 'anyopaque'
return self.write(value.*);
~~~~~^~
referenced by:
write__anon_9883: /nix/store/vm8x0fdp3gn99adhig5g0z9ri40hrq3w-zig-0.13.0/lib/zig/std/json/stringify.zig:533:43
write__anon_9561: /nix/store/vm8x0fdp3gn99adhig5g0z9ri40hrq3w-zig-0.13.0/lib/zig/std/json/stringify.zig:533:43
remaining reference traces hidden; use '-freference-trace' to see all reference traces
/nix/store/vm8x0fdp3gn99adhig5g0z9ri40hrq3w-zig-0.13.0/lib/zig/std/json/stringify.zig:552:52: error: values of type 'fn (*anyopaque, usize, u8, u
size) ?[*]u8' must be comptime-known, but operand value is runtime-known
return self.write(value.*);
~~~~~^~
/nix/store/vm8x0fdp3gn99adhig5g0z9ri40hrq3w-zig-0.13.0/lib/zig/std/json/stringify.zig:552:52: note: use '*const fn (*anyopaque, usize, u8, usize)
?[*]u8' for a function pointer type
/nix/store/vm8x0fdp3gn99adhig5g0z9ri40hrq3w-zig-0.13.0/lib/zig/std/json/stringify.zig:552:52: error: values of type 'fn (*anyopaque, []u8, u8, us
ize, usize) bool' must be comptime-known, but operand value is runtime-known
return self.write(value.*);
~~~~~^~
/nix/store/vm8x0fdp3gn99adhig5g0z9ri40hrq3w-zig-0.13.0/lib/zig/std/json/stringify.zig:552:52: note: use '*const fn (*anyopaque, []u8, u8, usize,
usize) bool' for a function pointer type
/nix/store/vm8x0fdp3gn99adhig5g0z9ri40hrq3w-zig-0.13.0/lib/zig/std/json/stringify.zig:552:52: error: values of type 'fn (*anyopaque, []u8, u8, us
ize) void' must be comptime-known, but operand value is runtime-known
return self.write(value.*);
~~~~~^~
/nix/store/vm8x0fdp3gn99adhig5g0z9ri40hrq3w-zig-0.13.0/lib/zig/std/json/stringify.zig:552:52: note: use '*const fn (*anyopaque, []u8, u8, usize)
void' for a function pointer type
error: the following command failed with 4 compilation errors:
The re-written code is this:
const std = @import("std");
const zap = @import("zap");
const Item = struct {
id: []const u8,
};
const ResponseArgs = struct {
docs: std.ArrayList(Item),
};
const Response = struct {
status: u8,
msg: []const u8,
args: ResponseArgs,
};
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator();
defer _ = gpa.deinit(); // Clean up the allocator
// or default: present menu
var docs = std.ArrayList(Item).init(allocator);
defer docs.deinit();
// Using try catch for error handling
try docs.append(.{ .id = "e0b596e5-eb14-4d48-8e21-63feb82a788c" });
try docs.append(.{ .id = "42c12917-6759-435c-8fc0-7f30812b4545" });
const response = Response{
.status = 200,
.msg = "Found 0 docs",
.args = ResponseArgs{ .docs = docs },
};
var buf: [100]u8 = undefined;
var json_to_send: []const u8 = undefined;
if (zap.stringifyBuf(&buf, response, .{})) |json| {
json_to_send = json;
} else {
json_to_send = "null";
}
std.debug.print("<< json: {s}\n", .{json_to_send});
}