diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS new file mode 100644 index 0000000..a02cc82 --- /dev/null +++ b/.github/CODEOWNERS @@ -0,0 +1 @@ +* @zshipko @bhelx diff --git a/src/index.ts b/src/index.ts index f4ff3e0..ac0c864 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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() { diff --git a/template/plugin/pdk_types.py.ejs b/template/plugin/pdk_types.py.ejs index 0e3102d..be16f80 100644 --- a/template/plugin/pdk_types.py.ejs +++ b/template/plugin/pdk_types.py.ejs @@ -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) %> <% }) %> <% } %> <% }); %>