Skip to content

Commit

Permalink
Refactor casting
Browse files Browse the repository at this point in the history
Refactors casting a bit. Fixes a bug where arrays were not being cast
correctly. I think this won't really scale to more complex types and we
should consider a dependency to handle this or code up a more
runtime dependent solution. But for now i'm aiming to
get it working well for the simple cases. Still have some tests to do.
  • Loading branch information
bhelx committed Oct 10, 2024
1 parent c9de0eb commit 210068d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 12 deletions.
52 changes: 40 additions & 12 deletions template/src/pdk.ts.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,30 @@
const hostFunctions = Host.getFunctions()
<% } %>

function isNull(v: any): boolean {
return v === undefined || v === null
}

function cast(caster: (v: any) => any, v: any): any {
if (isNull(v)) return v
if (Array.isArray(v)) return v.map(caster)
return caster(v)
}

function dateToJson(v: Date): string {
return v.toISOString()
}
function dateFromJson(v: string): Date {
return new Date(v)
}

function bufferToJson(v: ArrayBuffer): string {
return Host.arrayBufferToBase64(v)
}
function bufferFromJson(v: string): ArrayBuffer {
return Host.base64ToArrayBuffer(v)
}

<% Object.values(schema.schemas).forEach(schema => { %>
<% if (schema.properties.length > 0) { %>
Expand All @@ -24,12 +48,14 @@ export class <%- schema.name %> {
return {
...obj,
<% schema.properties.forEach(p => { -%>
<% if (isDateTime(p)) { -%>
<%- 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 -%> === undefined || obj.<%- p.name -%> === null ? obj.<%- p.name %> : Host.base64ToArrayBuffer(obj.<%- p.name -%>),
<% } else if (!isPrimitive(p)) {-%>
<%- p.name -%>: obj.<%- p.name -%> === undefined || obj.<%- p.name -%> === null ? obj.<%- p.name %> : <%- p.$ref.name %>.fromJson(obj.<%- p.name -%>),
<% let baseP = p.items ? p.items : p -%>
<% let baseRef = p.$ref ? p.$ref.name : (p.items && p.items.$ref ? p.items.$ref.name : null) -%>
<% if (isDateTime(baseP)) { -%>
<%- p.name -%>: cast(dateFromJson, obj.<%- p.name -%>),
<% } else if (isBuffer(baseP)) {-%>
<%- p.name -%>: cast(bufferFromJson, obj.<%- p.name -%>),
<% } else if (!isPrimitive(baseP)) {-%>
<%- p.name -%>: cast(<%- baseRef -%>.toJson, obj.<%- p.name -%>),
<% } -%>
<% }) -%>
}
Expand All @@ -39,12 +65,14 @@ export class <%- schema.name %> {
return {
...obj,
<% schema.properties.forEach(p => { -%>
<% if (p.type === "string" && p.format === "date-time") { -%>
<%- 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 -%> === 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 -%> === undefined || obj.<%- p.name -%> === null ? obj.<%- p.name %>: <%- p.$ref.name %>.toJson(obj.<%- p.name -%>) ,
<% let baseP = p.items ? p.items : p -%>
<% let baseRef = p.$ref ? p.$ref.name : (p.items && p.items.$ref ? p.items.$ref.name : null) -%>
<% if (isDateTime(baseP)) { -%>
<%- p.name -%>: cast(dateToJson, obj.<%- p.name -%>),
<% } else if (isBuffer(baseP)) {-%>
<%- p.name -%>: cast(bufferToJson, obj.<%- p.name -%>),
<% } else if (!isPrimitive(baseP)) {-%>
<%- p.name -%>: cast(<%- baseRef -%>.toJson, obj.<%- p.name -%>),
<% } -%>
<% }) -%>
}
Expand Down
5 changes: 5 additions & 0 deletions tests/schemas/fruit.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ components:
description: A set of all the enemies of pac-man
ComplexObject:
properties:
arrayOfDate:
type: array
items:
type: string
format: date-time
ghost:
"$ref": "#/components/schemas/GhostGang"
description: I can override the description for the property here
Expand Down

0 comments on commit 210068d

Please sign in to comment.