Skip to content

Commit 65d8cbb

Browse files
authored
Merge pull request #8 from dylibso/codeowners
codeowners
2 parents adae7c1 + 314025a commit 65d8cbb

3 files changed

Lines changed: 55 additions & 35 deletions

File tree

.github/CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* @zshipko @bhelx

src/index.ts

Lines changed: 53 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,61 @@ import ejs from "ejs";
22
import { getContext, helpers, Property } from "@dylibso/xtp-bindgen";
33

44
function toPythonType(property: Property): string {
5-
if (property.$ref) return property.$ref.name;
6-
switch (property.type) {
7-
case "string":
8-
if (property.format === "date-time") {
9-
return "datetime";
10-
}
11-
return "str";
12-
case "number":
13-
// @ts-ignore
14-
if (property.contentType === "application/json"){
15-
return "str";
16-
}
5+
let tp
176

18-
if (property.format === "float" || property.format === "double") {
19-
return "float";
20-
}
21-
22-
return "int";
23-
case "integer":
24-
// @ts-ignore
25-
if (property.contentType === "application/json"){
26-
return "str";
27-
}
28-
return "int";
29-
case "boolean":
30-
return "bool";
31-
case "object":
32-
return "dict";
33-
case "array":
34-
if (!property.items) return "list";
35-
return `List[${toPythonType(property.items as Property)}]`;
36-
case "buffer":
37-
return "bytes";
38-
default:
39-
throw new Error("Can't convert property to Python type: " + property.type);
7+
if (property.$ref) {
8+
tp = property.$ref.name
9+
} else {
10+
switch (property.type) {
11+
case "string":
12+
if (property.format === "date-time") {
13+
tp = "datetime"
14+
} else {
15+
tp = "str"
16+
}
17+
break
18+
case "number":
19+
// @ts-ignore
20+
if (property.contentType === "application/json") {
21+
tp = "str"
22+
} else if (property.format === "float" || property.format === "double") {
23+
tp = "float"
24+
} else {
25+
tp = "int"
26+
}
27+
break
28+
case "integer":
29+
// @ts-ignore
30+
if (property.contentType === "application/json") {
31+
tp = "str"
32+
} else {
33+
tp = "int"
34+
}
35+
break
36+
case "boolean":
37+
tp = "bool"
38+
break
39+
case "object":
40+
tp = "dict"
41+
break
42+
case "array":
43+
if (!property.items) {
44+
tp = "list"
45+
} else {
46+
tp = `List[${toPythonType(property.items as Property)}]`
47+
}
48+
break
49+
case "buffer":
50+
tp = "bytes"
51+
break
52+
default:
53+
throw new Error("Can't convert property to Python type: " + property.type);
54+
}
4055
}
56+
57+
if (!tp) throw new Error("Cant convert property to Python type: " + property.type)
58+
if (!property.nullable) return tp
59+
return `Optional[${tp}]`
4160
}
4261

4362
export function render() {

template/plugin/pdk_types.py.ejs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class <%- capitalize(schema.name) %>(extism.Json):
2121
<% if (p.description) { -%>
2222
# <%- formatCommentBlock(p.description, "# ") %>
2323
<% } -%>
24-
<%- p.name %>: <%- p.nullable ? `Optional[${toPythonType(p)}]` : `${toPythonType(p)}` %>
24+
<%- p.name %>: <%- toPythonType(p) %>
2525
<% }) %>
2626
<% } %>
2727
<% }); %>

0 commit comments

Comments
 (0)