Skip to content

feat: support required fields (make the others optional) #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 8, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 7 additions & 7 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,11 +105,11 @@ 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') { %>
Expand All @@ -129,11 +129,11 @@ 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') { %>
Expand Down
Loading