Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add default value for optional fields #11

Merged
merged 1 commit into from
Nov 11, 2024
Merged
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
13 changes: 13 additions & 0 deletions template/plugin/pdk_types.py.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,20 @@ class <%- capitalize(schema.name) %>(extism.Json):
<% if (p.description) { -%>
# <%- formatCommentBlock(p.description, "# ") %>
<% } -%>
<% if (!p.nullable) {%>
<%- p.name %>: <%- toPythonType(p) %>
<% } %>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the only difference b/w these two loops is the nullable part. maybe we can merge them something like this?


@dataclass
class <%- capitalize(schema.name) %>(extism.Json):
<% schema.properties.forEach(p => { -%>
<% if (p.description) { -%>
  # <%- formatCommentBlock(p.description, "# ") %>
<% } -%>
<% if (p.nullable) {%>
  <%- p.name %>: <%- toPythonType(p) %> = None
<% } else { %>
  <%- p.name %>: <%- toPythonType(p) %>
<% } %>
<% }) %>

<% } %>
<% }); %>

or just customize the = None part

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's an ordering issue, the fields with default values have to come after the fields with no defaults.

<% }) %>

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

<% } %>
<% }); %>

Loading