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
12 changes: 6 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ function isOptional(type: String): boolean {
return type.startsWith('Optional[')
}

function toPythonTypeX(type: XtpNormalizedType): string {
function toPythonTypeX(type: XtpNormalizedType, required: boolean = true): string {
const opt = (t: string) => {
return type.nullable ? `Optional[${t}]` : t
return type.nullable || required === false ? `Optional[${t}]` : t
}
switch (type.kind) {
case 'string':
Expand Down Expand Up @@ -64,12 +64,12 @@ function toPythonTypeX(type: XtpNormalizedType): string {
}


function toPythonType(property: XtpTyped): string {
return toPythonTypeX(property.xtpType);
function toPythonType(property: XtpTyped, required?: boolean): string {
return toPythonTypeX(property.xtpType, required === true ? true : false);
}

function toPythonParamType(property: XtpTyped): string {
let t = toPythonTypeX(property.xtpType);
function toPythonParamType(property: XtpTyped, required?: boolean): string {
let t = toPythonTypeX(property.xtpType, required);
// 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
Expand Down
8 changes: 4 additions & 4 deletions template/plugin/pdk_types.py.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@ class <%- pythonTypeName(schema.name) %>(Enum):
@dataclass
class <%- pythonTypeName(schema.name) %>(extism.Json):
<% schema.properties.forEach(p => { -%>
<% if (!p.nullable || p.required) {%>
<% if (!p.nullable && p.required) {%>
<% if (p.description) { -%>
# <%- formatCommentBlock(p.description, "# ") %>
<% } -%>
<%- p.name %>: <%- toPythonType(p) %>
<%- p.name %>: <%- toPythonType(p, p.required) %>
<% } %>
<% }) %>

<% schema.properties.forEach(p => { -%>
<% if (p.nullable && !p.required) {%>
<% if (p.nullable || !p.required) {%>
<% if (p.description) { -%>
# <%- formatCommentBlock(p.description, "# ") %>
<% } -%>
<%- p.name %>: <%- toPythonType(p) %> = None
<%- p.name %>: <%- toPythonType(p, p.required) %> = None
<% } %>
<% }) %>

Expand Down
19 changes: 19 additions & 0 deletions tests/schemas/fruit.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ exports:
contentType: application/json
$ref: "#/components/schemas/ComplexObject"
imports:
eatAString:
input:
contentType: text/plain; charset=utf-8
type: string
output:
contentType: text/plain; charset=utf-8
type: string
description: |
This is a host function. Right now host functions can only be the type (i64) -> i64.
We will support more in the future. Much of the same rules as exports apply.
eatAFruit:
input:
contentType: text/plain; charset=utf-8
Expand Down Expand Up @@ -117,4 +127,13 @@ components:
writeParams:
"$ref": "#/components/schemas/WriteParams"
nullable: true
aMapOfNullableDateTimes:
additionalProperties:
type: string
format: date-time
nullable: true
required:
- aBoolean
- aString
- writeParams
description: A complex json object
Loading