Fix custom routes

Fix CLI view generator
This commit is contained in:
Bob Farrell 2024-11-17 19:32:46 +00:00
parent b95506caf9
commit 78b6938530
3 changed files with 29 additions and 8 deletions

View File

@ -117,7 +117,7 @@ fn writeAction(allocator: std.mem.Allocator, writer: anytype, action: Action) !v
const function = try std.fmt.allocPrint( const function = try std.fmt.allocPrint(
allocator, allocator,
\\pub fn {s}({s}request: *jetzig.{s}) !jetzig.View {{ \\pub fn {s}({s}request: *jetzig.{s}) !jetzig.View {{
\\ return request.render({s}); \\ {s}return request.render({s});
\\}} \\}}
\\ \\
\\ \\
@ -131,7 +131,7 @@ fn writeAction(allocator: std.mem.Allocator, writer: anytype, action: Action) !v
if (action.static) "StaticRequest" else "Request", if (action.static) "StaticRequest" else "Request",
switch (action.method) { switch (action.method) {
.index, .post, .new => "", .index, .post, .new => "",
.get, .put, .patch, .delete => "\n _ = id;", .get, .put, .patch, .delete => "_ = id;\n ",
}, },
switch (action.method) { switch (action.method) {
.index, .get, .new => ".ok", .index, .get, .new => ".ok",

View File

@ -166,6 +166,9 @@ pub fn route(
@memcpy(&view_name, module_name); @memcpy(&view_name, module_name);
std.mem.replaceScalar(u8, &view_name, '.', '/'); std.mem.replaceScalar(u8, &view_name, '.', '/');
const args_fields = std.meta.fields(std.meta.ArgsTuple(@TypeOf(viewFn)));
const legacy = args_fields.len > 0 and args_fields[args_fields.len - 1].type == *jetzig.Data;
self.custom_routes.append(.{ self.custom_routes.append(.{
.id = "custom", .id = "custom",
.name = member, .name = member,
@ -175,9 +178,18 @@ pub fn route(
.uri_path = path, .uri_path = path,
.layout = if (@hasDecl(module, "layout")) module.layout else null, .layout = if (@hasDecl(module, "layout")) module.layout else null,
.view = comptime switch (viewType(path)) { .view = comptime switch (viewType(path)) {
.with_id => .{ .custom = .{ .with_id = viewFn } }, .with_id => if (legacy)
.with_args => .{ .custom = .{ .with_args = viewFn } }, .{ .legacy_with_id = viewFn }
.without_id => .{ .custom = .{ .without_id = viewFn } }, else
.{ .with_id = viewFn },
.without_id => if (legacy)
.{ .legacy_without_id = viewFn }
else
.{ .without_id = viewFn },
.with_args => if (legacy)
.{ .legacy_with_args = viewFn }
else
.{ .with_args = viewFn },
}, },
.template = self.allocator.dupe(u8, &template) catch @panic("OOM"), .template = self.allocator.dupe(u8, &template) catch @panic("OOM"),
.json_params = &.{}, .json_params = &.{},

View File

@ -129,15 +129,24 @@ pub fn validateFormat(self: Route, request: *const jetzig.http.Request) bool {
fn renderFn(self: Route, request: *jetzig.http.Request) anyerror!jetzig.views.View { fn renderFn(self: Route, request: *jetzig.http.Request) anyerror!jetzig.views.View {
return switch (self.view) { return switch (self.view) {
.without_id => |func| try func(request),
.legacy_without_id => |func| try func(request, request.response_data),
.with_id => |func| try func(request.path.resource_id, request), .with_id => |func| try func(request.path.resource_id, request),
.without_id => |func| try func(request),
.with_args => |func| try func(
try request.path.resourceArgs(self, request.allocator),
request,
),
.legacy_with_id => |func| try func( .legacy_with_id => |func| try func(
request.path.resource_id, request.path.resource_id,
request, request,
request.response_data, request.response_data,
), ),
else => unreachable, .legacy_without_id => |func| try func(request, request.response_data),
.legacy_with_args => |func| try func(
try request.path.resourceArgs(self, request.allocator),
request,
request.response_data,
),
else => unreachable, // renderStaticFn is called for static routes, we can never get here.
}; };
} }