From 3a91bbea489c91a72ec3673ec80480512a10fcf8 Mon Sep 17 00:00:00 2001 From: Bob Farrell Date: Sat, 9 Mar 2024 11:43:44 +0000 Subject: [PATCH] Skip parsing functions that are not in Route.Action enum 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). --- demo/src/app/views/root.zig | 5 +++++ src/GenerateRoutes.zig | 13 +++++++++++++ 2 files changed, 18 insertions(+) diff --git a/demo/src/app/views/root.zig b/demo/src/app/views/root.zig index e0ade83..1353b9e 100644 --- a/demo/src/app/views/root.zig +++ b/demo/src/app/views/root.zig @@ -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; +} diff --git a/src/GenerateRoutes.zig b/src/GenerateRoutes.zig index 3ebbf6e..119f116 100644 --- a/src/GenerateRoutes.zig +++ b/src/GenerateRoutes.zig @@ -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); @@ -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; +}