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).
This commit is contained in:
Bob Farrell 2024-03-09 11:43:44 +00:00
parent de40af0dc6
commit 3a91bbea48
2 changed files with 18 additions and 0 deletions

View File

@ -3,8 +3,13 @@ const jetzig = @import("jetzig");
pub fn index(request: *jetzig.Request, data: *jetzig.Data) !jetzig.View { pub fn index(request: *jetzig.Request, data: *jetzig.Data) !jetzig.View {
var root = try data.object(); var root = try data.object();
try root.put("message", data.string("Welcome to Jetzig!")); 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"); try request.response.headers.append("x-example-header", "example header value");
return request.render(.ok); return request.render(.ok);
} }
fn customFunction(a: i32, b: i32, c: i32) i32 {
return a + b + c;
}

View File

@ -430,6 +430,11 @@ fn parseFunction(
var args = std.ArrayList(Arg).init(self.allocator); var args = std.ArrayList(Arg).init(self.allocator);
defer args.deinit(); defer args.deinit();
if (!isActionFunctionName(function_name)) {
self.allocator.free(function_name);
return null;
}
while (it.next()) |arg| { while (it.next()) |arg| {
if (arg.name_token) |arg_token| { if (arg.name_token) |arg_token| {
const arg_name = self.ast.tokenSlice(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 { fn asNodeIndex(index: usize) std.zig.Ast.Node.Index {
return @as(std.zig.Ast.Node.Index, @intCast(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;
}