From dfaadf314089fe5fe689bf701e1fce179d2c40f5 Mon Sep 17 00:00:00 2001 From: Rene Schallner Date: Tue, 18 Apr 2023 21:02:43 +0200 Subject: [PATCH] Enpoint example: from singleton to @fieldParentPtr --- examples/endpoint/endpoint.zig | 87 +++++++++++++++++----------------- examples/endpoint/main.zig | 8 ++-- 2 files changed, 48 insertions(+), 47 deletions(-) diff --git a/examples/endpoint/endpoint.zig b/examples/endpoint/endpoint.zig index aebd66a..1a3ac80 100644 --- a/examples/endpoint/endpoint.zig +++ b/examples/endpoint/endpoint.zig @@ -3,57 +3,60 @@ const zap = @import("zap"); const Users = @import("users.zig"); const User = Users.User; -// the Endpoint +// an Endpoint pub const Self = @This(); -var alloc: std.mem.Allocator = undefined; -var endpoint: zap.SimpleEndpoint = undefined; -var users: Users = undefined; +alloc: std.mem.Allocator = undefined, +endpoint: zap.SimpleEndpoint = undefined, +users: Users = undefined, pub fn init( a: std.mem.Allocator, user_path: []const u8, -) void { - users = Users.init(a); - alloc = a; - endpoint = zap.SimpleEndpoint.init(.{ - .path = user_path, - .get = getUser, - .post = postUser, - .put = putUser, - .delete = deleteUser, - }); +) Self { + return .{ + .alloc = a, + .users = Users.init(a), + .endpoint = zap.SimpleEndpoint.init(.{ + .path = user_path, + .get = getUser, + .post = postUser, + .put = putUser, + .delete = deleteUser, + }), + }; } -pub fn getUsers() *Users { - return &users; +pub fn getUsers(self: *Self) *Users { + return &self.users; } -pub fn getUserEndpoint() *zap.SimpleEndpoint { - return &endpoint; +pub fn getUserEndpoint(self: *Self) *zap.SimpleEndpoint { + return &self.endpoint; } -fn userIdFromPath(path: []const u8) ?usize { - if (path.len >= endpoint.settings.path.len + 2) { - if (path[endpoint.settings.path.len] != '/') { +fn userIdFromPath(self: *Self, path: []const u8) ?usize { + if (path.len >= self.endpoint.settings.path.len + 2) { + if (path[self.endpoint.settings.path.len] != '/') { return null; } - const idstr = path[endpoint.settings.path.len + 1 ..]; + const idstr = path[self.endpoint.settings.path.len + 1 ..]; return std.fmt.parseUnsigned(usize, idstr, 10) catch null; } return null; } fn getUser(e: *zap.SimpleEndpoint, r: zap.SimpleRequest) void { + const self = @fieldParentPtr(Self, "endpoint", e); if (r.path) |path| { // /users if (path.len == e.settings.path.len) { - return listUsers(e, r); + return self.listUsers(r); } var jsonbuf: [256]u8 = undefined; - if (userIdFromPath(path)) |id| { - if (users.get(id)) |user| { + if (self.userIdFromPath(path)) |id| { + if (self.users.get(id)) |user| { if (zap.stringifyBuf(&jsonbuf, user, .{})) |json| { r.sendJson(json) catch return; } @@ -62,11 +65,9 @@ fn getUser(e: *zap.SimpleEndpoint, r: zap.SimpleRequest) void { } } -fn listUsers(e: *zap.SimpleEndpoint, r: zap.SimpleRequest) void { - _ = e; - - if (users.toJSON()) |json| { - defer alloc.free(json); +fn listUsers(self: *Self, r: zap.SimpleRequest) void { + if (self.users.toJSON()) |json| { + defer self.alloc.free(json); r.sendJson(json) catch return; } else |err| { std.debug.print("LIST error: {}\n", .{err}); @@ -74,13 +75,13 @@ fn listUsers(e: *zap.SimpleEndpoint, r: zap.SimpleRequest) void { } fn postUser(e: *zap.SimpleEndpoint, r: zap.SimpleRequest) void { - _ = e; + const self = @fieldParentPtr(Self, "endpoint", e); if (r.body) |body| { var stream = std.json.TokenStream.init(body); - var maybe_user: ?User = std.json.parse(User, &stream, .{ .allocator = alloc }) catch null; + var maybe_user: ?User = std.json.parse(User, &stream, .{ .allocator = self.alloc }) catch null; if (maybe_user) |u| { - defer std.json.parseFree(User, u, .{ .allocator = alloc }); - if (users.addByName(u.first_name, u.last_name)) |id| { + defer std.json.parseFree(User, u, .{ .allocator = self.alloc }); + if (self.users.addByName(u.first_name, u.last_name)) |id| { var jsonbuf: [128]u8 = undefined; if (zap.stringifyBuf(&jsonbuf, .{ .status = "OK", .id = id }, .{})) |json| { r.sendJson(json) catch return; @@ -94,17 +95,17 @@ fn postUser(e: *zap.SimpleEndpoint, r: zap.SimpleRequest) void { } fn putUser(e: *zap.SimpleEndpoint, r: zap.SimpleRequest) void { - _ = e; + const self = @fieldParentPtr(Self, "endpoint", e); if (r.path) |path| { - if (userIdFromPath(path)) |id| { - if (users.get(id)) |_| { + if (self.userIdFromPath(path)) |id| { + if (self.users.get(id)) |_| { if (r.body) |body| { var stream = std.json.TokenStream.init(body); - var maybe_user: ?User = std.json.parse(User, &stream, .{ .allocator = alloc }) catch null; + var maybe_user: ?User = std.json.parse(User, &stream, .{ .allocator = self.alloc }) catch null; if (maybe_user) |u| { - defer std.json.parseFree(User, u, .{ .allocator = alloc }); + defer std.json.parseFree(User, u, .{ .allocator = self.alloc }); var jsonbuf: [128]u8 = undefined; - if (users.update(id, u.first_name, u.last_name)) { + if (self.users.update(id, u.first_name, u.last_name)) { if (zap.stringifyBuf(&jsonbuf, .{ .status = "OK", .id = id }, .{})) |json| { r.sendJson(json) catch return; } @@ -121,11 +122,11 @@ fn putUser(e: *zap.SimpleEndpoint, r: zap.SimpleRequest) void { } fn deleteUser(e: *zap.SimpleEndpoint, r: zap.SimpleRequest) void { - _ = e; + const self = @fieldParentPtr(Self, "endpoint", e); if (r.path) |path| { - if (userIdFromPath(path)) |id| { + if (self.userIdFromPath(path)) |id| { var jsonbuf: [128]u8 = undefined; - if (users.delete(id)) { + if (self.users.delete(id)) { if (zap.stringifyBuf(&jsonbuf, .{ .status = "OK", .id = id }, .{})) |json| { r.sendJson(json) catch return; } diff --git a/examples/endpoint/main.zig b/examples/endpoint/main.zig index 828b656..9f60af5 100644 --- a/examples/endpoint/main.zig +++ b/examples/endpoint/main.zig @@ -21,15 +21,15 @@ pub fn main() !void { }, ); - Endpoint.init(allocator, "/users"); + var endpoint = Endpoint.init(allocator, "/users"); // add endpoint - try listener.addEndpoint(Endpoint.getUserEndpoint()); + try listener.addEndpoint(endpoint.getUserEndpoint()); // fake some users var uid: usize = undefined; - uid = try Endpoint.getUsers().addByName("renerocksai", null); - uid = try Endpoint.getUsers().addByName("renerocksai", "your mom"); + uid = try endpoint.getUsers().addByName("renerocksai", null); + uid = try endpoint.getUsers().addByName("renerocksai", "your mom"); // listen try listener.listen();