Skip to content

Commit

Permalink
fix: handle object type json
Browse files Browse the repository at this point in the history
  • Loading branch information
nilslice committed Aug 22, 2024
1 parent b33ecb1 commit a843985
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function toZigType(property: Property, pkg?: string): string {
case "boolean":
return "bool";
case "object":
return "std.json.Value";
return "std.json.ArrayHashMap(std.json.Value)";
case "array":
if (!property.items) return "[]std.json.Value";
// TODO this is not quite right to force cast
Expand Down
31 changes: 23 additions & 8 deletions template/src/pdk.zig.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,29 @@ export fn <%- ex.name %>() i32 {
// Get the input data
<% if (ex.input.contentType === 'application/json') { -%>
// in JSON
const jsonInput = _plugin.getJsonOpt(schema.<%- ex.input.$ref.name %>, .{}) catch |err| {
const msg = std.fmt.allocPrint(_plugin.allocator, "{}", .{err}) catch ERR_PRINTING_MSG;
_plugin.setError(msg);
return -1;
};
defer jsonInput.deinit();
<% if (ex.input.$ref) { -%>
const json_input = _plugin.getJsonOpt(schema.<%- ex.input.$ref.name %>, .{}) catch |err| {
const msg = std.fmt.allocPrint(_plugin.allocator, "{}", .{err}) catch ERR_PRINTING_MSG;
_plugin.setError(msg);
return -1;
};
defer json_input.deinit();
const input = json_input.value();
<% } else if (ex.input.type === 'object') { -%>
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(std.json.ArrayHashMap(std.json.Value), _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;
<% } -%>
const input = jsonInput.value();
<% } else if (ex.input.type === 'string') { -%>
<% if (ex.input.$ref && ex.input.$ref.enum) { -%>
// as a string Enum
Expand All @@ -31,7 +46,7 @@ export fn <%- ex.name %>() i32 {
const input = std.enums.nameCast(<%- ex.input.$ref.name %>, s);
<% } else { -%>
// as a string
const input = _plugin.getInput() catch |err| {
const input = _plugin.getInput() catch |err| {
const msg = std.fmt.allocPrint(_plugin.allocator, "{}", .{err}) catch ERR_PRINTING_MSG;
_plugin.setError(msg);
return -1;
Expand Down

0 comments on commit a843985

Please sign in to comment.