Skip to content

Commit

Permalink
fix: more base64 padding alignment, handle enums, check optionals
Browse files Browse the repository at this point in the history
  • Loading branch information
nilslice committed Sep 12, 2024
1 parent a6db9b4 commit ddf847c
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 49 deletions.
104 changes: 59 additions & 45 deletions template/src/pdk.zig.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,19 @@ export fn <%- ex.name %>() i32 {
};
defer json_input.deinit();
var input = json_input.value();
// decode all the inner buffer fields from base64 (may be no-op)
input = (input.__decodeBase64Fields() catch |err| {
const msg = std.fmt.allocPrint(_plugin.allocator, "{}", .{err}) catch ERR_PRINTING_MSG;
_plugin.setError(msg);
return -1;
}).*;
<% if (!ex.input.$ref.enum) { %>
var input = json_input.value();
// decode all the inner buffer fields from base64 (may be no-op)
input = (input.XXX__decodeBase64Fields() catch |err| {
const msg = std.fmt.allocPrint(_plugin.allocator, "{}", .{err}) catch ERR_PRINTING_MSG;
_plugin.setError(msg);
return -1;
}).*;
<% } %>
<% } else if (ex.input.type === 'buffer') { -%>
var input = json_input.value();
var b64dec = std.base64.standard_no_pad.Decoder;
var b64dec = std.base64.standard.Decoder;
const n = b64dec.calcSizeForSlice(input) catch |err| {
const msg = std.fmt.allocPrint(_plugin.allocator, "{}", .{err}) catch ERR_PRINTING_MSG;
_plugin.setError(msg);
Expand Down Expand Up @@ -105,6 +107,9 @@ export fn <%- ex.name %>() i32 {
<% } -%>
// Call the implementation function
<% if (ex.input.$ref && ex.input.$ref.enum) { -%>
const input = json_input.value();
<% } -%>
<% if (ex.output) { -%>
const output = user.<%- zigFuncName(ex.name) %>(<% if (ex.input) { %>input<% } %>) catch |err| {
const msg = std.fmt.allocPrint(_plugin.allocator, "{}", .{err}) catch ERR_PRINTING_MSG;
Expand Down Expand Up @@ -136,45 +141,54 @@ export fn <%- ex.name %>() i32 {
<% if (ex.output) { -%>
<% if (ex.output.contentType === 'application/json') { -%>
var json_output = output;
<% if (ex.output.$ref) { -%>
// encode all the inner buffer fields to base64 (may be no-op)
json_output = (json_output.__encodeBase64Fields() catch |err| {
const msg = std.fmt.allocPrint(_plugin.allocator, "{}", .{err}) catch ERR_PRINTING_MSG;
_plugin.setError(msg);
return -1;
}).*;
<% } else if (ex.output.type === 'buffer') { -%>
var b64enc = std.base64.standard_no_pad.Encoder;
const out_n = b64enc.calcSize(json_output.len) catch |err| {
const msg = std.fmt.allocPrint(_plugin.allocator, "{}", .{err}) catch ERR_PRINTING_MSG;
_plugin.setError(msg);
return -1;
};
const out_dest = _plugin.allocator.alloc(u8, out_n) catch |err| {
const msg = std.fmt.allocPrint(_plugin.allocator, "{}", .{err}) catch ERR_PRINTING_MSG;
_plugin.setError(msg);
return -1;
};
b64enc.encode(out_dest, json_output) catch |err| {
const msg = std.fmt.allocPrint(_plugin.allocator, "{}", .{err}) catch ERR_PRINTING_MSG;
_plugin.setError(msg);
<% if (ex.output.$ref && !ex.output.$ref.enum) { -%>
var json_output = output;
// encode all the inner buffer fields to base64 (may be no-op)
json_output = (json_output.XXX__encodeBase64Fields() catch |err| {
const msg = std.fmt.allocPrint(_plugin.allocator, "{}", .{err}) catch ERR_PRINTING_MSG;
_plugin.setError(msg);
return -1;
}).*;
_plugin.outputJson(json_output, .{}) catch |err| {
const msg = std.fmt.allocPrint(_plugin.allocator, "{}", .{err}) catch ERR_PRINTING_MSG;
_plugin.setError(msg);
return -1;
};
<% } else if (ex.output.type === 'buffer') { -%>
var json_output = output;
var b64enc = std.base64.standard.Encoder;
const out_n = b64enc.calcSize(json_output.len) catch |err| {
const msg = std.fmt.allocPrint(_plugin.allocator, "{}", .{err}) catch ERR_PRINTING_MSG;
_plugin.setError(msg);
return -1;
};
const out_dest = _plugin.allocator.alloc(u8, out_n) catch |err| {
const msg = std.fmt.allocPrint(_plugin.allocator, "{}", .{err}) catch ERR_PRINTING_MSG;
_plugin.setError(msg);
return -1;
};
json_output = b64enc.encode(out_dest, json_output) catch |err| {
const msg = std.fmt.allocPrint(_plugin.allocator, "{}", .{err}) catch ERR_PRINTING_MSG;
_plugin.setError(msg);
return -1;
};
_plugin.outputJson(json_output, .{}) catch |err| {
const msg = std.fmt.allocPrint(_plugin.allocator, "{}", .{err}) catch ERR_PRINTING_MSG;
_plugin.setError(msg);
return -1;
};
<% } else { %>
_plugin.outputJson(output, .{}) catch |err| {
const msg = std.fmt.allocPrint(_plugin.allocator, "{}", .{err}) catch ERR_PRINTING_MSG;
_plugin.setError(msg);
return -1;
};
<% } -%>
<% } else if (ex.output.contentType === 'text/plain; charset=UTF-8') { -%>
if (!std.unicode.utf8ValidateSlice(output)) {
_plugin.setError("output is not valud UTF8");
return -1;
};
json_output = out_dest;
<% } -%>
_plugin.outputJson(json_output, .{}) catch |err| {
const msg = std.fmt.allocPrint(_plugin.allocator, "{}", .{err}) catch ERR_PRINTING_MSG;
_plugin.setError(msg);
return -1;
};
<% } else if (ex.output.type === 'text/plain; charset=UTF-8') { -%>
if (!std.unicode.utf8ValidateSlice(input)) {
return error.InvalidUtf8;
}
_plugin.output(output);
<% } else if (ex.output.type === 'string') { -%>
_plugin.output(output);
<% } else { -%>
_plugin.output(output);
<% } -%>
Expand Down
10 changes: 6 additions & 4 deletions template/src/schema.zig.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,14 @@ pub const Host = struct {
<%- p.name %>: <%- p.nullable ? "?" : null %><%- toZigType(p) %>,
<% }) %>
pub fn __decodeBase64Fields(self: *<%- zigTypeName(schema.name) %>) !*<%- zigTypeName(schema.name) %> {
/// Internally used function, should not be called by plugin authors.
pub fn XXX__decodeBase64Fields(self: *<%- zigTypeName(schema.name) %>) !*<%- zigTypeName(schema.name) %> {
<% if (schema.properties.some(p => { return p.type === 'buffer' })) { -%>
var b64dec = std.base64.standard.Decoder;
<% } %>
<% schema.properties.forEach(p => { -%>
<% if (p.$ref && !p.$ref.enum) { %>
self.<%- p.name %> = (try self.<%- p.name %>.__decodeBase64Fields()).*;
self.<%- p.name %> = (try self.<%- p.name %>.<%- p.nullable ? '?.' : null %>XXX__decodeBase64Fields()).*;
<% } else if (p.type === 'buffer') { %>
const dest_<%- p.name %> = try std.heap.wasm_allocator.alloc(u8, try b64dec.calcSizeForSlice(self.<%- p.name %>));
try b64dec.decode(dest_<%- p.name %>, self.<%- p.name %>);
Expand All @@ -115,13 +116,14 @@ pub const Host = struct {
return self;
}
pub fn __encodeBase64Fields(self: *<%- zigTypeName(schema.name) %>) !*<%- zigTypeName(schema.name) %> {
/// Internally used function, should not be called by plugin authors.
pub fn XXX__encodeBase64Fields(self: *<%- zigTypeName(schema.name) %>) !*<%- zigTypeName(schema.name) %> {
<% if (schema.properties.some(p => { return p.type === 'buffer' })) { -%>
var b64enc = std.base64.standard.Encoder;
<% } %>
<% schema.properties.forEach(p => { -%>
<% if (p.$ref && !p.$ref.enum) { %>
self.<%- p.name %> = (try self.<%- p.name %>.__encodeBase64Fields()).*;
self.<%- p.name %> = (try self.<%- p.name %>.<%- p.nullable ? '?.' : null %>XXX__encodeBase64Fields()).*;
<% } else if (p.type === 'buffer') { %>
const dest_<%- p.name %> = try std.heap.wasm_allocator.alloc(u8, b64enc.calcSize(self.<%- p.name %>.len));
self.<%- p.name %> = b64enc.encode(dest_<%- p.name %>, self.<%- p.name %>);
Expand Down

0 comments on commit ddf847c

Please sign in to comment.