From 20046fad0c05778e1daf566468e7160f82b47d85 Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Mon, 26 Aug 2024 16:14:03 -0600 Subject: [PATCH] fix: handle more input/output combinations (#2) --- template/src/pdk.zig.ejs | 15 ++++++++++++++- template/src/schema.zig.ejs | 2 +- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/template/src/pdk.zig.ejs b/template/src/pdk.zig.ejs index 1f67c96..272ad13 100644 --- a/template/src/pdk.zig.ejs +++ b/template/src/pdk.zig.ejs @@ -36,6 +36,19 @@ export fn <%- ex.name %>() i32 { return -1; }; const input = parsed_input.value; + <% } else { -%> + const s = _plugin.getInput() catch |err| { + const msg = std.fmt.allocPrint(_plugin.allocator, "{}", .{err}) catch ERR_PRINTING_MSG; + _plugin.setError(msg); + return -1; + }; + defer _plugin.allocator.free(s); + const parsed_input = std.json.parseFromSlice(<%- toZigType(ex.input) %>, _plugin.allocator, s, .{ .allocate = .alloc_always }) catch |err| { + const msg = std.fmt.allocPrint(_plugin.allocator, "{}", .{err}) catch ERR_PRINTING_MSG; + _plugin.setError(msg); + return -1; + }; + const input = parsed_input.value; <% } -%> <% } else if (ex.input.type === 'string') { -%> @@ -65,7 +78,7 @@ export fn <%- ex.name %>() i32 { // Call the implementation function <% if (ex.output) { -%> - const output = user.<%- ex.name %>(input) catch |err| { + const output = user.<%- ex.name %>(<% if (ex.input) { %>input<% } %>) catch |err| { const msg = std.fmt.allocPrint(_plugin.allocator, "{}", .{err}) catch ERR_PRINTING_MSG; _plugin.setError(msg); return -1; diff --git a/template/src/schema.zig.ejs b/template/src/schema.zig.ejs index 200f9d0..dcff675 100644 --- a/template/src/schema.zig.ejs +++ b/template/src/schema.zig.ejs @@ -8,7 +8,7 @@ const DateTime = @import("datetime").DateTime; const _host = struct { <% if (schema.imports.length > 0) { -%> <% Object.values(schema.imports).forEach(imp => { -%> - extern "extism:host/user" fn <%- imp.name %>(u64) <%- imp.output ? "u64" : "void" %>; + extern "extism:host/user" fn <%- imp.name %>(<%- imp.input ? "u64" : null %>) <%- imp.output ? "u64" : "void" %>; <% }) -%> <% } -%> };