Skip to content

Commit

Permalink
feat: make generated code more idiomatic to go users (#16)
Browse files Browse the repository at this point in the history
* feat: make generated code more idiomatic to go users

* fix: apply conversion to missing spots

* chore: update to latest bindgen dep
  • Loading branch information
nilslice committed Sep 5, 2024
1 parent 3ee5aa4 commit 17e5059
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 36 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"
}
}
7 changes: 6 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import ejs from "ejs";
import { getContext, helpers, Property } from "@dylibso/xtp-bindgen";

function toGolangType(property: Property): string {
if (property.$ref) return property.$ref.name;
if (property.$ref) return goName(property.$ref.name);
switch (property.type) {
case "string":
if (property.format === "date-time") {
Expand Down Expand Up @@ -54,6 +54,10 @@ function makePublic(s: string) {
return pub;
}

function goName(s: string) {
return makePublic(helpers.snakeToCamelCase(s));
}

export function render() {
const tmpl = Host.inputString();
const ctx = {
Expand All @@ -62,6 +66,7 @@ export function render() {
toGolangType,
pointerToGolangType,
makePublic,
goName,
};

const output = ejs.render(tmpl, ctx);
Expand Down
2 changes: 1 addition & 1 deletion template/main.go.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ package main
// And returns <%- toGolangType(ex.output) %> (<%- formatCommentLine(ex.output.description) %>)
<% } -%>
<% -%>
func <%- ex.name %>(<%- ex.input ? `input ${toGolangType(ex.input)}` : null %>) <%- ex.output ? `(${toGolangType(ex.output)}, error)` : "error" %> {
func <%- goName(ex.name) %>(<%- ex.input ? `input ${toGolangType(ex.input)}` : null %>) <%- ex.output ? `(${toGolangType(ex.output)}, error)` : "error" %> {
<% if (featureFlags['stub-with-code-samples'] && codeSamples(ex, 'go').length > 0) { -%>
<%- codeSamples(ex, 'go')[0].source %>
<% } else { -%>
Expand Down
58 changes: 29 additions & 29 deletions template/pdk.gen.go.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import (

<% schema.exports.forEach(ex => { -%>
//export <%- ex.name %>
func _<%- ex.name %>() int32 {
func _<%- goName(ex.name) %>() int32 {
var err error
_ = err
<% if (ex.input) { -%>
<% if (ex.input.contentType === 'application/json') { -%>
pdk.Log(pdk.LogDebug, "<%- ex.name %>: getting JSON input")
pdk.Log(pdk.LogDebug, "<%- goName(ex.name) %>: getting JSON input")
var input <%- toGolangType(ex.input) %>
err = pdk.InputJSON(&input)
if err != nil {
Expand All @@ -24,40 +24,40 @@ func _<%- ex.name %>() int32 {
}
<% } else if (ex.input.type === 'string') { -%>
<% if (ex.input.$ref && ex.input.$ref.enum) { -%>
pdk.Log(pdk.LogDebug, "<%- ex.name %>: getting enum string input")
input, err := stringTo<%- ex.input.$ref.name %>(pdk.InputString())
pdk.Log(pdk.LogDebug, "<%- goName(ex.name) %>: getting enum string input")
input, err := stringTo<%- goName(ex.input.$ref.name) %>(pdk.InputString())
if err != nil {
pdk.SetError(err)
return -1
}
<% } else { -%>
pdk.Log(pdk.LogDebug, "<%- ex.name %>: getting string input")
pdk.Log(pdk.LogDebug, "<%- goName(ex.name) %>: getting string input")
input := pdk.InputString()
<% } %>
<% } else { -%>
pdk.Log(pdk.LogDebug, "<%- ex.name %>: getting bytes input")
pdk.Log(pdk.LogDebug, "<%- goName(ex.name) %>: getting bytes input")
input := pdk.Input()
<% } -%>
pdk.Log(pdk.LogDebug, "<%- ex.name %>: calling implementation function")
pdk.Log(pdk.LogDebug, "<%- goName(ex.name) %>: calling implementation function")
<% if (ex.output) { -%>
output, err := <%- ex.name %>(input)
output, err := <%- goName(ex.name) %>(input)
if err != nil {
pdk.SetError(err)
return -1
}
<% } else { -%>
err = <%- ex.name %>(input)
err = <%- goName(ex.name) %>(input)
if err != nil {
pdk.SetError(err)
return -1
}
<% } -%>
<% } else { -%>
<% if (ex.output) { -%>
output, err := <%- ex.name %>()
output, err := <%- goName(ex.name) %>()
<% } else { -%>
err = <%- ex.name %>()
err = <%- goName(ex.name) %>()
<% } -%>
if err != nil {
pdk.SetError(err)
Expand All @@ -67,22 +67,22 @@ func _<%- ex.name %>() int32 {
<% if (ex.output) { -%>
<% if (ex.output.contentType === 'application/json') { -%>
pdk.Log(pdk.LogDebug, "<%- ex.name %>: setting JSON output")
pdk.Log(pdk.LogDebug, "<%- goName(ex.name) %>: setting JSON output")
err = pdk.OutputJSON(output)
if err != nil {
pdk.SetError(err)
return -1
}
<% } else if (ex.output.type === 'string') { -%>
pdk.Log(pdk.LogDebug, "<%- ex.name %>: setting string output")
pdk.Log(pdk.LogDebug, "<%- goName(ex.name) %>: setting string output")
pdk.OutputString(output)
<% } else { -%>
pdk.Log(pdk.LogDebug, "<%- ex.name %>: setting bytes output")
pdk.Log(pdk.LogDebug, "<%- goName(ex.name) %>: setting bytes output")
pdk.Output(output)
<% } -%>
<% } -%>
pdk.Log(pdk.LogDebug, "<%- ex.name %>: returning")
pdk.Log(pdk.LogDebug, "<%- goName(ex.name) %>: returning")
return 0
}
Expand All @@ -91,7 +91,7 @@ func _<%- ex.name %>() int32 {
<% if (schema.imports.length > 0) { %>
<% Object.values(schema.imports).forEach(im => { %>
//go:wasmimport extism:host/user <%- im.name %>
func _<%- im.name %>(<%- im.input ? "uint64" : null %>) <%- im.output ? "uint64" : null %>
func _<%- goName(im.name) %>(<%- im.input ? "uint64" : null %>) <%- im.output ? "uint64" : null %>
<% }) %>
<% } %>
Expand All @@ -100,43 +100,43 @@ func _<%- ex.name %>() int32 {
<% if (schema.properties.length > 0) { %>
// <%- formatCommentBlock(schema.description, "// ") %>
type <%- schema.name %> struct {
type <%- goName(schema.name) %> struct {
<% schema.properties.forEach(p => { -%>
<% if (p.description) { -%>
// <%- formatCommentBlock(p.description, "// ") %>
<% } -%>
<%- makePublic(p.name) %> <%- p.nullable ? pointerToGolangType(p) : toGolangType(p) %> `json:"<%- p.name %>"`
<%- goName(p.name) %> <%- p.nullable ? pointerToGolangType(p) : toGolangType(p) %> `json:"<%- p.name %>"`
<% }) %>
}
<% } else if (schema.enum) { %>
// <%- formatCommentLine(schema.description) %>
type <%- makePublic(schema.name) %> string
type <%- goName(schema.name) %> string
const (
<% schema.enum.forEach((variant, i) => { -%>
<%- `${makePublic(schema.name)}${makePublic(variant)} ${makePublic(schema.name)} = "${variant}"` %>
<%- `${goName(schema.name)}${goName(variant)} ${goName(schema.name)} = "${variant}"` %>
<% }) -%>
)
func (v <%- makePublic(schema.name) %>) String() string {
func (v <%- goName(schema.name) %>) String() string {
switch (v) {
<% schema.enum.forEach((variant) => { -%>
case <%- `${makePublic(schema.name)}${makePublic(variant)}` %>:
case <%- `${goName(schema.name)}${goName(variant)}` %>:
return `<%- variant %>`
<% }) -%>
default:
return ""
}
}
func stringTo<%- makePublic(schema.name) %>(s string) (<%- makePublic(schema.name) %>, error) {
func stringTo<%- goName(schema.name) %>(s string) (<%- goName(schema.name) %>, error) {
switch (s) {
<% schema.enum.forEach((variant) => { -%>
case `<%- variant %>`:
return <%- `${makePublic(schema.name)}${makePublic(variant)}` %>, nil
return <%- `${goName(schema.name)}${goName(variant)}` %>, nil
<% }) -%>
default:
return <%- makePublic(schema.name) %>(""), errors.New("unable to convert string to <%- `${makePublic(schema.name)}` %>")
return <%- goName(schema.name) %>(""), errors.New("unable to convert string to <%- `${goName(schema.name)}` %>")
}
}
Expand All @@ -146,14 +146,14 @@ func _<%- ex.name %>() int32 {

<% schema.imports.forEach(imp => { %>
<% if (hasComment(imp)) -%>
// <%- makePublic(imp.name) %> <%- formatCommentBlock(imp.description, "// ") %>
// <%- goName(imp.name) %> <%- formatCommentBlock(imp.description, "// ") %>
<% if (hasComment(imp.input)) { -%>
// It takes input of <%- toGolangType(imp.input) %> (<%- formatCommentLine(imp.input.description) %>)
<% } -%>
<% if (hasComment(imp.output)) { -%>
// And it returns an output <%- toGolangType(imp.output) %> (<%- formatCommentLine(imp.output.description) %>)
<% } -%>
func <%- makePublic(imp.name) %>(<%- imp.input ? `input ${toGolangType(imp.input)}` : null %>) <%- imp.output ? `(${pointerToGolangType(imp.output)}, error)` : "error" %> {
func <%- goName(imp.name) %>(<%- imp.input ? `input ${toGolangType(imp.input)}` : null %>) <%- imp.output ? `(${pointerToGolangType(imp.output)}, error)` : "error" %> {
var err error
_ = err
<% if (imp.input) { -%>
Expand All @@ -172,9 +172,9 @@ func _<%- ex.name %>() int32 {
mem := pdk.AllocateBytes(input)
<% } -%>
<% if (imp.output) { -%>offs :=<% } -%> _<%- imp.name %>(mem.Offset())
<% if (imp.output) { -%>offs :=<% } -%> _<%- goName(imp.name) %>(mem.Offset())
<% } else { -%>
<% if (imp.output) { -%>offs :=<% } -%> _<%- imp.name %>()
<% if (imp.output) { -%>offs :=<% } -%> _<%- goName(imp.name) %>()
<% } -%>
<% if (imp.output) { -%>
Expand Down

0 comments on commit 17e5059

Please sign in to comment.