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

fix: nested types, use dataclass_wizard to deserialize/serialize types #21

Merged
merged 7 commits into from
Jan 3, 2025
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: 1 addition & 1 deletion bindgen-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ case $1 in
echo "building '$PLUGIN_NAME'..."
xtp plugin build --path $PLUGIN_NAME
echo "testing '$PLUGIN_NAME'..."
xtp plugin test $PLUGIN_NAME/plugin.wasm --with test.wasm --mock-host mock.wasm
EXTISM_ENABLE_WASI_OUTPUT=1 xtp plugin test $PLUGIN_NAME/plugin.wasm --with test.wasm --mock-host mock.wasm
;;
esac
14 changes: 12 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
EnumType,
ArrayType,
XtpNormalizedType,
XtpTyped
XtpTyped,
Parameter
} from "@dylibso/xtp-bindgen"

function pythonTypeName(s: string): string {
Expand Down Expand Up @@ -78,6 +79,14 @@ function toPythonParamType(property: XtpTyped, required?: boolean): string {
return t;
}

function toPythonHostParamType(param: Parameter): string {
if (param.contentType === 'application/x-binary') {
return 'bytes'
} else {
return 'str'
}
}

export function render() {
const tmpl = Host.inputString();
const ctx = {
Expand All @@ -86,7 +95,8 @@ export function render() {
toPythonType,
toPythonParamType,
pythonTypeName,
pythonFunctionName
pythonFunctionName,
toPythonHostParamType
};

const output = ejs.render(tmpl, ctx);
Expand Down
32 changes: 27 additions & 5 deletions template/plugin/__init__.py.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from typing import Optional, List # noqa: F401
from datetime import datetime # noqa: F401
import json
import extism # pyright: ignore
import plugin

Expand All @@ -21,7 +22,7 @@ from pdk_types import <%- Object.values(schema.schemas).map(schema => pythonType
# And it returns an output <%- toPythonParamType(imp.output) %> (<%- formatCommentLine(imp.output.description) %>)
<% } -%>
@extism.import_fn("extism:host/user", "<%- imp.name %>")
def <%- pythonFunctionName(imp.name) %>(<% if (imp.input) { -%>input: <%- toPythonParamType(imp.input) %><%} -%>) <% if (imp.output) { %> -> <%- toPythonParamType(imp.output) %><% } %>: # pyright: ignore [reportReturnType]
def _<%- pythonFunctionName(imp.name) %>(<% if (imp.input) { -%>input: <%- toPythonHostParamType(imp.input) %> <%} -%>) <% if (imp.output) { %> -> <%- toPythonHostParamType(imp.output) %> <% } %>: # pyright: ignore [reportReturnType]
pass
<% }) %>

Expand All @@ -34,7 +35,28 @@ def <%- pythonFunctionName(imp.name) %>(<% if (imp.input) { -%>input: <%- toPyth
<% } -%>
@extism.plugin_fn
def <%- ex.name %>():
res = plugin.<%- pythonFunctionName(ex.name) %>(<% if (ex.input) { %> extism.input(<%- toPythonParamType(ex.input) %>) <% } %>)
extism.output(res)

<% }) %>
<% if (ex.input) { -%>
input = <% if (ex.input.contentType === 'application/x-binary') { %> extism.input_bytes() <% } else { %> extism.input_str() <% } %>
<% if (ex.input.contentType === 'application/json') { -%>
<% if (ex.input.xtpType.kind === 'object') { -%>
input = <%- toPythonParamType(ex.input) %>.from_json(input)
<% } else { -%>
input = json.loads(input)
<% } -%>
<% } -%>
<% } -%>
res = plugin.<%- pythonFunctionName(ex.name) %>(<% if (ex.input) { %> input <% } %>)
<% if (ex.output) { -%>
<% if (ex.output.contentType === 'application/x-binary') { %> extism.output_bytes( <% } else { %> extism.output_str( <% } %>
<% if (ex.output.contentType === 'application/json') { -%>
<% if (ex.output.xtpType.kind === 'object') { -%>
res.to_json()
<% } else { -%>
json.dumps(res)
<% } %>
<% } else { -%>
res
<% } -%>
)
<% } -%>
<% }) -%>
34 changes: 33 additions & 1 deletion template/plugin/pdk_imports.py.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from typing import Optional, List # noqa: F401
from datetime import datetime # noqa: F401
import extism # noqa: F401 # pyright: ignore
import json

<% if (Object.values(schema.schemas).length > 0) { %>
from pdk_types import <%- Object.values(schema.schemas).map(schema => pythonTypeName(schema.name)).join(", ") %> # noqa: F401
Expand All @@ -18,8 +19,39 @@ from pdk_types import <%- Object.values(schema.schemas).map(schema => pythonType
# And it returns an output <%- toPythonParamType(imp.output) %> (<%- formatCommentLine(imp.output.description) %>)
<% } -%>
@extism.import_fn("extism:host/user", "<%- imp.name %>")
def <%- pythonFunctionName(imp.name) %>(<% if (imp.input) { -%>input: <%- toPythonParamType(imp.input) %><%} -%>) <% if (imp.output) { %> -> <%- toPythonParamType(imp.output) %><% } %>: # pyright: ignore [reportReturnType]
def _<%- pythonFunctionName(imp.name) %>(<% if (imp.input) { -%>input: <%- toPythonHostParamType(imp.input) %> <%} -%>) <% if (imp.output) { %> -> <%- toPythonHostParamType(imp.output) %> <% } %>: # pyright: ignore [reportReturnType]
pass

def <%- pythonFunctionName(imp.name) %>(<% if (imp.input) { -%>input: <%- toPythonParamType(imp.input) %><%} -%>) <% if (imp.output) { %> -> <%- toPythonParamType(imp.output) %><% } %>: # pyright: ignore [reportReturnType]
<% if (imp.output) { %>
return (
<% if (imp.output.contentType === 'application/json') { %>
<% if (imp.output.xtpType.kind === 'object') { %>
<%- toPythonParamType(imp.output) %>.from_json(
<% } else { %>
json.loads(
<% } -%>
<% } -%>
<% } -%>
_<%- pythonFunctionName(imp.name) %>(
<% if (imp.input) { -%>
<% if (imp.input.contentType === 'application/json') { %>
<% if (imp.input.xtpType.kind === 'object') { %>
input.to_json()
<% } else { %>
json.dumps(input)
<% } -%>
<% } else { %>
input
<% } -%>
<% } -%>
)
<% if (imp.output) { %>
)
<% if (imp.output.contentType === 'application/json') { %>
)
<% } -%>
<% } -%>
<% }) %>


24 changes: 21 additions & 3 deletions template/plugin/pdk_types.py.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ from enum import Enum # noqa: F401
from typing import Optional, List # noqa: F401
from datetime import datetime # noqa: F401
from dataclasses import dataclass # noqa: F401

import extism # noqa: F401 # pyright: ignore
from dataclass_wizard import JSONWizard # noqa: F401
from dataclass_wizard.type_def import JSONObject
from base64 import b64encode, b64decode

<% Object.values(schema.schemas).forEach(schema => { %>
<% if (schema.enum) { %>
Expand All @@ -16,7 +17,7 @@ class <%- pythonTypeName(schema.name) %>(Enum):
<% }) %>
<% } else { %>
@dataclass
class <%- pythonTypeName(schema.name) %>(extism.Json):
class <%- pythonTypeName(schema.name) %>(JSONWizard):
<% schema.properties.forEach(p => { -%>
<% if (!p.nullable && p.required) {%>
<% if (p.description) { -%>
Expand All @@ -35,6 +36,23 @@ class <%- pythonTypeName(schema.name) %>(extism.Json):
<% } %>
<% }) %>

@classmethod
def _pre_from_dict(cls, o: JSONObject) -> JSONObject:
<% schema.properties.forEach(p => { -%>
<% if (p.xtpType.kind === 'buffer') {%>
o['<%- p.name %>'] = b64decode(o['<%- p.name %>'])
<% } -%>
<% }) -%>
return o

def _pre_dict(self):
<% schema.properties.forEach(p => { -%>
<% if (p.xtpType.kind === 'buffer') {%>
self.<%- p.name %> = b64encode(self.<%- p.name %>).decode("utf-8")
<% } -%>
<% }) -%>
return

<% } %>
<% }); %>

2 changes: 0 additions & 2 deletions template/prepare.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,3 @@ if ! command_exists extism-py; then
sleep 2
exit 1
fi


6 changes: 4 additions & 2 deletions template/pyproject.toml.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ name = "<%= project.name %>"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.12"
dependencies = []
requires-python = ">=3.13,<3.14"
dependencies = [
"dataclass-wizard>=0.33.0,<0.34.0"
]

[tool.uv]
dev-dependencies = [
Expand Down
2 changes: 1 addition & 1 deletion template/xtp.toml.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ name = "<%= project.name %>"

[scripts]
# xtp plugin build runs this script to generate the wasm file
build = "PYTHONPATH=./plugin extism-py -o plugin.wasm plugin/__init__.py"
build = "PYTHONPATH=./plugin:./.venv/lib/python3.13/site-packages extism-py -o plugin.wasm plugin/__init__.py"

# xtp plugin init runs this script to format the code
format = "uv run ruff format plugin/*.py"
Expand Down
Loading