Skip to content

Commit

Permalink
router returns a pointer to dispatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
karlseguin committed Sep 17, 2024
1 parent 844300c commit 4857368
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/httpz.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 6 additions & 6 deletions src/router.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand All @@ -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;
}
Expand Down

0 comments on commit 4857368

Please sign in to comment.