From c9de0eb037c853612a6c71b3ed8a6443cd46d0da Mon Sep 17 00:00:00 2001 From: Benjamin Eckel Date: Tue, 1 Oct 2024 16:25:03 -0500 Subject: [PATCH] Preserve undefined vs null in encoders (#28) * 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 --- .github/workflows/release.yml | 3 +-- .github/workflows/test.yml | 5 ++--- template/src/pdk.ts.ejs | 12 ++++++------ 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index a0f6475..b0ffc7b 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -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 diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 5636f69..37b8997 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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 diff --git a/template/src/pdk.ts.ejs b/template/src/pdk.ts.ejs index 8982721..970ec2c 100644 --- a/template/src/pdk.ts.ejs +++ b/template/src/pdk.ts.ejs @@ -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 -%>), <% } -%> <% }) -%> } @@ -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 -%>) , <% } -%> <% }) -%> }