From 3882eba2f398be973a6a33b42ff75feb73876a27 Mon Sep 17 00:00:00 2001 From: Bob Farrell Date: Sun, 9 Jun 2024 11:13:33 +0100 Subject: [PATCH] Clean up static params and init script --- build.zig | 6 +++- cli/commands/init.zig | 7 ----- demo/.ruby-version | 1 + demo/build.zig | 9 +++--- demo/build.zig.zon | 4 --- demo/src/app/views/iguanas.zig | 45 --------------------------- demo/src/app/views/iguanas/index.zmpl | 7 ----- demo/src/app/views/static.zig | 4 +-- src/compile_static_routes.zig | 4 +-- src/jetzig/http/Server.zig | 35 ++++++++++----------- src/jetzig/testing.zig | 2 +- src/test_runner.zig | 4 +-- 12 files changed, 35 insertions(+), 93 deletions(-) create mode 100644 demo/.ruby-version delete mode 100644 demo/src/app/views/iguanas.zig delete mode 100644 demo/src/app/views/iguanas/index.zmpl diff --git a/build.zig b/build.zig index d174ef4..b4fb83b 100644 --- a/build.zig +++ b/build.zig @@ -203,6 +203,11 @@ pub fn jetzigInit(b: *std.Build, exe: *std.Build.Step.Compile, options: JetzigIn exe_static_routes.root_module.addImport("zmpl", zmpl_module); // exe_static_routes.root_module.addImport("jetzig_app", &exe.root_module); + const markdown_fragments_write_files = b.addWriteFiles(); + const path = markdown_fragments_write_files.add("markdown_fragments.zig", try generateMarkdownFragments(b)); + const markdown_fragments_module = b.createModule(.{ .root_source_file = path }); + exe_static_routes.root_module.addImport("markdown_fragments", markdown_fragments_module); + const run_static_routes_cmd = b.addRunArtifact(exe_static_routes); const static_outputs_path = run_static_routes_cmd.addOutputFileArg("static.zig"); const static_module = b.createModule(.{ .root_source_file = static_outputs_path }); @@ -218,7 +223,6 @@ pub fn jetzigInit(b: *std.Build, exe: *std.Build.Step.Compile, options: JetzigIn }); exe_unit_tests.root_module.addImport("jetzig", jetzig_module); exe_unit_tests.root_module.addImport("static", static_module); - exe_unit_tests.root_module.addImport("__jetzig_project", &exe.root_module); var it = exe.root_module.import_table.iterator(); while (it.next()) |import| { diff --git a/cli/commands/init.zig b/cli/commands/init.zig index a6758b0..87ace36 100644 --- a/cli/commands/init.zig +++ b/cli/commands/init.zig @@ -191,13 +191,6 @@ pub fn run( github_url, }); - try util.runCommand(allocator, realpath, &[_][]const u8{ - "zig", - "fetch", - "--save", - "https://github.com/jetzig-framework/iguanas/archive/89c2abf29de0bc31054a9a6feac5a6a83bab0459.tar.gz", - }); - // TODO: Use arg or interactive prompt to do Git setup in net project, default to no. // const git_setup = false; // if (git_setup) try gitSetup(allocator, install_dir); diff --git a/demo/.ruby-version b/demo/.ruby-version new file mode 100644 index 0000000..be94e6f --- /dev/null +++ b/demo/.ruby-version @@ -0,0 +1 @@ +3.2.2 diff --git a/demo/build.zig b/demo/build.zig index 289f83e..56ee763 100644 --- a/demo/build.zig +++ b/demo/build.zig @@ -12,11 +12,12 @@ pub fn build(b: *std.Build) !void { .optimize = optimize, }); - // Example dependency: - const iguanas_dep = b.dependency("iguanas", .{ .optimize = optimize, .target = target }); - exe.root_module.addImport("iguanas", iguanas_dep.module("iguanas")); + // Example Dependency + // ------------------- + // const iguanas_dep = b.dependency("iguanas", .{ .optimize = optimize, .target = target }); + // exe.root_module.addImport("iguanas", iguanas_dep.module("iguanas")); - // All dependencies **must** be added to imports above this line. + // ^ Add all dependencies before `jetzig.jetzigInit()` ^ try jetzig.jetzigInit(b, exe, .{}); diff --git a/demo/build.zig.zon b/demo/build.zig.zon index cf38161..321158c 100644 --- a/demo/build.zig.zon +++ b/demo/build.zig.zon @@ -6,10 +6,6 @@ .jetzig = .{ .path = "../", }, - .iguanas = .{ - .url = "https://github.com/jetzig-framework/iguanas/archive/045f34b3455fb5bbec8cbf81fdc5d19ee54bd625.tar.gz", - .hash = "12207f8cca149669f7c4bdc02103f6194189a312bbaae97b54e6dc3fb8483d6107bf", - }, }, .paths = .{ // This makes *all* files, recursively, included in this package. It is generally diff --git a/demo/src/app/views/iguanas.zig b/demo/src/app/views/iguanas.zig deleted file mode 100644 index 85b9ccf..0000000 --- a/demo/src/app/views/iguanas.zig +++ /dev/null @@ -1,45 +0,0 @@ -const std = @import("std"); -const jetzig = @import("jetzig"); -const iguanas = @import("iguanas"); - -/// This example uses a layout. A layout is a template that exists in `src/app/views/layouts` and -/// references `{zmpl.content}`. -/// -/// The content is the rendered template for the current view which is then injected into the -/// layout in place of `{zmpl.content}`. -/// -/// See `demo/src/app/views/layouts/application.zmpl` -/// and `demo/src/app/views/iguanas/index.zmpl` -pub const layout = "application"; - -pub fn index(request: *jetzig.Request, data: *jetzig.Data) !jetzig.View { - var root = try data.array(); - - const params = try request.params(); - - const count = params.getT(.integer, "iguanas") orelse 10; - - const iguanas_slice = try iguanas.iguanas(request.allocator, @intCast(count)); - - for (iguanas_slice) |iguana| { - try root.append(data.string(iguana)); - } - - return request.render(.ok); -} - -test "index" { - var app = try jetzig.testing.app(std.testing.allocator, @import("routes")); - defer app.deinit(); - - const response = try app.request(.GET, "/iguanas", .{ .json = .{ .iguanas = 10 } }); - try response.expectJson(".1", "iguana"); - try response.expectJson(".2", "iguana"); - try response.expectJson(".3", "iguana"); - try response.expectJson(".4", "iguana"); - try response.expectJson(".5", "iguana"); - try response.expectJson(".6", "iguana"); - try response.expectJson(".7", "iguana"); - try response.expectJson(".8", "iguana"); - try response.expectJson(".9", "iguana"); -} diff --git a/demo/src/app/views/iguanas/index.zmpl b/demo/src/app/views/iguanas/index.zmpl deleted file mode 100644 index c07004f..0000000 --- a/demo/src/app/views/iguanas/index.zmpl +++ /dev/null @@ -1,7 +0,0 @@ -
-@zig { - for (zmpl.items(.array)) |iguana| { -
{{iguana}}
- } -} -
diff --git a/demo/src/app/views/static.zig b/demo/src/app/views/static.zig index c2b2ec7..18b7130 100644 --- a/demo/src/app/views/static.zig +++ b/demo/src/app/views/static.zig @@ -64,8 +64,8 @@ pub fn get(id: []const u8, request: *jetzig.StaticRequest, data: *jetzig.Data) ! try root.put("message", "id is not '123'"); } - if (params.get("foo")) |foo| try root.put("foo", foo); - if (params.get("bar")) |bar| try root.put("bar", bar); + try root.put("foo", params.get("foo")); + try root.put("bar", params.get("bar")); return request.render(.created); } diff --git a/src/compile_static_routes.zig b/src/compile_static_routes.zig index da9bcec..ade3e3b 100644 --- a/src/compile_static_routes.zig +++ b/src/compile_static_routes.zig @@ -2,6 +2,7 @@ const std = @import("std"); const jetzig = @import("jetzig"); const routes = @import("routes").routes; const zmpl = @import("zmpl"); +const markdown_fragments = @import("markdown_fragments"); // const jetzig_options = @import("jetzig_app").jetzig_options; pub fn main() !void { @@ -114,10 +115,9 @@ fn renderMarkdown( route: jetzig.views.Route, view: jetzig.views.View, ) !?[]const u8 { - const fragments = null; const path = try std.mem.join(allocator, "/", &[_][]const u8{ route.uri_path, @tagName(route.action) }); defer allocator.free(path); - const content = try jetzig.markdown.render(allocator, path, fragments) orelse return null; + const content = try jetzig.markdown.render(allocator, path, markdown_fragments) orelse return null; if (route.layout) |layout_name| { try view.data.addConst("jetzig_view", view.data.string(route.name)); diff --git a/src/jetzig/http/Server.zig b/src/jetzig/http/Server.zig index 3d437fb..aa4387a 100644 --- a/src/jetzig/http/Server.zig +++ b/src/jetzig/http/Server.zig @@ -642,11 +642,6 @@ fn matchStaticContent(self: *Server, request: *jetzig.http.Request) !?[]const u8 .HTML, .UNKNOWN => static_output.output.html, .JSON => static_output.output.json, }; - } else { - return switch (request_format) { - .HTML, .UNKNOWN => static_output.output.html, - .JSON => static_output.output.json, - }; } } } @@ -659,34 +654,38 @@ fn matchStaticContent(self: *Server, request: *jetzig.http.Request) !?[]const u8 } pub fn decodeStaticParams(self: *Server) !void { + if (!@hasDecl(jetzig.root, "static")) return; + // Store decoded static params (i.e. declared in views) for faster comparison at request time. var decoded = std.ArrayList(*jetzig.data.Value).init(self.allocator); for (jetzig.root.static.compiled) |compiled| { - if (compiled.output.params) |params| { - const data = try self.allocator.create(jetzig.data.Data); - data.* = jetzig.data.Data.init(self.allocator); - try data.fromJson(params); - try decoded.append(data.value.?); - } + const data = try self.allocator.create(jetzig.data.Data); + data.* = jetzig.data.Data.init(self.allocator); + try data.fromJson(compiled.output.params orelse "{}"); + try decoded.append(data.value.?); } self.decoded_static_route_params = try decoded.toOwnedSlice(); } fn matchStaticOutput( - maybe_id: ?[]const u8, - maybe_params: ?*jetzig.data.Value, + maybe_expected_id: ?[]const u8, + maybe_expected_params: ?*jetzig.data.Value, route: jetzig.views.Route, request: *const jetzig.http.Request, params: *jetzig.data.Value, ) bool { - return if (maybe_params) |expected_params| blk: { + return if (maybe_expected_params) |expected_params| blk: { + const params_match = expected_params.count() == 0 or expected_params.eql(params); break :blk switch (route.action) { - .index, .post => expected_params.count() == 0 or expected_params.eql(params), - inline else => if (maybe_id) |id| - std.mem.eql(u8, id, request.path.resource_id) and expected_params.eql(params) + .index, .post => params_match, + inline else => if (maybe_expected_id) |expected_id| + std.mem.eql(u8, expected_id, request.path.resource_id) and params_match else false, }; - } else if (maybe_id) |id| std.mem.eql(u8, id, request.path.resource_id) else maybe_params == null; + } else if (maybe_expected_id) |id| + std.mem.eql(u8, id, request.path.resource_id) + else + true; // We reached a params filter (possibly the default catch-all) with no params set. } diff --git a/src/jetzig/testing.zig b/src/jetzig/testing.zig index d1c2459..368db2c 100644 --- a/src/jetzig/testing.zig +++ b/src/jetzig/testing.zig @@ -222,7 +222,7 @@ pub fn expectJson(expected_path: []const u8, expected_value: anytype, response: switch (@typeInfo(@TypeOf(expected_value))) { .Pointer, .Array => { logFailure( - "Expected \"" ++ jetzig.colors.red("{s}") ++ "\" in " ++ jetzig.colors.cyan("{s}") ++ ", found \"" ++ jetzig.colors.green("{s}") ++ "\nJSON:" ++ json_banner, + "Expected \"" ++ jetzig.colors.red("{s}") ++ "\" in " ++ jetzig.colors.cyan("{s}") ++ ", found \"" ++ jetzig.colors.green("{s}") ++ "\"\nJSON:" ++ json_banner, .{ expected_value, expected_path, string.value, try jsonPretty(response) }, ); }, diff --git a/src/test_runner.zig b/src/test_runner.zig index ddd3cf4..6f9711b 100644 --- a/src/test_runner.zig +++ b/src/test_runner.zig @@ -37,7 +37,7 @@ const Test = struct { trace: ?[]const u8, }; - const name_template = jetzig.colors.blue("{s}") ++ ":" ++ jetzig.colors.cyan("{s}") ++ " "; + const name_template = jetzig.colors.blue("{s}") ++ jetzig.colors.yellow("->") ++ "\"" ++ jetzig.colors.cyan("{s}") ++ "\" "; pub fn init(test_fn: std.builtin.TestFn) Test { return if (std.mem.indexOf(u8, test_fn.name, ".test.")) |index| @@ -117,7 +117,7 @@ const Test = struct { fn printFailureDetail(self: Test, index: usize, failure: Failure, writer: anytype) !void { try writer.print("\n", .{}); - const count = " FAILURE: ".len + (self.module orelse "tests").len + ":".len + self.name.len + 1; + const count = " FAILURE: ".len + (self.module orelse "tests").len + ":".len + self.name.len + 4; try writer.writeAll(jetzig.colors.red("┌")); for (0..count) |_| try writer.writeAll(jetzig.colors.red("─"));