Skip to content

Commit

Permalink
fix: handle more input/output combinations (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
nilslice authored Aug 26, 2024
1 parent c843319 commit 20046fa
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
15 changes: 14 additions & 1 deletion template/src/pdk.zig.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -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') { -%>
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion template/src/schema.zig.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -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" %>;
<% }) -%>
<% } -%>
};
Expand Down

0 comments on commit 20046fa

Please sign in to comment.