mirror of
https://github.com/jetzig-framework/jetzig.git
synced 2025-05-14 22:16:08 +00:00
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:
parent
de40af0dc6
commit
3a91bbea48
@ -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;
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user