-
Notifications
You must be signed in to change notification settings - Fork 1
feat: add support for maps #36
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
Changes from all commits
ae626de
fb224b9
02fd6fd
0f0038e
65b3f29
492b697
b09ed5c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,30 @@ function cast(caster: (v: any) => any, v: any): any { | |
| return caster(v) | ||
| } | ||
|
|
||
| export function mapFromJson<T>(obj: any, cast: (value: any) => T): Record<string, T> { | ||
| const result: Record<string, T> = {}; | ||
|
|
||
| if (!obj) return result | ||
|
|
||
| for (const [key, value] of Object.entries(obj)) { | ||
| result[key] = cast(value); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure it's safe to mutate this and we may need to copy. we'll be changing the type of a property that a user my still be holding right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We are mutating the local copy |
||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| export function mapToJson<T>(obj: Record<string, T>, cast: (value: T) => any): any { | ||
| const result: Record<string, any> = {}; | ||
|
|
||
| if (!obj) return result | ||
|
|
||
| for (const [key, value] of Object.entries(obj)) { | ||
| result[key] = cast(value); | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| function dateToJson(v: Date): string { | ||
| return v.toISOString() | ||
| } | ||
|
|
@@ -48,31 +72,21 @@ export class <%- schema.name %> { | |
| return { | ||
| ...obj, | ||
| <% schema.properties.forEach(p => { -%> | ||
| <% 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 -%>), | ||
| <% let prop = propertyParsingSnippet(p) -%> | ||
| <% if (prop) { -%> | ||
| <%- p.name -%>: <%- prop %>, | ||
| <% } -%> | ||
| <% }) -%> | ||
| } | ||
| } | ||
|
|
||
| static toJson(obj: <%- schema.name %>): any{ | ||
| static toJson(obj: <%- schema.name %>): any { | ||
| return { | ||
| ...obj, | ||
| <% schema.properties.forEach(p => { -%> | ||
| <% 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 -%>), | ||
| <% let prop = propertyToJsonSnippet(p) -%> | ||
| <% if (prop) { -%> | ||
| <%- p.name %>: <%- prop %>, | ||
| <% } -%> | ||
| <% }) -%> | ||
| } | ||
|
|
@@ -117,6 +131,9 @@ export function <%- imp.name %>(<%- imp.input ? `input: ${toTypeScriptType(imp.i | |
| <% if (isJsonEncoded(imp.input)) { -%> | ||
| <% if (isPrimitive(imp.input)) { %> | ||
| const mem = Memory.fromJsonObject(input as any) | ||
| <% } else if (isMap(imp.input)) { %> | ||
| const serialized = <%- mapToJsonSnippet('input', imp.input) %> | ||
| const mem = Memory.fromJsonObject(serialized) | ||
| <% } else { %> | ||
| const casted = <%- toTypeScriptType(imp.input) %>.toJson(input) | ||
| const mem = Memory.fromJsonObject(casted) | ||
|
|
@@ -138,6 +155,9 @@ export function <%- imp.name %>(<%- imp.input ? `input: ${toTypeScriptType(imp.i | |
| <% if (isJsonEncoded(imp.output)) { -%> | ||
| <% if (isPrimitive(imp.output)) { -%> | ||
| return Memory.find(ptr).readJsonObject(); | ||
| <% } else if (isMap(imp.output)) { -%> | ||
| const output = Memory.find(ptr).readJsonObject(); | ||
| return <%- mapParsingSnippet('output', imp.output) %> | ||
| <% } else { -%> | ||
| const output = Memory.find(ptr).readJsonObject(); | ||
| return <%- toTypeScriptType(imp.output) %>.fromJson(output) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why use Record over Map? my understanding is Record is pretty limited to the key and value types it can hold.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think i see the logic here. looks like keys can only really be strings anyway.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't have a strong opinion here, but it seems like
Recordhas a more straightforward path for json serialization: https://stackoverflow.com/questions/50153172/how-to-serialize-a-map-in-javascript