From 08aa1b0efa068d1f80f38461c61bf9eb830c96d6 Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Thu, 5 Sep 2024 11:38:08 -0600 Subject: [PATCH 1/5] feat: make generated code more idiomatic to zig users --- package-lock.json | 8 ++++---- package.json | 2 +- src/index.ts | 19 ++++++++++++++++--- template/src/main.zig.ejs | 2 +- template/src/pdk.zig.ejs | 8 ++++---- template/src/schema.zig.ejs | 6 +++--- 6 files changed, 29 insertions(+), 16 deletions(-) diff --git a/package-lock.json b/package-lock.json index 5e4a4c0..5a0d921 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,7 @@ "version": "0.0.1", "license": "BSD-3-Clause", "dependencies": { - "@dylibso/xtp-bindgen": "1.0.0-rc.5", + "@dylibso/xtp-bindgen": "1.0.0-rc.7", "ejs": "^3.1.10" }, "devDependencies": { @@ -21,9 +21,9 @@ } }, "node_modules/@dylibso/xtp-bindgen": { - "version": "1.0.0-rc.5", - "resolved": "https://registry.npmjs.org/@dylibso/xtp-bindgen/-/xtp-bindgen-1.0.0-rc.5.tgz", - "integrity": "sha512-F+s0XA5NeS7q6ikWe7Qou8AsLqYJfCQQbFEpgzWsqIh3YoLF7jcEwgc3Df60FbRocitOf/ZRlCxgaqJlAuD73w==" + "version": "1.0.0-rc.7", + "resolved": "https://registry.npmjs.org/@dylibso/xtp-bindgen/-/xtp-bindgen-1.0.0-rc.7.tgz", + "integrity": "sha512-8nMT8xqsC6FYVT2tS4xB3R+VVYAfiu6AceZLlRjfB2SCE5H6YqgX0INU9ZL9IacvFr+mRjEoQZ6B+G4Y/WR9WQ==" }, "node_modules/@esbuild/aix-ppc64": { "version": "0.19.12", diff --git a/package.json b/package.json index a8abcf3..df40610 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/src/index.ts b/src/index.ts index 1239970..d6ddd48 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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") { @@ -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; @@ -71,8 +82,10 @@ export function render() { ...getContext(), toZigType, pointerToZigType, - makeStructName, addStdImport, + zigTypeName, + zigFuncName, + zigVarName, }; const output = ejs.render(tmpl, ctx); diff --git a/template/src/main.zig.ejs b/template/src/main.zig.ejs index 6a41f57..e4b92d1 100644 --- a/template/src/main.zig.ejs +++ b/template/src/main.zig.ejs @@ -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 { -%> diff --git a/template/src/pdk.zig.ejs b/template/src/pdk.zig.ejs index 272ad13..02e673c 100644 --- a/template/src/pdk.zig.ejs +++ b/template/src/pdk.zig.ejs @@ -78,13 +78,13 @@ 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; @@ -92,13 +92,13 @@ export fn <%- ex.name %>() i32 { <% } -%> <% } 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; diff --git a/template/src/schema.zig.ejs b/template/src/schema.zig.ejs index dcff675..8f6834b 100644 --- a/template/src/schema.zig.ejs +++ b/template/src/schema.zig.ejs @@ -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, .{}); @@ -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, "/// ") %> @@ -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 %>, <% }) -%> From d3aebcbfa9a7e9556ce93fa2a310f225d25aae5d Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Mon, 9 Sep 2024 09:50:10 -0600 Subject: [PATCH 2/5] test: include more naming types --- tests/schemas/fruit.yaml | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/schemas/fruit.yaml b/tests/schemas/fruit.yaml index bdf0d1f..740ddbd 100644 --- a/tests/schemas/fruit.yaml +++ b/tests/schemas/fruit.yaml @@ -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" @@ -69,6 +78,11 @@ imports: version: v1-draft components: schemas: + some_value: + properties: + a_string: + type: string + description: a string WriteParams: properties: key: From 23276021a16b6c414e26bfab2784b31cf30fa023 Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Mon, 9 Sep 2024 09:57:29 -0600 Subject: [PATCH 3/5] ci: tesh.sh temp use zig build alone --- tests/test.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test.sh b/tests/test.sh index 96e554a..dcc71cf 100755 --- a/tests/test.sh +++ b/tests/test.sh @@ -11,6 +11,7 @@ for file in ./schemas/*.yaml; do rm -rf output xtp plugin init --schema-file $file --template ../bundle --path output -y --feature stub-with-code-samples --name output cd output - xtp plugin build + # xtp plugin build + zig build cd .. done From 5441859e0ac0a7d5cb77b51a0a8422bd3bf8ba8a Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Mon, 9 Sep 2024 10:01:48 -0600 Subject: [PATCH 4/5] ci: pin zig version to 0.13.0 --- .github/workflows/test.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 41f1699..8ab4fad 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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 From 434992eb25b79ada8bb98b35417bb4f31c94d8cb Mon Sep 17 00:00:00 2001 From: Steve Manuel Date: Mon, 9 Sep 2024 11:01:12 -0600 Subject: [PATCH 5/5] test: revert tool use --- tests/test.sh | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/test.sh b/tests/test.sh index dcc71cf..96e554a 100755 --- a/tests/test.sh +++ b/tests/test.sh @@ -11,7 +11,6 @@ for file in ./schemas/*.yaml; do rm -rf output xtp plugin init --schema-file $file --template ../bundle --path output -y --feature stub-with-code-samples --name output cd output - # xtp plugin build - zig build + xtp plugin build cd .. done