Skip to content

Commit

Permalink
Merge pull request #122 from jetzig-framework/pseudo-http-verbs-and-e…
Browse files Browse the repository at this point in the history
…dit-action

Routing updates
  • Loading branch information
bobf authored Nov 24, 2024
2 parents 835885a + f44a6b3 commit 937efd9
Show file tree
Hide file tree
Showing 9 changed files with 186 additions and 65 deletions.
13 changes: 5 additions & 8 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
.hash = "1220d0e8734628fd910a73146e804d10a3269e3e7d065de6bb0e3e88d5ba234eb163",
},
.zmpl = .{
.url = "https://github.com/jetzig-framework/zmpl/archive/af75c8b842c3957eb97b4fc4bc49c7b2243968fa.tar.gz",
.hash = "1220ecac93d295dafd2f034a86f0979f6108d40e5ea1a39e3a2b9977c35147cac684",
.url = "https://github.com/jetzig-framework/zmpl/archive/ef1930b08e1f174ddb02a3a0a01b35aa8a4af235.tar.gz",
.hash = "1220a7bacb828f12cd013b0906da61a17fac6819ab8cee81e00d9ae1aa0faa992720",
},
.jetkv = .{
.url = "https://github.com/jetzig-framework/jetkv/archive/2b1130a48979ea2871c8cf6ca89c38b1e7062839.tar.gz",
.hash = "12201d75d73aad5e1c996de4d5ae87a00e58479c8d469bc2eeb5fdeeac8857bc09af",
},
.jetquery = .{
.url = "https://github.com/jetzig-framework/jetquery/archive/a31db467c4af1c97bc7c806e1cc1a81a39162954.tar.gz",
.hash = "12203af0466ccc3a9ab57fcdf57c92c57989fa7e827d81bc98d0a5787d65402c73c3",
.url = "https://github.com/jetzig-framework/jetquery/archive/5394d7cf5d7360bd5052cd13902e26f08610423b.tar.gz",
.hash = "1220119a1ee89d8d4b7e984a82bc70fe5d57aa412b821c561ce80a93fd8806bc4b8a",
},
.jetcommon = .{
.url = "https://github.com/jetzig-framework/jetcommon/archive/86f24cfdf2aaa0e8ada4539a6edef882708ced2b.tar.gz",
Expand All @@ -26,10 +26,7 @@
.url = "https://github.com/ikskuh/zig-args/archive/0abdd6947a70e6d8cc83b66228cea614aa856206.tar.gz",
.hash = "1220411a8c46d95bbf3b6e2059854bcb3c5159d428814099df5294232b9980517e9c",
},
.pg = .{
.url = "https://github.com/karlseguin/pg.zig/archive/f376f4b30c63f1fdf90bc3afe246d3bc4175cd46.tar.gz",
.hash = "12200a55304988e942015b6244570b2dc0e87e5764719c9e7d5c812cd7ad34f6b138"
},
.pg = .{ .url = "https://github.com/karlseguin/pg.zig/archive/f376f4b30c63f1fdf90bc3afe246d3bc4175cd46.tar.gz", .hash = "12200a55304988e942015b6244570b2dc0e87e5764719c9e7d5c812cd7ad34f6b138" },
.smtp_client = .{
.url = "https://github.com/karlseguin/smtp_client.zig/archive/3cbe8f269e4c3a6bce407e7ae48b2c76307c559f.tar.gz",
.hash = "1220de146446d0cae4396e346cb8283dd5e086491f8577ddbd5e03ad0928111d8bc6",
Expand Down
17 changes: 9 additions & 8 deletions cli/commands/generate/view.zig
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub fn run(allocator: std.mem.Allocator, cwd: std.fs.Dir, args: [][]const u8, he
const action_args = if (args.len > 1)
args[1..]
else
&[_][]const u8{ "index", "get", "new", "post", "put", "patch", "delete" };
&[_][]const u8{ "index", "get", "new", "edit", "post", "put", "patch", "delete" };

var actions = std.ArrayList(Action).init(allocator);
defer actions.deinit();
Expand Down Expand Up @@ -92,7 +92,7 @@ pub fn run(allocator: std.mem.Allocator, cwd: std.fs.Dir, args: [][]const u8, he
std.debug.print("Generated view: {s}\n", .{realpath});
}

const Method = enum { index, get, new, post, put, patch, delete };
const Method = enum { index, get, new, edit, post, put, patch, delete };
const Action = struct {
method: Method,
static: bool,
Expand Down Expand Up @@ -126,15 +126,15 @@ fn writeAction(allocator: std.mem.Allocator, writer: anytype, action: Action) !v
@tagName(action.method),
switch (action.method) {
.index, .post, .new => "",
.get, .put, .patch, .delete => "id: []const u8, ",
.get, .edit, .put, .patch, .delete => "id: []const u8, ",
},
if (action.static) "StaticRequest" else "Request",
switch (action.method) {
.index, .post, .new => "",
.get, .put, .patch, .delete => "_ = id;\n ",
.get, .edit, .put, .patch, .delete => "_ = id;\n ",
},
switch (action.method) {
.index, .get, .new => ".ok",
.index, .get, .edit, .new => ".ok",
.post => ".created",
.put, .patch, .delete => ".ok",
},
Expand Down Expand Up @@ -164,17 +164,18 @@ fn writeTest(allocator: std.mem.Allocator, writer: anytype, name: []const u8, ac
.{
@tagName(action.method),
switch (action.method) {
.index, .get, .new => "GET",
.index, .get, .edit, .new => "GET",
.put, .patch, .delete, .post => action_upper,
},
name,
switch (action.method) {
.index, .post => "",
.edit => "/example-id/edit",
.new => "/new",
.get, .put, .patch, .delete => "/example-id",
},
switch (action.method) {
.index, .get, .new => ".ok",
.index, .get, .new, .edit => ".ok",
.post => ".created",
.put, .patch, .delete => ".ok",
},
Expand Down Expand Up @@ -208,7 +209,7 @@ fn writeStaticParams(allocator: std.mem.Allocator, actions: []Action, writer: an
defer allocator.free(output);
try writer.writeAll(output);
},
.get, .put, .patch, .delete => {
.get, .put, .patch, .delete, .edit => {
const output = try std.fmt.allocPrint(
allocator,
\\ .{s} = .{{
Expand Down
6 changes: 6 additions & 0 deletions demo/src/app/views/root.zig
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ pub fn index(request: *jetzig.Request, data: *jetzig.Data) !jetzig.View {
return request.render(.ok);
}

pub fn edit(id: []const u8, request: *jetzig.Request) !jetzig.View {
var root = try request.data(.object);
try root.put("id", id);
return request.render(.ok);
}

fn customFunction(a: i32, b: i32, c: i32) i32 {
return a + b + c;
}
Expand Down
5 changes: 5 additions & 0 deletions demo/src/app/views/session.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ pub fn index(request: *jetzig.Request, data: *jetzig.Data) !jetzig.View {
return request.render(.ok);
}

pub fn edit(id: []const u8, request: *jetzig.Request) !jetzig.View {
try request.server.logger.INFO("id: {s}", .{id});
return request.render(.ok);
}

pub fn post(request: *jetzig.Request, data: *jetzig.Data) !jetzig.View {
_ = data;
const params = try request.params();
Expand Down
17 changes: 14 additions & 3 deletions src/Routes.zig
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,20 @@ const Function = struct {
defer self.routes.allocator.free(relative_path);

const path = relative_path[0 .. relative_path.len - std.fs.path.extension(relative_path).len];
if (std.mem.eql(u8, path, "root")) return try self.routes.allocator.dupe(u8, "/");
const is_root = std.mem.eql(u8, path, "root");
const is_new = std.mem.eql(u8, self.name, "new");
const is_edit = std.mem.eql(u8, self.name, "edit");
if (is_root) {
if (is_edit) return try self.routes.allocator.dupe(u8, "/edit");
if (is_new) return try self.routes.allocator.dupe(u8, "/new");
return try self.routes.allocator.dupe(u8, "/");
}

const maybe_new = if (is_new) ("/new") else "";
// jetzig.http.Path.actionPath translates `/foo/bar/1/edit` to `/foo/bar/edit`
const maybe_edit = if (is_edit) ("/edit") else "";

const maybe_new = if (std.mem.eql(u8, self.name, "new")) "/new" else "";
return try std.mem.concat(self.routes.allocator, u8, &[_][]const u8{ "/", path, maybe_new });
return try std.mem.concat(self.routes.allocator, u8, &[_][]const u8{ "/", path, maybe_new, maybe_edit });
}

pub fn lessThanFn(context: void, lhs: Function, rhs: Function) bool {
Expand Down Expand Up @@ -305,6 +315,7 @@ fn writeRoute(self: *Routes, writer: std.ArrayList(u8).Writer, route: Function)
.{ "index", false },
.{ "post", false },
.{ "new", false },
.{ "edit", true },
.{ "get", true },
.{ "edit", true },
.{ "put", true },
Expand Down
Loading

0 comments on commit 937efd9

Please sign in to comment.