Skip to content

Commit

Permalink
feat: make generated code more idiomatic to zig users
Browse files Browse the repository at this point in the history
  • Loading branch information
nilslice committed Sep 5, 2024
1 parent 20046fa commit 08aa1b0
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 16 deletions.
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.5",
"@dylibso/xtp-bindgen": "1.0.0-rc.7",
"ejs": "^3.1.10"
}
}
19 changes: 16 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import ejs from "ejs";
import { getContext, helpers, Property, XtpSchema } from "@dylibso/xtp-bindgen";

function toZigType(property: Property, pkg?: string): string {
if (property.$ref) return (pkg ? `${pkg}.` : "") + property.$ref.name;
if (property.$ref) {
return (pkg ? `${pkg}.` : "") + zigTypeName(property.$ref.name);
}
switch (property.type) {
case "string":
if (property.format === "date-time") {
Expand Down Expand Up @@ -54,7 +56,16 @@ function addStdImport(schema: XtpSchema) {
: null;
}

function makeStructName(s: string) {
function zigFuncName(s: string) {
return helpers.snakeToCamelCase(s);
}

function zigVarName(s: string) {
return helpers.camelToSnakeCase(s);
}

function zigTypeName(s: string) {
s = helpers.snakeToCamelCase(s);
const cap = s.charAt(0).toUpperCase();
if (s.charAt(0) === cap) {
return s;
Expand All @@ -71,8 +82,10 @@ export function render() {
...getContext(),
toZigType,
pointerToZigType,
makeStructName,
addStdImport,
zigTypeName,
zigFuncName,
zigVarName,
};

const output = ejs.render(tmpl, ctx);
Expand Down
2 changes: 1 addition & 1 deletion template/src/main.zig.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const schema = @import("schema.zig");
/// And returns <%- toZigType(ex.output) %> (<%- formatCommentLine(ex.output.description) %>)
<% } -%>
<% -%>
pub fn <%- 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, "schema")}` : null %>) !<%- ex.output ? `${toZigType(ex.output, "schema")}` : "void" %> {
<% if (featureFlags['stub-with-code-samples'] && codeSamples(ex, 'zig').length > 0) { -%>
<%- codeSamples(ex, 'zig')[0].source %>
<% } else { -%>
Expand Down
8 changes: 4 additions & 4 deletions template/src/pdk.zig.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -78,27 +78,27 @@ export fn <%- ex.name %>() i32 {
// Call the implementation function
<% if (ex.output) { -%>
const output = user.<%- ex.name %>(<% if (ex.input) { %>input<% } %>) catch |err| {
const output = user.<%- zigFuncName(ex.name) %>(<% if (ex.input) { %>input<% } %>) catch |err| {
const msg = std.fmt.allocPrint(_plugin.allocator, "{}", .{err}) catch ERR_PRINTING_MSG;
_plugin.setError(msg);
return -1;
};
<% } else { -%>
user.<%- ex.name %>(input) catch |err| {
user.<%- zigFuncName(ex.name) %>(input) catch |err| {
const msg = std.fmt.allocPrint(_plugin.allocator, "{}", .{err}) catch ERR_PRINTING_MSG;
_plugin.setError(msg);
return -1;
};
<% } -%>
<% } else { -%>
<% if (ex.output) { -%>
const output = user.<%- ex.name %>() catch |err| {
const output = user.<%- zigFuncName(ex.name) %>() catch |err| {
const msg = std.fmt.allocPrint(_plugin.allocator, "{}", .{err}) catch ERR_PRINTING_MSG;
_plugin.setError(msg);
return -1;
};
<% } else { -%>
user.<%- ex.name %>() catch |err| {
user.<%- zigFuncName(ex.name) %>() catch |err| {
const msg = std.fmt.allocPrint(_plugin.allocator, "{}", .{err}) catch ERR_PRINTING_MSG;
_plugin.setError(msg);
return -1;
Expand Down
6 changes: 3 additions & 3 deletions template/src/schema.zig.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub const Host = struct {
<% if (imp.output && hasComment(imp.output)) { -%>
/// And it returns an output <%- toZigType(imp.output) %> (<%- formatCommentLine(imp.output.description) %>)
<% } -%>
pub fn <%- 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)}` : null %>) !<%- imp.output ? `${toZigType(imp.output)}` : "void" %> {
<% if (imp.input) { -%>
<% if (imp.input.contentType === 'application/json') { -%>
const b = try std.json.stringifyAlloc(_plugin.allocator, input, .{});
Expand Down Expand Up @@ -90,7 +90,7 @@ pub const Host = struct {
<% Object.values(schema.schemas).forEach(schema => { %>
<% if (schema.properties.length > 0) { -%>
/// <%- formatCommentBlock(schema.description, "/// ") %>
pub const <%- makeStructName(schema.name) %> = struct {
pub const <%- zigTypeName(schema.name) %> = struct {
<% schema.properties.forEach(p => { -%>
<% if (p.description) { -%>
/// <%- formatCommentBlock(p.description, "/// ") %>
Expand All @@ -102,7 +102,7 @@ pub const Host = struct {
<% if (schema.description) { -%>
/// <%- formatCommentBlock(schema.description, "/// ") %>
<% } -%>
pub const <%- makeStructName(schema.name) %> = enum {
pub const <%- zigTypeName(schema.name) %> = enum {
<% schema.enum.forEach((variant) => { -%>
<%- variant %>,
<% }) -%>
Expand Down

0 comments on commit 08aa1b0

Please sign in to comment.