Skip to content
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
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @zshipko @bhelx
87 changes: 53 additions & 34 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,61 @@ import ejs from "ejs";
import { getContext, helpers, Property } from "@dylibso/xtp-bindgen";

function toPythonType(property: Property): string {
if (property.$ref) return property.$ref.name;
switch (property.type) {
case "string":
if (property.format === "date-time") {
return "datetime";
}
return "str";
case "number":
// @ts-ignore
if (property.contentType === "application/json"){
return "str";
}
let tp

if (property.format === "float" || property.format === "double") {
return "float";
}

return "int";
case "integer":
// @ts-ignore
if (property.contentType === "application/json"){
return "str";
}
return "int";
case "boolean":
return "bool";
case "object":
return "dict";
case "array":
if (!property.items) return "list";
return `List[${toPythonType(property.items as Property)}]`;
case "buffer":
return "bytes";
default:
throw new Error("Can't convert property to Python type: " + property.type);
if (property.$ref) {
tp = property.$ref.name
} else {
switch (property.type) {
case "string":
if (property.format === "date-time") {
tp = "datetime"
} else {
tp = "str"
}
break
case "number":
// @ts-ignore
if (property.contentType === "application/json") {
tp = "str"
} else if (property.format === "float" || property.format === "double") {
tp = "float"
} else {
tp = "int"
}
break
case "integer":
// @ts-ignore
if (property.contentType === "application/json") {
tp = "str"
} else {
tp = "int"
}
break
case "boolean":
tp = "bool"
break
case "object":
tp = "dict"
break
case "array":
if (!property.items) {
tp = "list"
} else {
tp = `List[${toPythonType(property.items as Property)}]`
}
break
case "buffer":
tp = "bytes"
break
default:
throw new Error("Can't convert property to Python type: " + property.type);
}
}

if (!tp) throw new Error("Cant convert property to Python type: " + property.type)
if (!property.nullable) return tp
return `Optional[${tp}]`
}

export function render() {
Expand Down
2 changes: 1 addition & 1 deletion template/plugin/pdk_types.py.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class <%- capitalize(schema.name) %>(extism.Json):
<% if (p.description) { -%>
# <%- formatCommentBlock(p.description, "# ") %>
<% } -%>
<%- p.name %>: <%- p.nullable ? `Optional[${toPythonType(p)}]` : `${toPythonType(p)}` %>
<%- p.name %>: <%- toPythonType(p) %>
<% }) %>
<% } %>
<% }); %>