From 59738e24e009f40f88773c78a51819bf792b629a Mon Sep 17 00:00:00 2001 From: zach Date: Tue, 19 Nov 2024 14:40:18 -0800 Subject: [PATCH 1/9] cleanup: only set default values for non-required fields --- template/plugin/pdk_types.py.ejs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/template/plugin/pdk_types.py.ejs b/template/plugin/pdk_types.py.ejs index cdcd11e..12eb413 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, "# ") %> <% } -%> -<% if (!p.nullable) {%> +<% if (!p.nullable || p.required) {%> <%- p.name %>: <%- toPythonType(p) %> <% } %> <% }) %> @@ -30,8 +30,8 @@ class <%- capitalize(schema.name) %>(extism.Json): <% if (p.description) { -%> # <%- formatCommentBlock(p.description, "# ") %> <% } -%> -<% if (p.nullable) {%> - <%- p.name %>: <%- toPythonType(p) %> = None +<% if (p.nullable && !p.required) {%> + <%- p.name %>: <%- toPythonType(p) %> <% } %> <% }) %> From 564b6f4cee13c3a72b0fa3ef4ef3b20eb96e8039 Mon Sep 17 00:00:00 2001 From: zach Date: Tue, 19 Nov 2024 14:46:21 -0800 Subject: [PATCH 2/9] fix: move comments into if statement --- template/plugin/pdk_types.py.ejs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/template/plugin/pdk_types.py.ejs b/template/plugin/pdk_types.py.ejs index 12eb413..1ca6ca0 100644 --- a/template/plugin/pdk_types.py.ejs +++ b/template/plugin/pdk_types.py.ejs @@ -18,19 +18,19 @@ class <%- capitalize(schema.name) %>(Enum): @dataclass class <%- capitalize(schema.name) %>(extism.Json): <% schema.properties.forEach(p => { -%> -<% if (p.description) { -%> - # <%- formatCommentBlock(p.description, "# ") %> -<% } -%> <% if (!p.nullable || p.required) {%> + <% if (p.description) { -%> + # <%- formatCommentBlock(p.description, "# ") %> + <% } -%> <%- p.name %>: <%- toPythonType(p) %> <% } %> <% }) %> <% schema.properties.forEach(p => { -%> -<% if (p.description) { -%> - # <%- formatCommentBlock(p.description, "# ") %> -<% } -%> <% if (p.nullable && !p.required) {%> + <% if (p.description) { -%> + # <%- formatCommentBlock(p.description, "# ") %> + <% } -%> <%- p.name %>: <%- toPythonType(p) %> <% } %> <% }) %> From 7c60c8c89dd359a8affdc5884567716f922a0528 Mon Sep 17 00:00:00 2001 From: zach Date: Tue, 19 Nov 2024 14:50:24 -0800 Subject: [PATCH 3/9] fix: copy paste error --- template/plugin/pdk_types.py.ejs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/template/plugin/pdk_types.py.ejs b/template/plugin/pdk_types.py.ejs index 1ca6ca0..344bac7 100644 --- a/template/plugin/pdk_types.py.ejs +++ b/template/plugin/pdk_types.py.ejs @@ -31,7 +31,7 @@ class <%- capitalize(schema.name) %>(extism.Json): <% if (p.description) { -%> # <%- formatCommentBlock(p.description, "# ") %> <% } -%> - <%- p.name %>: <%- toPythonType(p) %> + <%- p.name %>: <%- toPythonType(p) %> = None <% } %> <% }) %> From d46a1c32bcadbd3823ae4f33e697b7045c4fa1fe Mon Sep 17 00:00:00 2001 From: zach Date: Tue, 19 Nov 2024 16:37:32 -0800 Subject: [PATCH 4/9] chore: start bindgen refactor --- bindgen-test.sh | 2 +- package-lock.json | 9 ++- package.json | 2 +- src/index.ts | 119 ++++++++++++++++------------- template/plugin/__init__.py.ejs | 8 +- template/plugin/pdk_imports.py.ejs | 6 +- template/plugin/pdk_types.py.ejs | 6 +- template/plugin/plugin.py.ejs | 9 ++- 8 files changed, 90 insertions(+), 71 deletions(-) diff --git a/bindgen-test.sh b/bindgen-test.sh index 32257d3..f38584c 100755 --- a/bindgen-test.sh +++ b/bindgen-test.sh @@ -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 + xtp plugin test $PLUGIN_NAME/plugin.wasm --with test.wasm --mock-host mock.wasm --log-level info ;; esac diff --git a/package-lock.json b/package-lock.json index 8ba5d1d..2c6b4d4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,7 @@ "version": "0.0.1", "license": "BSD-3-Clause", "dependencies": { - "@dylibso/xtp-bindgen": "1.0.0-rc.11", + "@dylibso/xtp-bindgen": "1.0.0-rc.13", "ejs": "^3.1.10" }, "devDependencies": { @@ -21,9 +21,10 @@ } }, "node_modules/@dylibso/xtp-bindgen": { - "version": "1.0.0-rc.11", - "resolved": "https://registry.npmjs.org/@dylibso/xtp-bindgen/-/xtp-bindgen-1.0.0-rc.11.tgz", - "integrity": "sha512-zXesPfNHKaEK3IwMKFW5qk4UoJTazxmslpyUYn6n4FffZvY7QPBOSomyNVaNRtTr3ziz5SwF2RAm+Rken21HIg==" + "version": "1.0.0-rc.13", + "resolved": "https://registry.npmjs.org/@dylibso/xtp-bindgen/-/xtp-bindgen-1.0.0-rc.13.tgz", + "integrity": "sha512-aCmYSgC3Xwdlhm8mBKVpkzuHAtQ84ZgIwkdYQ3QFmDRxnBlZUtOZgpRoJTtOnahm0lUCiBkWkLcsikUluMY/qw==", + "license": "BSD-3-Clause" }, "node_modules/@esbuild/aix-ppc64": { "version": "0.19.12", diff --git a/package.json b/package.json index d752f3b..bd1786f 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,7 @@ "typescript": "^5.3.2" }, "dependencies": { - "@dylibso/xtp-bindgen": "1.0.0-rc.11", + "@dylibso/xtp-bindgen": "1.0.0-rc.13", "ejs": "^3.1.10" } } diff --git a/src/index.ts b/src/index.ts index 260fff1..d23e679 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,62 +1,71 @@ import ejs from "ejs"; -import { getContext, helpers, Property } from "@dylibso/xtp-bindgen"; +import { + helpers, + getContext, + ObjectType, + EnumType, + ArrayType, + XtpNormalizedType, + MapType, + Parameter, + Property, + XtpTyped +} from "@dylibso/xtp-bindgen" -function toPythonType(property: Property): string { - let tp +function pythonTypeName(s: string): string { + return helpers.snakeToPascalCase(s); +} + +function pythonFunctionName(s: string): string { + return helpers.camelToSnakeCase(s); +} - 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); - } + +function toPythonTypeX(type: XtpNormalizedType): string { + switch (type.kind) { + case 'string': + return 'str'; + case 'int32': + return 'int'; + case 'float': + return 'float'; + case 'double': + return 'float' + case 'byte': + return 'byte'; + case 'date-time': + return "datetime"; + case 'boolean': + return 'bool'; + case 'array': + const arrayType = type as ArrayType + return `List[${toPythonTypeX(arrayType.elementType)}]` + case 'buffer': + return 'bytes'; + case 'map': + // TODO: improve typing of dicts + return 'dict'; + case 'object': + return pythonTypeName((type as ObjectType).name); + case 'enum': + return pythonTypeName((type as EnumType).name); + default: + throw new Error("Can't convert XTP type to Python type: " + type) } +} - if (!tp) throw new Error("Cant convert property to Python type: " + property.type) - if (!property.nullable && !property.required) return tp - return `Optional[${tp}]` + +function toPythonType(property: XtpTyped, required?: boolean): string { + let t = toPythonTypeX(property.xtpType); + if (property.xtpType.nullable) { + t = `Optional[${t}]`; + } + if (required === undefined || required) return t; + + if (!property.xtpType.nullable) { + t = `Optional[${t}]`; + } + return t; } export function render() { @@ -65,6 +74,8 @@ export function render() { ...helpers, ...getContext(), toPythonType, + pythonTypeName, + pythonFunctionName }; const output = ejs.render(tmpl, ctx); diff --git a/template/plugin/__init__.py.ejs b/template/plugin/__init__.py.ejs index d3e6dd9..3dfd966 100644 --- a/template/plugin/__init__.py.ejs +++ b/template/plugin/__init__.py.ejs @@ -1,10 +1,12 @@ # THIS FILE WAS GENERATED BY `xtp-python-bindgen`. DO NOT EDIT. +from typing import Optional, List # noqa: F401 +from datetime import datetime # noqa: F401 import extism # pyright: ignore import plugin <% if (Object.values(schema.schemas).length > 0){ %> -from pdk_types import <%- Object.values(schema.schemas).map(schema => schema.name).join(", ") %> # noqa: F401 +from pdk_types import <%- Object.values(schema.schemas).map(schema => pythonTypeName(schema.name)).join(", ") %> # noqa: F401 <% } %> # Imports @@ -19,7 +21,7 @@ from pdk_types import <%- Object.values(schema.schemas).map(schema => schema.nam # And it returns an output <%- toPythonType(imp.output) %> (<%- formatCommentLine(imp.output.description) %>) <% } -%> @extism.import_fn("extism:host/user", "<%- imp.name %>") -def <%- camelToSnakeCase(imp.name) %>(<% if (imp.input) { -%>input: <%- toPythonType(imp.input) %><%} -%>) <% if (imp.output) { %> -> <%- toPythonType(imp.output) %><% } %>: # pyright: ignore [reportReturnType] +def <%- pythonFunctionName(imp.name) %>(<% if (imp.input) { -%>input: <%- toPythonType(imp.input) %><%} -%>) <% if (imp.output) { %> -> <%- toPythonType(imp.output) %><% } %>: # pyright: ignore [reportReturnType] pass <% }) %> @@ -32,7 +34,7 @@ def <%- camelToSnakeCase(imp.name) %>(<% if (imp.input) { -%>input: <%- toPython <% } -%> @extism.plugin_fn def <%- ex.name %>(): - res = plugin.<%- camelToSnakeCase(ex.name) %>(<% if (ex.input) { %> extism.input(<%- toPythonType(ex.input) %>) <% } %>) + res = plugin.<%- pythonFunctionName(ex.name) %>(<% if (ex.input) { %> extism.input(<%- toPythonType(ex.input) %>) <% } %>) extism.output(res) <% }) %> diff --git a/template/plugin/pdk_imports.py.ejs b/template/plugin/pdk_imports.py.ejs index 9c5cb9d..a040b0b 100644 --- a/template/plugin/pdk_imports.py.ejs +++ b/template/plugin/pdk_imports.py.ejs @@ -1,9 +1,11 @@ # THIS FILE WAS GENERATED BY `xtp-python-bindgen`. DO NOT EDIT. +from typing import Optional, List # noqa: F401 +from datetime import datetime # noqa: F401 import extism # noqa: F401 # pyright: ignore <% if (Object.values(schema.schemas).length > 0) { %> -from pdk_types import <%- Object.values(schema.schemas).map(schema => schema.name).join(", ") %> # noqa: F401 +from pdk_types import <%- Object.values(schema.schemas).map(schema => pythonTypeName(schema.name)).join(", ") %> # noqa: F401 <% } %> <% schema.imports.forEach(imp => { %> @@ -16,7 +18,7 @@ from pdk_types import <%- Object.values(schema.schemas).map(schema => schema.nam # And it returns an output <%- toPythonType(imp.output) %> (<%- formatCommentLine(imp.output.description) %>) <% } -%> @extism.import_fn("extism:host/user", "<%- imp.name %>") -def <%- camelToSnakeCase(imp.name) %>(<% if (imp.input) { -%>input: <%- toPythonType(imp.input) %><%} -%>) <% if (imp.output) { %> -> <%- toPythonType(imp.output) %><% } %>: # pyright: ignore [reportReturnType] +def <%- pythonFunctionName(imp.name) %>(<% if (imp.input) { -%>input: <%- toPythonType(imp.input) %><%} -%>) <% if (imp.output) { %> -> <%- toPythonType(imp.output) %><% } %>: # pyright: ignore [reportReturnType] pass <% }) %> diff --git a/template/plugin/pdk_types.py.ejs b/template/plugin/pdk_types.py.ejs index 344bac7..5054b1e 100644 --- a/template/plugin/pdk_types.py.ejs +++ b/template/plugin/pdk_types.py.ejs @@ -10,13 +10,13 @@ import extism # noqa: F401 # pyright: ignore <% Object.values(schema.schemas).forEach(schema => { %> <% if (schema.enum) { %> -class <%- capitalize(schema.name) %>(Enum): +class <%- pythonTypeName(schema.name) %>(Enum): <% schema.enum.forEach(variant => { -%> - <%- capitalize(variant) %> = "<%- variant %>" + <%- pythonTypeName(variant) %> = "<%- variant %>" <% }) %> <% } else { %> @dataclass -class <%- capitalize(schema.name) %>(extism.Json): +class <%- pythonTypeName(schema.name) %>(extism.Json): <% schema.properties.forEach(p => { -%> <% if (!p.nullable || p.required) {%> <% if (p.description) { -%> diff --git a/template/plugin/plugin.py.ejs b/template/plugin/plugin.py.ejs index 4afb7a2..c7624cc 100644 --- a/template/plugin/plugin.py.ejs +++ b/template/plugin/plugin.py.ejs @@ -1,9 +1,12 @@ +from typing import Optional, List # noqa: F401 +from datetime import datetime # noqa: F401 import extism # noqa: F401 # pyright: ignore + <% if (Object.values(schema.schemas).length > 0) { %> -from pdk_types import <%- Object.values(schema.schemas).map(schema => schema.name).join(", ") %> # noqa: F401 +from pdk_types import <%- Object.values(schema.schemas).map(schema => pythonTypeName(schema.name)).join(", ") %> # noqa: F401 <% } %> <% if (schema.imports.length > 0) { %> -from pdk_imports import <%- schema.imports.map(schema => camelToSnakeCase(schema.name)).join(", ") %> # noqa: F401 +from pdk_imports import <%- schema.imports.map(schema => pythonFunctionName(schema.name)).join(", ") %> # noqa: F401 <% } %> from typing import List, Optional # noqa: F401 @@ -11,7 +14,7 @@ from typing import List, Optional # noqa: F401 <% if (hasComment(ex)) { -%> # <%- formatCommentBlock(ex.description, "# ") %> <% } -%> -def <%- camelToSnakeCase(ex.name) %>(<% if (ex.input) { %>input: <%- toPythonType(ex.input) %> <% } %>) <% if (ex.output) {%>-> <%- toPythonType(ex.output) %><%}%>: +def <%- pythonFunctionName(ex.name) %>(<% if (ex.input) { %>input: <%- toPythonType(ex.input) %> <% } %>) <% if (ex.output) {%>-> <%- toPythonType(ex.output) %><%}%>: <% if (featureFlags['stub-with-code-samples'] && codeSamples(ex, 'python').length > 0) { -%> <%- codeSamples(ex, 'python')[0].source %> <% } else { -%> From 062e816bff1ae7a7afd61abe95cf5c9e8196e0dc Mon Sep 17 00:00:00 2001 From: zach Date: Tue, 19 Nov 2024 16:41:54 -0800 Subject: [PATCH 5/9] fix: log level --- bindgen-test.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bindgen-test.sh b/bindgen-test.sh index f38584c..32257d3 100755 --- a/bindgen-test.sh +++ b/bindgen-test.sh @@ -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 --log-level info + xtp plugin test $PLUGIN_NAME/plugin.wasm --with test.wasm --mock-host mock.wasm ;; esac From 5e0878fb8d4144d615b6b3fc74a2beeba60ead06 Mon Sep 17 00:00:00 2001 From: zach Date: Tue, 19 Nov 2024 17:06:16 -0800 Subject: [PATCH 6/9] fix: input/outputs types --- src/index.ts | 12 ++++++++++++ template/plugin/__init__.py.ejs | 8 ++++---- template/plugin/pdk_imports.py.ejs | 6 +++--- template/plugin/plugin.py.ejs | 2 +- 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/index.ts b/src/index.ts index d23e679..4a4df66 100644 --- a/src/index.ts +++ b/src/index.ts @@ -68,12 +68,24 @@ function toPythonType(property: XtpTyped, required?: boolean): string { return t; } +function toPythonParamType(property: XtpTyped): string { + let t = toPythonTypeX(property.xtpType); + if (t === 'int' || t === 'float'){ + t = 'str'; + } + if (property.xtpType.nullable) { + t = `Optional[${t}]`; + } + return t; +} + export function render() { const tmpl = Host.inputString(); const ctx = { ...helpers, ...getContext(), toPythonType, + toPythonParamType, pythonTypeName, pythonFunctionName }; diff --git a/template/plugin/__init__.py.ejs b/template/plugin/__init__.py.ejs index 3dfd966..ae0dcf9 100644 --- a/template/plugin/__init__.py.ejs +++ b/template/plugin/__init__.py.ejs @@ -15,13 +15,13 @@ from pdk_types import <%- Object.values(schema.schemas).map(schema => pythonType <% if (hasComment(imp)) -%> #<%- imp.name %> <%- formatCommentBlock(imp.description, "# ") %> <% if (hasComment(imp.input)) { -%> -# It takes input of <%- toPythonType(imp.input) %> (<%- formatCommentLine(imp.input.description) %>) +# It takes input of <%- toPythonParamType(imp.input) %> (<%- formatCommentLine(imp.input.description) %>) <% } -%> <% if (hasComment(imp.output)) { -%> -# And it returns an output <%- toPythonType(imp.output) %> (<%- formatCommentLine(imp.output.description) %>) +# 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: <%- toPythonType(imp.input) %><%} -%>) <% if (imp.output) { %> -> <%- toPythonType(imp.output) %><% } %>: # pyright: ignore [reportReturnType] +def <%- pythonFunctionName(imp.name) %>(<% if (imp.input) { -%>input: <%- toPythonParamType(imp.input) %><%} -%>) <% if (imp.output) { %> -> <%- toPythonParamType(imp.output) %><% } %>: # pyright: ignore [reportReturnType] pass <% }) %> @@ -34,7 +34,7 @@ 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(<%- toPythonType(ex.input) %>) <% } %>) + res = plugin.<%- pythonFunctionName(ex.name) %>(<% if (ex.input) { %> extism.input(<%- toPythonParamType(ex.input) %>) <% } %>) extism.output(res) <% }) %> diff --git a/template/plugin/pdk_imports.py.ejs b/template/plugin/pdk_imports.py.ejs index a040b0b..59ac0f2 100644 --- a/template/plugin/pdk_imports.py.ejs +++ b/template/plugin/pdk_imports.py.ejs @@ -12,13 +12,13 @@ from pdk_types import <%- Object.values(schema.schemas).map(schema => pythonType <% if (hasComment(imp)) -%> #<%- imp.name %> <%- formatCommentBlock(imp.description, "# ") %> <% if (hasComment(imp.input)) { -%> -# It takes input of <%- toPythonType(imp.input) %> (<%- formatCommentLine(imp.input.description) %>) +# It takes input of <%- toPythonParamType(imp.input) %> (<%- formatCommentLine(imp.input.description) %>) <% } -%> <% if (hasComment(imp.output)) { -%> -# And it returns an output <%- toPythonType(imp.output) %> (<%- formatCommentLine(imp.output.description) %>) +# 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: <%- toPythonType(imp.input) %><%} -%>) <% if (imp.output) { %> -> <%- toPythonType(imp.output) %><% } %>: # pyright: ignore [reportReturnType] +def <%- pythonFunctionName(imp.name) %>(<% if (imp.input) { -%>input: <%- toPythonParamType(imp.input) %><%} -%>) <% if (imp.output) { %> -> <%- toPythonParamType(imp.output) %><% } %>: # pyright: ignore [reportReturnType] pass <% }) %> diff --git a/template/plugin/plugin.py.ejs b/template/plugin/plugin.py.ejs index c7624cc..720f83a 100644 --- a/template/plugin/plugin.py.ejs +++ b/template/plugin/plugin.py.ejs @@ -14,7 +14,7 @@ from typing import List, Optional # noqa: F401 <% if (hasComment(ex)) { -%> # <%- formatCommentBlock(ex.description, "# ") %> <% } -%> -def <%- pythonFunctionName(ex.name) %>(<% if (ex.input) { %>input: <%- toPythonType(ex.input) %> <% } %>) <% if (ex.output) {%>-> <%- toPythonType(ex.output) %><%}%>: +def <%- pythonFunctionName(ex.name) %>(<% if (ex.input) { %>input: <%- toPythonParamType(ex.input) %> <% } %>) <% if (ex.output) {%>-> <%- toPythonParamType(ex.output) %><%}%>: <% if (featureFlags['stub-with-code-samples'] && codeSamples(ex, 'python').length > 0) { -%> <%- codeSamples(ex, 'python')[0].source %> <% } else { -%> From be5e646c52753f55cd309141374efa0bcd36cafe Mon Sep 17 00:00:00 2001 From: zach Date: Wed, 20 Nov 2024 09:17:44 -0800 Subject: [PATCH 7/9] cleanup: better handling of optional types --- src/index.ts | 49 ++++++++++++++++++++++--------------------------- 1 file changed, 22 insertions(+), 27 deletions(-) diff --git a/src/index.ts b/src/index.ts index 4a4df66..16075b2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -20,35 +20,41 @@ function pythonFunctionName(s: string): string { return helpers.camelToSnakeCase(s); } +function isOptional(type: String): boolean { + return type.startsWith('Optional[') +} function toPythonTypeX(type: XtpNormalizedType): string { + const opt = (t: string) => { + return type.nullable ? `Optional[${t}]` : t + } switch (type.kind) { case 'string': - return 'str'; + return opt('str'); case 'int32': - return 'int'; + return opt('int'); case 'float': - return 'float'; + return opt('float'); case 'double': - return 'float' + return opt('float') case 'byte': - return 'byte'; + return opt('byte'); case 'date-time': - return "datetime"; + return opt("datetime"); case 'boolean': - return 'bool'; + return opt('bool'); case 'array': const arrayType = type as ArrayType - return `List[${toPythonTypeX(arrayType.elementType)}]` + return opt(`List[${toPythonTypeX(arrayType.elementType)}]`); case 'buffer': - return 'bytes'; + return opt('bytes'); case 'map': // TODO: improve typing of dicts - return 'dict'; + return opt('dict'); case 'object': - return pythonTypeName((type as ObjectType).name); + return opt(pythonTypeName((type as ObjectType).name)); case 'enum': - return pythonTypeName((type as EnumType).name); + return opt(pythonTypeName((type as EnumType).name)); default: throw new Error("Can't convert XTP type to Python type: " + type) } @@ -57,25 +63,14 @@ function toPythonTypeX(type: XtpNormalizedType): string { function toPythonType(property: XtpTyped, required?: boolean): string { let t = toPythonTypeX(property.xtpType); - if (property.xtpType.nullable) { - t = `Optional[${t}]`; - } - if (required === undefined || required) return t; - - if (!property.xtpType.nullable) { - t = `Optional[${t}]`; - } - return t; + if (required === undefined || required || isOptional(t)) return t; + return `Optional[${t}]`; } function toPythonParamType(property: XtpTyped): string { let t = toPythonTypeX(property.xtpType); - if (t === 'int' || t === 'float'){ - t = 'str'; - } - if (property.xtpType.nullable) { - t = `Optional[${t}]`; - } + t = t.replace('int', 'str'); + t = t.replace('float', 'str'); return t; } From babe8061ea7beaf82f88cde9fbd584a0ba4bc9af Mon Sep 17 00:00:00 2001 From: zach Date: Wed, 20 Nov 2024 13:31:46 -0800 Subject: [PATCH 8/9] ci: bundle? --- .github/workflows/test.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index d6dec0e..a53eab3 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -63,6 +63,7 @@ jobs: - name: Run Bindgen Test run: | + sh bundle.sh sh bindgen-test.sh install sh bindgen-test.sh run From 79b0d13ce4b8a41bf3771c0474caafad1c9459e0 Mon Sep 17 00:00:00 2001 From: zach Date: Fri, 22 Nov 2024 09:10:44 -0800 Subject: [PATCH 9/9] cleanup: address feedback --- src/index.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/index.ts b/src/index.ts index 16075b2..c7e0ef8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,9 +6,6 @@ import { EnumType, ArrayType, XtpNormalizedType, - MapType, - Parameter, - Property, XtpTyped } from "@dylibso/xtp-bindgen" @@ -61,14 +58,17 @@ function toPythonTypeX(type: XtpNormalizedType): string { } -function toPythonType(property: XtpTyped, required?: boolean): string { +function toPythonType(property: XtpTyped): string { let t = toPythonTypeX(property.xtpType); - if (required === undefined || required || isOptional(t)) return t; + if (isOptional(t)) return t; return `Optional[${t}]`; } function toPythonParamType(property: XtpTyped): string { let t = toPythonTypeX(property.xtpType); + // We need to represent bare int/float as a str in Python for now, + // there may be some updates to the encoder we can make to handle + // this case at some point t = t.replace('int', 'str'); t = t.replace('float', 'str'); return t;