Skip to content

Commit

Permalink
Add support for base64 encoded buffer properties (#22)
Browse files Browse the repository at this point in the history
* Add support for base64 encoded buffer properties

* Top level support

* refactor: we dont actually need this function

* bump: Bump version 15
  • Loading branch information
bhelx authored Sep 13, 2024
1 parent 996cc1b commit eb81879
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 30 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
19 changes: 6 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +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 "string":
if (p.format === 'date-time') return true
return false
default:
return false
}
}

function toTypeScriptType(property: Property | Parameter): string {
let tp
if (property.$ref) {
Expand Down Expand Up @@ -61,13 +49,18 @@ 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,
}
const output = ejs.render(tmpl, ctx)
Host.outputString(output)
Expand Down
8 changes: 6 additions & 2 deletions template/src/index.ts.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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)
Expand Down
24 changes: 12 additions & 12 deletions template/src/pdk.ts.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +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 (!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,
<% } -%>
<% }) -%>
}
Expand All @@ -39,12 +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 (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,
<% } -%>
<% }) -%>
}
Expand Down
16 changes: 16 additions & 0 deletions tests/schemas/fruit.yaml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down

0 comments on commit eb81879

Please sign in to comment.