Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Casting and support arrays #30

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Contributor Author

@bhelx bhelx Oct 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cast can apply caster to any value. If the value is an array, then it will apply caster to every value and return a new array. this saves us from having to put this logic into the codegen. However i don't think this will scale to all use cases.

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 -%>.fromJson, 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
Loading