From 48573688344d592c6db7a4fe3fc35ddbc4510b40 Mon Sep 17 00:00:00 2001 From: Karl Seguin Date: Tue, 17 Sep 2024 08:56:21 +0800 Subject: [PATCH] router returns a pointer to dispatcher --- src/httpz.zig | 2 +- src/router.zig | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/httpz.zig b/src/httpz.zig index 7ab482b..d181b99 100644 --- a/src/httpz.zig +++ b/src/httpz.zig @@ -616,7 +616,7 @@ pub fn Server(comptime H: type) type { handler: H, // pull this out of da since we'll access it a lot (not really, but w/e) middlewares: []const Middleware(H), - dispatchable_action: ?DispatchableAction(H, ActionArg), + dispatchable_action: ?*const DispatchableAction(H, ActionArg), pub fn next(self: *Executor) !void { const index = self.index; diff --git a/src/router.zig b/src/router.zig index 0b53e34..67693ce 100644 --- a/src/router.zig +++ b/src/router.zig @@ -69,7 +69,7 @@ pub fn Router(comptime Handler: type, comptime Action: type) type { return Group(Handler, Action).init(self, prefix, config); } - pub fn route(self: *Self, method: httpz.Method, url: []const u8, params: *Params) ?DispatchableAction { + pub fn route(self: *Self, method: httpz.Method, url: []const u8, params: *Params) ?*const DispatchableAction { return switch (method) { httpz.Method.GET => getRoute(DispatchableAction, &self._get, url, params), httpz.Method.POST => getRoute(DispatchableAction, &self._post, url, params), @@ -409,10 +409,10 @@ pub fn Part(comptime A: type) type { }; } -fn getRoute(comptime A: type, root: *const Part(A), url: []const u8, params: *Params) ?A { +fn getRoute(comptime A: type, root: *const Part(A), url: []const u8, params: *Params) ?*const A { std.debug.assert(url.len != 0); if (url.len == 1 and url[0] == '/') { - return root.action; + return if (root.action) |*a| a else null; } var normalized = url; @@ -447,14 +447,14 @@ fn getRoute(comptime A: type, root: *const Part(A), url: []const u8, params: *Pa } else { params.len = 0; if (glob_all) |fallback| { - return fallback.action; + return if (fallback.action) |*a| a else null; } return null; } pos = index + 1; // +1 tos skip the slash on the next iteration } - if (route_part.action) |action| { + if (route_part.action) |*action| { if (route_part.param_names) |names| { params.addNames(names); } else { @@ -465,7 +465,7 @@ fn getRoute(comptime A: type, root: *const Part(A), url: []const u8, params: *Pa params.len = 0; if (glob_all) |fallback| { - return fallback.action; + return if (fallback.action) |*a| a else null; } return null; }