Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import ejs from "ejs";
import { getContext, helpers, Property, XtpSchema } from "@dylibso/xtp-bindgen";
import { getContext, helpers, Property, XtpSchema, Schema } from "@dylibso/xtp-bindgen";

function toZigType(property: Property, pkg?: string): string {
if (property.$ref) {
Expand Down Expand Up @@ -41,6 +41,10 @@ function pointerToZigType(property: Property) {
return `*${typ}`;
}

function isZigOptional(schema: Schema, property: Property) {
return property.nullable || !schema.required?.includes(property.name);
}

function addStdImport(schema: XtpSchema) {
// in the generated `main.zig` this would include a reference to
// std.json.ArrayHashMap and std.json.Value, so we import "std".
Expand Down Expand Up @@ -77,6 +81,7 @@ export function render() {
const ctx = {
...helpers,
...getContext(),
isZigOptional,
toZigType,
pointerToZigType,
addStdImport,
Expand Down
43 changes: 30 additions & 13 deletions template/src/schema.zig.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ pub const Host = struct {
<% if (p.description) { -%>
/// <%- formatCommentBlock(p.description, "/// ") %>
<% } -%>
<%- p.name %>: <%- p.nullable ? "?" : null %><%- toZigType(p) %><%- p.nullable ? " = null" : null %>,
<%- p.name %>: <%- isZigOptional(schema, p) ? "?" : null %><%- toZigType(p) %><%- isZigOptional(schema, p) ? " = null" : null %>,
<% }) %>

/// Internally used function, should not be called by plugin authors.
Expand All @@ -105,18 +105,28 @@ pub const Host = struct {
<% } %>
<% schema.properties.forEach(p => { -%>
<% if (p.$ref && !p.$ref.enum) { %>
<% if (p.nullable) { %>
<% if (isZigOptional(schema, p)) { %>
if (self.<%- p.name %> != null) {
<% } -%>
self.<%- p.name %> = (try self.<%- p.name %>.<%- p.nullable ? '?.' : null %>XXX__decodeBase64Fields()).*;
<% if (p.nullable) { %>
self.<%- p.name %> = (try self.<%- p.name %>.<%- isZigOptional(schema, p) ? '?.' : null %>XXX__decodeBase64Fields()).*;
<% if (isZigOptional(schema, p)) { %>
}
<% } -%>
<% } 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 %>);
self.<%- p.name %> = dest_<%- p.name %>;
<% } %>
<% if (isZigOptional(schema, p)) { %>
if (self.aBuffer != null) {
<% } /* end isZigOptional */ -%>

const srcBuf = self.<%- p.name %><%- isZigOptional(schema, p) ? '.?' : null %>;
const dest_<%- p.name %> = try std.heap.wasm_allocator.alloc(u8, try b64dec.calcSizeForSlice(srcBuf));
try b64dec.decode(dest_<%- p.name %>, srcBuf);
self.<%- p.name %> = dest_<%- p.name %>;

<% if (isZigOptional(schema, p)) { %>
}
<% } /* end isZigOptional */ -%>

<% } /* end buffer */ %>
<% }) %>

return self;
Expand All @@ -129,16 +139,23 @@ pub const Host = struct {
<% } %>
<% schema.properties.forEach(p => { -%>
<% if (p.$ref && !p.$ref.enum) { %>
<% if (p.nullable) { %>
<% if (isZigOptional(schema, p)) { %>
if (self.<%- p.name %> != null) {
<% } -%>
self.<%- p.name %> = (try self.<%- p.name %>.<%- p.nullable ? '?.' : null %>XXX__encodeBase64Fields()).*;
<% if (p.nullable) { %>
self.<%- p.name %> = (try self.<%- p.name %>.<%- isZigOptional(schema, p) ? '?.' : null %>XXX__encodeBase64Fields()).*;
<% if (isZigOptional(schema, p)) { %>
}
<% } -%>
<% } else if (p.type === 'buffer') { %>
Comment thread
evacchi marked this conversation as resolved.
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 %>);
<% if (isZigOptional(schema, p)) { %>
if (self.<%- p.name %> != null) {
<% } -%>
const srcBuf = self.<%- p.name %><%- isZigOptional(schema, p) ? '.?' : null %>;
const dest_<%- p.name %> = try std.heap.wasm_allocator.alloc(u8, b64enc.calcSize(srcBuf.len));
self.<%- p.name %> = b64enc.encode(dest_<%- p.name %>, srcBuf);
<% if (isZigOptional(schema, p)) { %>
}
<% } /* end isZigOptional */ -%>
<% } %>
<% }) %>

Expand Down