Skip to content
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

feat: refactor to use latest xtp-bindgen #13

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"typescript": "^5.3.2"
},
"dependencies": {
"@dylibso/xtp-bindgen": "1.0.0-rc.8",
"@dylibso/xtp-bindgen": "1.0.0-rc.16",
"ejs": "^3.1.10"
}
}
69 changes: 43 additions & 26 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,60 @@
import ejs from "ejs";
import { getContext, helpers, Property, XtpSchema } from "@dylibso/xtp-bindgen";
import {
ArrayType,
EnumType,
getContext,
helpers,
ObjectType,
XtpNormalizedType,
XtpSchema,
} from "@dylibso/xtp-bindgen";

function toZigType(property: Property, pkg?: string): string {
if (property.$ref) {
return (pkg ? `${pkg}.` : "") + zigTypeName(property.$ref.name);
}
switch (property.type) {
function toZigType(type: XtpNormalizedType, pkg?: string): string {
switch (type.kind) {
case "date-time":
return "DateTime";
case "string":
if (property.format === "date-time") {
return "DateTime";
}
return "[]const u8";
case "number":
if (property.format === "float") {
return "f64";
}
if (property.format === "double") {
return "f64";
}
return "i64";
case "integer":
case "float":
return "f64";
case "double":
return "f64";
case "int32":
return "i32";
case "int64":
return "i64";
case "boolean":
return "bool";
case "buffer":
return "[]const u8";
case "byte":
return "u8";

case "object":
const oType = type as ObjectType;
if (oType.properties?.length > 0) {
return (pkg ? `${pkg}.` : "") + zigTypeName(oType.name);
}
return "std.json.ArrayHashMap(std.json.Value)";

case "array":
if (!property.items) return "[]std.json.Value";
// TODO this is not quite right to force cast
return `[]${toZigType(property.items as Property)}`;
case "buffer":
return "[]const u8";
const arrayType = type as ArrayType;
if (!arrayType.elementType) {
return "[]std.json.Value";
}
return `[]${toZigType(arrayType.elementType)}`;

case "enum":
const eType = type as EnumType;
return zigTypeName(eType.name);

default:
throw new Error("Can't convert property to Zig type: " + property.type);
throw new Error("Can't convert property to Zig type: " + type.kind);
}
}

function pointerToZigType(property: Property) {
const typ = toZigType(property);
function pointerToZigType(type: XtpNormalizedType) {
const typ = toZigType(type);
return `*${typ}`;
}

Expand Down
6 changes: 3 additions & 3 deletions template/src/main.zig.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ const schema = @import("schema.zig");
<% if (hasComment(ex)) -%>
/// <%- formatCommentBlock(ex.description, "/// ") %>
<% if (ex.input && hasComment(ex.input)) { -%>
/// It takes <%- toZigType(ex.input) %> as input (<%- formatCommentLine(ex.input.description) %>)
/// It takes <%- toZigType(ex.input.xtpType) %> as input (<%- formatCommentLine(ex.input.description) %>)
<% } -%>
<% if (ex.output && hasComment(ex.output)) { -%>
/// And returns <%- toZigType(ex.output) %> (<%- formatCommentLine(ex.output.description) %>)
/// And returns <%- toZigType(ex.output.xtpType) %> (<%- formatCommentLine(ex.output.description) %>)
<% } -%>
<% -%>
pub fn <%- zigFuncName(ex.name) %>(<%- ex.input ? `input: ${toZigType(ex.input, "schema")}` : null %>) !<%- ex.output ? `${toZigType(ex.output, "schema")}` : "void" %> {
pub fn <%- zigFuncName(ex.name) %>(<%- ex.input ? `input: ${toZigType(ex.input.xtpType, "schema")}` : null %>) !<%- ex.output ? `${toZigType(ex.output.xtpType, "schema")}` : "void" %> {
<% if (featureFlags['stub-with-code-samples'] && codeSamples(ex, 'zig').length > 0) { -%>
<%- codeSamples(ex, 'zig')[0].source %>
<% } else { -%>
Expand Down
31 changes: 16 additions & 15 deletions template/src/pdk.zig.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ const ERR_PRINTING_MSG: []const u8 = "std.fmt.allocPrint failed when formatting
export fn <%- ex.name %>() i32 {
<% if (ex.input) { -%>
// Get the input data
<% if (ex.input.contentType === 'application/json') { -%>
<% if (isJsonEncoded(ex.input)) { -%>
// in JSON
<% if (ex.input.$ref) { -%>
const json_input = _plugin.getJsonOpt(schema.<%- zigTypeName(ex.input.$ref.name) %>, .{}) catch |err| {
<% if (isObject(ex.input)) { -%>
const json_input = _plugin.getJsonOpt(schema.<%- zigTypeName(ex.input.xtpType.name) %>, .{}) catch |err| {
const msg = std.fmt.allocPrint(_plugin.allocator, "{}", .{err}) catch ERR_PRINTING_MSG;
_plugin.setError(msg);
return -1;
};
defer json_input.deinit();

<% if (!ex.input.$ref.enum) { %>
<% if (!isEnum(ex.input)) { %>
var input = json_input.value();
// decode all the inner buffer fields from base64 (may be no-op)
input = (input.XXX__decodeBase64Fields() catch |err| {
Expand All @@ -33,7 +33,7 @@ export fn <%- ex.name %>() i32 {
}).*;
<% } %>

<% } else if (ex.input.type === 'buffer') { -%>
<% } else if (ex.input.xtpType.type === 'buffer') { -%>
var input = json_input.value();
var b64dec = std.base64.standard.Decoder;
const n = b64dec.calcSizeForSlice(input) catch |err| {
Expand All @@ -53,7 +53,7 @@ export fn <%- ex.name %>() i32 {
};
input = dest;

<% } else if (ex.input.type === 'object') { -%>
<% } else if (isObject(ex.input)) { -%>
const s = _plugin.getInput() catch |err| {
const msg = std.fmt.allocPrint(_plugin.allocator, "{}", .{err}) catch ERR_PRINTING_MSG;
_plugin.setError(msg);
Expand All @@ -73,20 +73,20 @@ export fn <%- ex.name %>() i32 {
return -1;
};
defer _plugin.allocator.free(s);
const parsed_input = std.json.parseFromSlice(<%- toZigType(ex.input) %>, _plugin.allocator, s, .{ .allocate = .alloc_always }) catch |err| {
const parsed_input = std.json.parseFromSlice(<%- toZigType(ex.input.xtpType) %>, _plugin.allocator, s, .{ .allocate = .alloc_always }) catch |err| {
const msg = std.fmt.allocPrint(_plugin.allocator, "{}", .{err}) catch ERR_PRINTING_MSG;
_plugin.setError(msg);
return -1;
};
const input = parsed_input.value;
<% } -%>

<% } else if (ex.input.type === 'string') { -%>
<% if (ex.input.$ref && ex.input.$ref.enum) { -%>
<% } else if (isString(ex.input)) { -%>
<% if (isEnum(ex.input)) { -%>
// as a string Enum
const s = _plugin.getInput();
defer _plugin.allocator.free(s);
const input = std.enums.nameCast(<%- ex.input.$ref.name %>, s);
const input = std.enums.nameCast(<%- ex.input.xtpType.$ref.name %>, s);
<% } else { -%>
// as a string
const input = _plugin.getInput() catch |err| {
Expand All @@ -107,7 +107,7 @@ export fn <%- ex.name %>() i32 {
<% } -%>

// Call the implementation function
<% if (ex.input.$ref && ex.input.$ref.enum) { -%>
<% if (isEnum(ex.input)) { -%>
const input = json_input.value();
<% } -%>
<% if (ex.output) { -%>
Expand Down Expand Up @@ -140,8 +140,8 @@ export fn <%- ex.name %>() i32 {
<% } -%>

<% if (ex.output) { -%>
<% if (ex.output.contentType === 'application/json') { -%>
<% if (ex.output.$ref && !ex.output.$ref.enum) { -%>
<% if (isJsonEncoded(ex.output)) { -%>
<% if (!isEnum(ex.output)) { -%>
var json_output = output;
// encode all the inner buffer fields to base64 (may be no-op)
json_output = (json_output.XXX__encodeBase64Fields() catch |err| {
Expand All @@ -154,7 +154,7 @@ export fn <%- ex.name %>() i32 {
_plugin.setError(msg);
return -1;
};
<% } else if (ex.output.type === 'buffer') { -%>
<% } else if (ex.output.xtpType.type === 'buffer') { -%>
var json_output = output;
var b64enc = std.base64.standard.Encoder;
const out_n = b64enc.calcSize(json_output.len) catch |err| {
Expand Down Expand Up @@ -184,11 +184,12 @@ export fn <%- ex.name %>() i32 {
return -1;
};
<% } -%>
<% } else if (ex.output.contentType === 'text/plain; charset=UTF-8') { -%>
<% } else if (isUtf8Encoded(ex.output)) { -%>
if (!std.unicode.utf8ValidateSlice(output)) {
_plugin.setError("output is not valud UTF8");
return -1;
}
_plugin.output(output);
<% } else { -%>
_plugin.output(output);
<% } -%>
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 @@ -23,12 +23,12 @@ pub const Host = struct {
<% if (hasComment(imp)) -%>
/// <%- formatCommentBlock(imp.description, "/// ") %>
<% if (imp.input && hasComment(imp.input)) { -%>
/// It takes input of <%- toZigType(imp.input) %> (<%- formatCommentLine(imp.input.description) %>)
/// It takes input of <%- toZigType(imp.input.xtpType) %> (<%- formatCommentLine(imp.input.description) %>)
<% } -%>
<% if (imp.output && hasComment(imp.output)) { -%>
/// And it returns an output <%- toZigType(imp.output) %> (<%- formatCommentLine(imp.output.description) %>)
/// And it returns an output <%- toZigType(imp.output.xtpType) %> (<%- formatCommentLine(imp.output.description) %>)
<% } -%>
pub fn <%- zigFuncName(imp.name) %>(<%- imp.input ? `input: ${toZigType(imp.input)}` : null %>) !<%- imp.output ? `${toZigType(imp.output)}` : "void" %> {
pub fn <%- zigFuncName(imp.name) %>(<%- imp.input ? `input: ${toZigType(imp.input.xtpType)}` : null %>) !<%- imp.output ? `${toZigType(imp.output.xtpType)}` : "void" %> {
<% if (imp.input) { -%>
<% if (imp.input.contentType === 'application/json') { -%>
const b = try std.json.stringifyAlloc(_plugin.allocator, input, .{});
Expand Down Expand Up @@ -63,7 +63,7 @@ pub const Host = struct {
<% if (imp.output.contentType === 'application/json') { -%>
const buffer = try _plugin.allocator.alloc(u8, @intCast(outMem.length));
outMem.load(buffer);
const out = try std.json.parseFromSlice(<%- toZigType(imp.output) %>, _plugin.allocator, buffer, .{ .allocate = .alloc_always, .ignore_unknown_fields = true });
const out = try std.json.parseFromSlice(<%- toZigType(imp.output.xtpType) %>, _plugin.allocator, buffer, .{ .allocate = .alloc_always, .ignore_unknown_fields = true });
return out.value;
<% } else if (imp.output.contentType === 'text/plain; charset=UTF-8') { -%>
const buffer = try _plugin.allocator.alloc(u8, @intCast(outMem.length));
Expand All @@ -88,14 +88,14 @@ pub const Host = struct {
<% } -%>

<% Object.values(schema.schemas).forEach(schema => { %>
<% if (schema.properties.length > 0) { -%>
<% if (isObject(schema)) { -%>
/// <%- formatCommentBlock(schema.description, "/// ") %>
pub const <%- zigTypeName(schema.name) %> = struct {
<% schema.properties.forEach(p => { -%>
<% if (p.description) { -%>
/// <%- formatCommentBlock(p.description, "/// ") %>
<% } -%>
<%- p.name %>: <%- p.nullable ? "?" : null %><%- toZigType(p) %><%- p.nullable ? " = null" : null %>,
<%- p.name %>: <%- p.nullable ? "?" : null %><%- toZigType(p.xtpType) %><%- p.nullable ? " = null" : null %>,
<% }) %>

/// Internally used function, should not be called by plugin authors.
Expand Down Expand Up @@ -145,7 +145,7 @@ pub const Host = struct {
return self;
}
};
<% } else if (schema.enum) { %>
<% } else if (isEnum(schema)) { %>
<% if (schema.description) { -%>
/// <%- formatCommentBlock(schema.description, "/// ") %>
<% } -%>
Expand Down
Loading