Skip to content

Commit 2c58c32

Browse files
authored
feat: support required fields (make the others optional) (#17)
* feat: support `required` fields (make the others optional) Signed-off-by: Edoardo Vacchi <evacchi@users.noreply.github.com> * handle nullable buffers Signed-off-by: Edoardo Vacchi <evacchi@users.noreply.github.com> --------- Signed-off-by: Edoardo Vacchi <evacchi@users.noreply.github.com>
1 parent 4d881b5 commit 2c58c32

File tree

2 files changed

+36
-14
lines changed

2 files changed

+36
-14
lines changed

src/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import ejs from "ejs";
2-
import { getContext, helpers, Property, XtpSchema } from "@dylibso/xtp-bindgen";
2+
import { getContext, helpers, Property, XtpSchema, Schema } from "@dylibso/xtp-bindgen";
33

44
function toZigType(property: Property, pkg?: string): string {
55
if (property.$ref) {
@@ -41,6 +41,10 @@ function pointerToZigType(property: Property) {
4141
return `*${typ}`;
4242
}
4343

44+
function isZigOptional(schema: Schema, property: Property) {
45+
return property.nullable || !schema.required?.includes(property.name);
46+
}
47+
4448
function addStdImport(schema: XtpSchema) {
4549
// in the generated `main.zig` this would include a reference to
4650
// std.json.ArrayHashMap and std.json.Value, so we import "std".
@@ -77,6 +81,7 @@ export function render() {
7781
const ctx = {
7882
...helpers,
7983
...getContext(),
84+
isZigOptional,
8085
toZigType,
8186
pointerToZigType,
8287
addStdImport,

template/src/schema.zig.ejs

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ pub const Host = struct {
9595
<% if (p.description) { -%>
9696
/// <%- formatCommentBlock(p.description, "/// ") %>
9797
<% } -%>
98-
<%- p.name %>: <%- p.nullable ? "?" : null %><%- toZigType(p) %><%- p.nullable ? " = null" : null %>,
98+
<%- p.name %>: <%- isZigOptional(schema, p) ? "?" : null %><%- toZigType(p) %><%- isZigOptional(schema, p) ? " = null" : null %>,
9999
<% }) %>
100100
101101
/// Internally used function, should not be called by plugin authors.
@@ -105,18 +105,28 @@ pub const Host = struct {
105105
<% } %>
106106
<% schema.properties.forEach(p => { -%>
107107
<% if (p.$ref && !p.$ref.enum) { %>
108-
<% if (p.nullable) { %>
108+
<% if (isZigOptional(schema, p)) { %>
109109
if (self.<%- p.name %> != null) {
110110
<% } -%>
111-
self.<%- p.name %> = (try self.<%- p.name %>.<%- p.nullable ? '?.' : null %>XXX__decodeBase64Fields()).*;
112-
<% if (p.nullable) { %>
111+
self.<%- p.name %> = (try self.<%- p.name %>.<%- isZigOptional(schema, p) ? '?.' : null %>XXX__decodeBase64Fields()).*;
112+
<% if (isZigOptional(schema, p)) { %>
113113
}
114114
<% } -%>
115115
<% } else if (p.type === 'buffer') { %>
116-
const dest_<%- p.name %> = try std.heap.wasm_allocator.alloc(u8, try b64dec.calcSizeForSlice(self.<%- p.name %>));
117-
try b64dec.decode(dest_<%- p.name %>, self.<%- p.name %>);
118-
self.<%- p.name %> = dest_<%- p.name %>;
119-
<% } %>
116+
<% if (isZigOptional(schema, p)) { %>
117+
if (self.aBuffer != null) {
118+
<% } /* end isZigOptional */ -%>
119+
120+
const srcBuf = self.<%- p.name %><%- isZigOptional(schema, p) ? '.?' : null %>;
121+
const dest_<%- p.name %> = try std.heap.wasm_allocator.alloc(u8, try b64dec.calcSizeForSlice(srcBuf));
122+
try b64dec.decode(dest_<%- p.name %>, srcBuf);
123+
self.<%- p.name %> = dest_<%- p.name %>;
124+
125+
<% if (isZigOptional(schema, p)) { %>
126+
}
127+
<% } /* end isZigOptional */ -%>
128+
129+
<% } /* end buffer */ %>
120130
<% }) %>
121131
122132
return self;
@@ -129,16 +139,23 @@ pub const Host = struct {
129139
<% } %>
130140
<% schema.properties.forEach(p => { -%>
131141
<% if (p.$ref && !p.$ref.enum) { %>
132-
<% if (p.nullable) { %>
142+
<% if (isZigOptional(schema, p)) { %>
133143
if (self.<%- p.name %> != null) {
134144
<% } -%>
135-
self.<%- p.name %> = (try self.<%- p.name %>.<%- p.nullable ? '?.' : null %>XXX__encodeBase64Fields()).*;
136-
<% if (p.nullable) { %>
145+
self.<%- p.name %> = (try self.<%- p.name %>.<%- isZigOptional(schema, p) ? '?.' : null %>XXX__encodeBase64Fields()).*;
146+
<% if (isZigOptional(schema, p)) { %>
137147
}
138148
<% } -%>
139149
<% } else if (p.type === 'buffer') { %>
140-
const dest_<%- p.name %> = try std.heap.wasm_allocator.alloc(u8, b64enc.calcSize(self.<%- p.name %>.len));
141-
self.<%- p.name %> = b64enc.encode(dest_<%- p.name %>, self.<%- p.name %>);
150+
<% if (isZigOptional(schema, p)) { %>
151+
if (self.<%- p.name %> != null) {
152+
<% } -%>
153+
const srcBuf = self.<%- p.name %><%- isZigOptional(schema, p) ? '.?' : null %>;
154+
const dest_<%- p.name %> = try std.heap.wasm_allocator.alloc(u8, b64enc.calcSize(srcBuf.len));
155+
self.<%- p.name %> = b64enc.encode(dest_<%- p.name %>, srcBuf);
156+
<% if (isZigOptional(schema, p)) { %>
157+
}
158+
<% } /* end isZigOptional */ -%>
142159
<% } %>
143160
<% }) %>
144161

0 commit comments

Comments
 (0)