Skip to content

Commit

Permalink
Merge pull request #8 from dylibso/codeowners
Browse files Browse the repository at this point in the history
codeowners
  • Loading branch information
bhelx authored Oct 22, 2024
2 parents adae7c1 + 314025a commit 65d8cbb
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 35 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
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) %>
<% }) %>
<% } %>
<% }); %>

0 comments on commit 65d8cbb

Please sign in to comment.