Skip to content

Commit

Permalink
Skip parsing functions that are not in Route.Action enum
Browse files Browse the repository at this point in the history
Allow custom functions with arbitrary signatures and avoid any issues of
trying to parse functions that are not Jeztig route action functions
(i.e. index, get, post, put, patch, delete).
  • Loading branch information
bobf committed Mar 9, 2024
1 parent de40af0 commit 3a91bbe
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
5 changes: 5 additions & 0 deletions demo/src/app/views/root.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ const jetzig = @import("jetzig");
pub fn index(request: *jetzig.Request, data: *jetzig.Data) !jetzig.View {
var root = try data.object();
try root.put("message", data.string("Welcome to Jetzig!"));
try root.put("number", data.integer(customFunction(100, 200, 300)));

try request.response.headers.append("x-example-header", "example header value");

return request.render(.ok);
}

fn customFunction(a: i32, b: i32, c: i32) i32 {
return a + b + c;
}
13 changes: 13 additions & 0 deletions src/GenerateRoutes.zig
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,11 @@ fn parseFunction(
var args = std.ArrayList(Arg).init(self.allocator);
defer args.deinit();

if (!isActionFunctionName(function_name)) {
self.allocator.free(function_name);
return null;
}

while (it.next()) |arg| {
if (arg.name_token) |arg_token| {
const arg_name = self.ast.tokenSlice(arg_token);
Expand Down Expand Up @@ -478,3 +483,11 @@ fn parseTypeExpr(self: *Self, node: std.zig.Ast.Node) ![]const u8 {
fn asNodeIndex(index: usize) std.zig.Ast.Node.Index {
return @as(std.zig.Ast.Node.Index, @intCast(index));
}

fn isActionFunctionName(name: []const u8) bool {
inline for (@typeInfo(jetzig.views.Route.Action).Enum.fields) |field| {
if (std.mem.eql(u8, field.name, name)) return true;
}

return false;
}

0 comments on commit 3a91bbe

Please sign in to comment.