From b4f221b44fcc7aa7e2197b01be164bf5cd1d7620 Mon Sep 17 00:00:00 2001 From: Benjamin Eckel Date: Thu, 12 Sep 2024 12:30:36 -0500 Subject: [PATCH 1/4] Add support for base64 encoded buffer properties --- src/index.ts | 8 ++++++++ template/src/pdk.ts.ejs | 8 ++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index f400c94..e6b2a97 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,6 +5,8 @@ function needsCasting(p: Property | Parameter): boolean { if (p.$ref) return true switch (p.type) { + case "buffer": + return true case "string": if (p.format === 'date-time') return true return false @@ -61,11 +63,17 @@ function toTypeScriptType(property: Property | Parameter): string { return `${tp} | null` } +// TODO: can move this helper up to shared library? +function isBuffer(property: Property | Parameter): boolean { + return property.type === 'buffer' +} + export function render() { const tmpl = Host.inputString() const ctx = { ...getContext(), ...helpers, + isBuffer, toTypeScriptType, needsCasting, } diff --git a/template/src/pdk.ts.ejs b/template/src/pdk.ts.ejs index f7cb3dd..01279ec 100644 --- a/template/src/pdk.ts.ejs +++ b/template/src/pdk.ts.ejs @@ -27,8 +27,10 @@ export class <%- schema.name %> { <% if (needsCasting(p)) { -%> <% if (isDateTime(p)) { -%> <%- p.name -%>: obj.<%- p.name -%> ? new Date(obj.<%- p.name -%>) : null, + <% } else if (isBuffer(p)) {-%> + <%- p.name -%>: obj.<%- p.name -%> ? Host.base64ToArrayBuffer(obj.<%- p.name -%>) : null, <% } else if (!isPrimitive(p)) {-%> - <%- p.name -%>: obj.<%- p.name -%> ? <%- p.$ref.name %>.fromJson(obj.<%- p.name %>) : null, + <%- p.name -%>: obj.<%- p.name -%> ? <%- p.$ref.name %>.fromJson(obj.<%- p.name -%>) : null, <% } -%> <% } -%> <% }) -%> @@ -42,8 +44,10 @@ export class <%- schema.name %> { <% if (needsCasting(p)) { -%> <% if (p.type === "string" && p.format === "date-time") { -%> <%- p.name -%>: obj.<%- p.name -%> ? obj.<%- p.name %>.toISOString() : null, + <% } else if (isBuffer(p)) {-%> + <%- p.name -%>: obj.<%- p.name -%> ? Host.arrayBufferToBase64(obj.<%- p.name -%>) : null, <% } 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 -%> ? <%- p.$ref.name %>.toJson(obj.<%- p.name -%>) : null, <% } -%> <% } -%> <% }) -%> From ca9daf723e0e7713414664c606b645e8f5a72d90 Mon Sep 17 00:00:00 2001 From: Benjamin Eckel Date: Thu, 12 Sep 2024 13:02:37 -0500 Subject: [PATCH 2/4] Top level support --- template/src/index.ts.ejs | 8 ++++++-- tests/schemas/fruit.yaml | 16 ++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/template/src/index.ts.ejs b/template/src/index.ts.ejs index cbbf878..ee39b55 100644 --- a/template/src/index.ts.ejs +++ b/template/src/index.ts.ejs @@ -11,7 +11,9 @@ import { export function <%- ex.name %>(): number { <% if (ex.input) { -%> <% if (isJsonEncoded(ex.input)) { -%> - <% if (isPrimitive(ex.input)) { -%> + <% if (isBuffer(ex.input)) { -%> + const input: <%- toTypeScriptType(ex.input) %> = Host.base64ToArrayBuffer(JSON.parse(Host.inputString())) + <% } else if (isPrimitive(ex.input)) { -%> const input: <%- toTypeScriptType(ex.input) %> = JSON.parse(Host.inputString()) <% } else { -%> const untypedInput = JSON.parse(Host.inputString()) @@ -38,7 +40,9 @@ export function <%- ex.name %>(): number { <% if (ex.output) { -%> <% if (isJsonEncoded(ex.output)) { -%> - <% if (isPrimitive(ex.output)) { -%> + <% if (isBuffer(ex.output)) { -%> + Host.outputString(JSON.stringify(Host.arrayBufferToBase64(output))) + <% } else if (isPrimitive(ex.output)) { -%> Host.outputString(JSON.stringify(output)) <% } else { -%> const untypedOutput = <%- toTypeScriptType(ex.output) %>.toJson(output) diff --git a/tests/schemas/fruit.yaml b/tests/schemas/fruit.yaml index bdf0d1f..d38c41a 100644 --- a/tests/schemas/fruit.yaml +++ b/tests/schemas/fruit.yaml @@ -1,4 +1,20 @@ exports: + topLevelBuffJSON: + description: Top level json buffers + input: + type: buffer + contentType: application/json + output: + type: buffer + contentType: application/json + topLevelBuffRaw: + description: Top level json buffers + input: + type: buffer + contentType: application/x-binary + output: + type: buffer + contentType: application/x-binary voidFunc: description: "This demonstrates how you can create an export with\nno inputs or outputs. \n" From c34fcb3267defd35c8350aee8345c8e81b45b2b4 Mon Sep 17 00:00:00 2001 From: Benjamin Eckel Date: Thu, 12 Sep 2024 13:45:54 -0500 Subject: [PATCH 3/4] refactor: we dont actually need this function --- src/index.ts | 14 -------------- template/src/pdk.ts.ejs | 28 ++++++++++++---------------- 2 files changed, 12 insertions(+), 30 deletions(-) diff --git a/src/index.ts b/src/index.ts index e6b2a97..fc6f543 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,20 +1,6 @@ import ejs from 'ejs' import { helpers, getContext, Property, Parameter } from "@dylibso/xtp-bindgen" -function needsCasting(p: Property | Parameter): boolean { - if (p.$ref) return true - - switch (p.type) { - case "buffer": - return true - case "string": - if (p.format === 'date-time') return true - return false - default: - return false - } -} - function toTypeScriptType(property: Property | Parameter): string { let tp if (property.$ref) { diff --git a/template/src/pdk.ts.ejs b/template/src/pdk.ts.ejs index 01279ec..1e0f402 100644 --- a/template/src/pdk.ts.ejs +++ b/template/src/pdk.ts.ejs @@ -24,14 +24,12 @@ export class <%- schema.name %> { return { ...obj, <% schema.properties.forEach(p => { -%> - <% if (needsCasting(p)) { -%> - <% if (isDateTime(p)) { -%> - <%- p.name -%>: obj.<%- p.name -%> ? new Date(obj.<%- p.name -%>) : null, - <% } else if (isBuffer(p)) {-%> - <%- p.name -%>: obj.<%- p.name -%> ? Host.base64ToArrayBuffer(obj.<%- p.name -%>) : null, - <% } else if (!isPrimitive(p)) {-%> - <%- p.name -%>: obj.<%- p.name -%> ? <%- p.$ref.name %>.fromJson(obj.<%- p.name -%>) : null, - <% } -%> + <% if (isDateTime(p)) { -%> + <%- p.name -%>: obj.<%- p.name -%> ? new Date(obj.<%- p.name -%>) : null, + <% } else if (isBuffer(p)) {-%> + <%- p.name -%>: obj.<%- p.name -%> ? Host.base64ToArrayBuffer(obj.<%- p.name -%>) : null, + <% } else if (!isPrimitive(p)) {-%> + <%- p.name -%>: obj.<%- p.name -%> ? <%- p.$ref.name %>.fromJson(obj.<%- p.name -%>) : null, <% } -%> <% }) -%> } @@ -41,14 +39,12 @@ export class <%- schema.name %> { return { ...obj, <% schema.properties.forEach(p => { -%> - <% if (needsCasting(p)) { -%> - <% if (p.type === "string" && p.format === "date-time") { -%> - <%- p.name -%>: obj.<%- p.name -%> ? obj.<%- p.name %>.toISOString() : null, - <% } else if (isBuffer(p)) {-%> - <%- p.name -%>: obj.<%- p.name -%> ? Host.arrayBufferToBase64(obj.<%- p.name -%>) : null, - <% } else if (p.$ref && !p.$ref.enum) {-%> - <%- p.name -%>: obj.<%- p.name -%> ? <%- p.$ref.name %>.toJson(obj.<%- p.name -%>) : null, - <% } -%> + <% if (p.type === "string" && p.format === "date-time") { -%> + <%- p.name -%>: obj.<%- p.name -%> ? obj.<%- p.name %>.toISOString() : null, + <% } else if (isBuffer(p)) {-%> + <%- p.name -%>: obj.<%- p.name -%> ? Host.arrayBufferToBase64(obj.<%- p.name -%>) : null, + <% } else if (p.$ref && !p.$ref.enum) {-%> + <%- p.name -%>: obj.<%- p.name -%> ? <%- p.$ref.name %>.toJson(obj.<%- p.name -%>) : null, <% } -%> <% }) -%> } From fcfe56a66472fc504d51e712e2e68e2f69bbed69 Mon Sep 17 00:00:00 2001 From: Benjamin Eckel Date: Thu, 12 Sep 2024 13:47:16 -0500 Subject: [PATCH 4/4] bump: Bump version 15 --- package-lock.json | 4 ++-- package.json | 2 +- src/index.ts | 1 - 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/package-lock.json b/package-lock.json index 4a8ff52..6fd8952 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "xtp-typescript-bindgen", - "version": "0.0.12", + "version": "0.0.15", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "xtp-typescript-bindgen", - "version": "0.0.12", + "version": "0.0.15", "license": "BSD-3-Clause", "dependencies": { "@dylibso/xtp-bindgen": "1.0.0-rc.5", diff --git a/package.json b/package.json index 56ba100..b621c16 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xtp-typescript-bindgen", - "version": "0.0.12", + "version": "0.0.15", "description": "XTP TypeScript bindgen plugin", "main": "dist/index.js", "scripts": { diff --git a/src/index.ts b/src/index.ts index fc6f543..3c65f66 100644 --- a/src/index.ts +++ b/src/index.ts @@ -61,7 +61,6 @@ export function render() { ...helpers, isBuffer, toTypeScriptType, - needsCasting, } const output = ejs.render(tmpl, ctx) Host.outputString(output)