Skip to content

Commit

Permalink
Preserve undefined vs null in encoders (#28)
Browse files Browse the repository at this point in the history
* Preserve undefined vs null in encoders

In the json encoder / decoder, we were essentially re-writing undefineds
with null. This should preserve the value as null or undefined in the
falsey case.

* fix install scripts

* more explicit check
  • Loading branch information
bhelx authored Oct 1, 2024
1 parent 61e62b6 commit c9de0eb
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 11 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ jobs:

- name: Setup Extism
run: |
curl -O https://raw.githubusercontent.com/extism/js-pdk/main/install.sh
sh install.sh
curl -L https://raw.githubusercontent.com/extism/js-pdk/main/install.sh | bash
- name: Set up Node.js
uses: actions/setup-node@v2
Expand Down
5 changes: 2 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ jobs:

- name: Setup Extism and XTP
run: |
curl -O https://raw.githubusercontent.com/extism/js-pdk/main/install.sh
sh install.sh
curl https://static.dylibso.com/cli/install.sh | sh
curl https://raw.githubusercontent.com/extism/js-pdk/main/install.sh | bash
curl https://static.dylibso.com/cli/install.sh | bash
- name: Set up Node.js
uses: actions/setup-node@v3
Expand Down
12 changes: 6 additions & 6 deletions template/src/pdk.ts.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ export class <%- schema.name %> {
...obj,
<% schema.properties.forEach(p => { -%>
<% if (isDateTime(p)) { -%>
<%- p.name -%>: obj.<%- p.name -%> ? new Date(obj.<%- p.name -%>) : null,
<%- p.name -%>: obj.<%- p.name -%> === undefined || obj.<%- p.name -%> === null ? obj.<%- p.name %> : new Date(obj.<%- p.name -%>),
<% } else if (isBuffer(p)) {-%>
<%- p.name -%>: obj.<%- p.name -%> ? Host.base64ToArrayBuffer(obj.<%- p.name -%>) : null,
<%- p.name -%>: obj.<%- p.name -%> === undefined || obj.<%- p.name -%> === null ? obj.<%- p.name %> : Host.base64ToArrayBuffer(obj.<%- p.name -%>),
<% } else if (!isPrimitive(p)) {-%>
<%- p.name -%>: obj.<%- p.name -%> ? <%- p.$ref.name %>.fromJson(obj.<%- p.name -%>) : null,
<%- p.name -%>: obj.<%- p.name -%> === undefined || obj.<%- p.name -%> === null ? obj.<%- p.name %> : <%- p.$ref.name %>.fromJson(obj.<%- p.name -%>),
<% } -%>
<% }) -%>
}
Expand All @@ -40,11 +40,11 @@ export class <%- schema.name %> {
...obj,
<% schema.properties.forEach(p => { -%>
<% if (p.type === "string" && p.format === "date-time") { -%>
<%- p.name -%>: obj.<%- p.name -%> ? obj.<%- p.name %>.toISOString() : null,
<%- p.name -%>: obj.<%- p.name -%> === undefined || obj.<%- p.name -%> === null ? obj.<%- p.name %> : obj.<%- p.name %>.toISOString(),
<% } else if (isBuffer(p)) {-%>
<%- p.name -%>: obj.<%- p.name -%> ? Host.arrayBufferToBase64(obj.<%- p.name -%>) : null,
<%- p.name -%>: obj.<%- p.name -%> === undefined || obj.<%- p.name -%> === null ? obj.<%- p.name %> : Host.arrayBufferToBase64(obj.<%- p.name -%>),
<% } else if (p.$ref && !p.$ref.enum) {-%>
<%- p.name -%>: obj.<%- p.name -%> ? <%- p.$ref.name %>.toJson(obj.<%- p.name -%>) : null,
<%- p.name -%>: obj.<%- p.name -%> === undefined || obj.<%- p.name -%> === null ? obj.<%- p.name %>: <%- p.$ref.name %>.toJson(obj.<%- p.name -%>) ,
<% } -%>
<% }) -%>
}
Expand Down

0 comments on commit c9de0eb

Please sign in to comment.