mirror of
https://github.com/zigzap/zap.git
synced 2025-10-20 23:24:09 +00:00
zap util
This commit is contained in:
parent
5c8aa438f9
commit
b79d4d58c6
5 changed files with 50 additions and 38 deletions
|
@ -57,12 +57,12 @@ fn userIdFromPath(path: []const u8) ?usize {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn getUser(e: *zap.SimpleEndpoint, r: zap.SimpleRequest) void {
|
fn getUser(e: *zap.SimpleEndpoint, r: zap.SimpleRequest) void {
|
||||||
_ = e;
|
_ = e;
|
||||||
if (r.path) |path| {
|
if (r.path) |path| {
|
||||||
if (userIdFromPath(path)) |id| {
|
if (userIdFromPath(path)) |id| {
|
||||||
if (users.get(id)) |user| {
|
if (users.get(id)) |user| {
|
||||||
if (Users.stringify(user, .{})) |json| {
|
if (zap.stringify(user, .{})) |json| {
|
||||||
_ = r.sendJson(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;
|
_ = e;
|
||||||
var l: std.ArrayList(Users.User) = std.ArrayList(Users.User).init(alloc);
|
var l: std.ArrayList(Users.User) = std.ArrayList(Users.User).init(alloc);
|
||||||
if (users.list(&l)) {} else |_| {
|
if (users.list(&l)) {} else |_| {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (Users.stringifyUserList(&l, .{})) |maybe_json| {
|
if (zap.stringifyArrayList(Users.User, &l, .{})) |maybe_json| {
|
||||||
if (maybe_json) |json| {
|
if (maybe_json) |json| {
|
||||||
_ = r.sendJson(json);
|
_ = r.sendJson(json);
|
||||||
}
|
}
|
||||||
|
@ -84,3 +84,16 @@ pub fn listUsers(e: *zap.SimpleEndpoint, r: zap.SimpleRequest) void {
|
||||||
return;
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -2,6 +2,6 @@
|
||||||
<body>
|
<body>
|
||||||
<p><a href="/user/1">Show example user 1</a></p>
|
<p><a href="/user/1">Show example user 1</a></p>
|
||||||
<p><a href="/user/2">Show example user 2</a></p>
|
<p><a href="/user/2">Show example user 2</a></p>
|
||||||
<p><a href="/user/2">Show ALL users</a></p>
|
<p><a href="/list">Show ALL users</a></p>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
|
@ -111,36 +111,3 @@ const JsonUserIterator = struct {
|
||||||
return null;
|
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;
|
|
||||||
}
|
|
||||||
|
|
31
src/util.zig
Normal file
31
src/util.zig
Normal file
|
@ -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;
|
||||||
|
}
|
|
@ -8,6 +8,7 @@ pub const C = @cImport({
|
||||||
});
|
});
|
||||||
|
|
||||||
pub usingnamespace @import("endpoint.zig");
|
pub usingnamespace @import("endpoint.zig");
|
||||||
|
pub usingnamespace @import("util.zig");
|
||||||
|
|
||||||
pub fn fio2str(o: C.FIOBJ) ?[]const u8 {
|
pub fn fio2str(o: C.FIOBJ) ?[]const u8 {
|
||||||
if (o == 0) return null;
|
if (o == 0) return null;
|
||||||
|
|
Loading…
Add table
Reference in a new issue