From b79d4d58c66a7857850bd2387fef2dc38adcc742 Mon Sep 17 00:00:00 2001 From: Rene Schallner Date: Sat, 14 Jan 2023 22:18:21 +0100 Subject: [PATCH] zap util --- examples/endpoints/endpoints.zig | 21 +++++++++++++++---- examples/endpoints/html/index.html | 2 +- examples/endpoints/users.zig | 33 ------------------------------ src/util.zig | 31 ++++++++++++++++++++++++++++ src/zap.zig | 1 + 5 files changed, 50 insertions(+), 38 deletions(-) create mode 100644 src/util.zig diff --git a/examples/endpoints/endpoints.zig b/examples/endpoints/endpoints.zig index a2d1c70..2885389 100644 --- a/examples/endpoints/endpoints.zig +++ b/examples/endpoints/endpoints.zig @@ -57,12 +57,12 @@ fn userIdFromPath(path: []const u8) ?usize { return null; } -pub fn getUser(e: *zap.SimpleEndpoint, r: zap.SimpleRequest) void { +fn getUser(e: *zap.SimpleEndpoint, r: zap.SimpleRequest) void { _ = e; if (r.path) |path| { if (userIdFromPath(path)) |id| { if (users.get(id)) |user| { - if (Users.stringify(user, .{})) |json| { + if (zap.stringify(user, .{})) |json| { _ = r.sendJson(json); } } @@ -70,13 +70,13 @@ pub fn getUser(e: *zap.SimpleEndpoint, r: zap.SimpleRequest) void { } } -pub fn listUsers(e: *zap.SimpleEndpoint, r: zap.SimpleRequest) void { +fn listUsers(e: *zap.SimpleEndpoint, r: zap.SimpleRequest) void { _ = e; var l: std.ArrayList(Users.User) = std.ArrayList(Users.User).init(alloc); if (users.list(&l)) {} else |_| { return; } - if (Users.stringifyUserList(&l, .{})) |maybe_json| { + if (zap.stringifyArrayList(Users.User, &l, .{})) |maybe_json| { if (maybe_json) |json| { _ = r.sendJson(json); } @@ -84,3 +84,16 @@ pub fn listUsers(e: *zap.SimpleEndpoint, r: zap.SimpleRequest) void { return; } } + +fn postUser(e: *zap.SimpleEndpoint, r: zap.SimpleRequest) void { + _ = e; + if (r.path) |path| { + if (userIdFromPath(path)) |id| { + if (users.get(id)) |user| { + if (zap.stringify(user, .{})) |json| { + _ = r.sendJson(json); + } + } + } + } +} diff --git a/examples/endpoints/html/index.html b/examples/endpoints/html/index.html index eb6d074..270c2c0 100644 --- a/examples/endpoints/html/index.html +++ b/examples/endpoints/html/index.html @@ -2,6 +2,6 @@

Show example user 1

Show example user 2

-

Show ALL users

+

Show ALL users

diff --git a/examples/endpoints/users.zig b/examples/endpoints/users.zig index 2646659..788a0b5 100644 --- a/examples/endpoints/users.zig +++ b/examples/endpoints/users.zig @@ -111,36 +111,3 @@ const JsonUserIterator = struct { return null; } }; - -// -// JSON helpers -// -var jsonbuf: [100 * 1024]u8 = undefined; - -pub fn stringify(value: anytype, options: std.json.StringifyOptions) ?[]const u8 { - var fba = std.heap.FixedBufferAllocator.init(&jsonbuf); - var string = std.ArrayList(u8).init(fba.allocator()); - if (std.json.stringify(value, options, string.writer())) { - return string.items; - } else |_| { // error - return null; - } -} - -pub fn stringifyUserList( - userlist: *std.ArrayList(User), - options: std.json.StringifyOptions, -) !?[]const u8 { - var fba = std.heap.FixedBufferAllocator.init(&jsonbuf); - var string = std.ArrayList(u8).init(fba.allocator()); - var writer = string.writer(); - try writer.writeByte('['); - var first: bool = true; - for (userlist.items) |user| { - if (!first) try writer.writeByte(','); - first = false; - try std.json.stringify(user, options, string.writer()); - } - try writer.writeByte(']'); - return string.items; -} diff --git a/src/util.zig b/src/util.zig new file mode 100644 index 0000000..ab865b0 --- /dev/null +++ b/src/util.zig @@ -0,0 +1,31 @@ +const std = @import("std"); + +// +// JSON helpers +// +var jsonbuf: [100 * 1024]u8 = undefined; + +pub fn stringify(value: anytype, options: std.json.StringifyOptions) ?[]const u8 { + var fba = std.heap.FixedBufferAllocator.init(&jsonbuf); + var string = std.ArrayList(u8).init(fba.allocator()); + if (std.json.stringify(value, options, string.writer())) { + return string.items; + } else |_| { // error + return null; + } +} + +pub fn stringifyArrayList(comptime T: anytype, list: *std.ArrayList(T), options: std.json.StringifyOptions) !?[]const u8 { + var fba = std.heap.FixedBufferAllocator.init(&jsonbuf); + var string = std.ArrayList(u8).init(fba.allocator()); + var writer = string.writer(); + try writer.writeByte('['); + var first: bool = true; + for (list.items) |user| { + if (!first) try writer.writeByte(','); + first = false; + try std.json.stringify(user, options, string.writer()); + } + try writer.writeByte(']'); + return string.items; +} diff --git a/src/zap.zig b/src/zap.zig index 6a9e8ae..65b122a 100644 --- a/src/zap.zig +++ b/src/zap.zig @@ -8,6 +8,7 @@ pub const C = @cImport({ }); pub usingnamespace @import("endpoint.zig"); +pub usingnamespace @import("util.zig"); pub fn fio2str(o: C.FIOBJ) ?[]const u8 { if (o == 0) return null;