Skip to content

Commit 4418c74

Browse files
committed
handle nullable buffers
Signed-off-by: Edoardo Vacchi <evacchi@users.noreply.github.com>
1 parent 797fdaa commit 4418c74

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

template/src/schema.zig.ejs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,20 @@ pub const Host = struct {
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;
@@ -137,8 +147,15 @@ pub const Host = struct {
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)