From a843985abe47e24465def36d358bf63946a8b9c0 Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Thu, 22 Aug 2024 09:53:28 -0600 Subject: [PATCH] fix: handle object type json --- src/index.ts | 2 +- template/src/pdk.zig.ejs | 31 +++++++++++++++++++++++-------- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/src/index.ts b/src/index.ts index df8ed7e..08f9130 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 diff --git a/template/src/pdk.zig.ejs b/template/src/pdk.zig.ejs index 4cc0282..1f67c96 100644 --- a/template/src/pdk.zig.ejs +++ b/template/src/pdk.zig.ejs @@ -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 @@ -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;