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: make generated code more idiomatic to zig users #3

Merged
merged 5 commits into from
Sep 9, 2024
Merged
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
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ jobs:

- name: Install Zig
uses: goto-bus-stop/setup-zig@v2
with:
version: 0.13.0

- name: Check Zig Version
run: zig version
Expand Down
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
14 changes: 14 additions & 0 deletions tests/schemas/fruit.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
exports:
snake_case_func_name:
description: This is a function that uses snake case in the name
input:
type: string
contentType: text/plain; charset=utf-8
description: A string passed into plugin input
output:
$ref: "#/components/schemas/some_value"
contentType: application/json
voidFunc:
description: "This demonstrates how you can create an export with\nno inputs or
outputs. \n"
Expand Down Expand Up @@ -69,6 +78,11 @@ imports:
version: v1-draft
components:
schemas:
some_value:
properties:
a_string:
type: string
description: a string
WriteParams:
properties:
key:
Expand Down
Loading