From 1ece076c565f99414c0f7b93fb79bb7a0b85824e Mon Sep 17 00:00:00 2001 From: JairusSW Date: Sat, 8 Jun 2024 21:29:54 -0700 Subject: [PATCH] fix: #71 --- assembly/__tests__/as-json.spec.ts | 25 +++++++++++++++++++++++++ assembly/test.ts | 27 ++++++++++++++++++++++----- transform/lib/index.js | 17 +++++++++++++---- transform/src/index.ts | 17 +++++++++++++---- 4 files changed, 73 insertions(+), 13 deletions(-) diff --git a/assembly/__tests__/as-json.spec.ts b/assembly/__tests__/as-json.spec.ts index 9ba6638..6a85941 100644 --- a/assembly/__tests__/as-json.spec.ts +++ b/assembly/__tests__/as-json.spec.ts @@ -14,7 +14,32 @@ function canSer(data: T, toBe: string): void { const serialized = JSON.stringify(data); expect(serialized).toBe(toBe); } +@json +class BaseObject { + a: string; + + constructor(a: string) { + this.a = a; + } +} + +@json +class DerivedObject extends BaseObject { + b: string; + + constructor(a: string, b: string) { + super(a); + this.b = b; + } +} +describe("Ser/de object hierarchies", () => { + it("should ser/de objects derived from base objects", () => { + const o = new DerivedObject("1", "2"); + const s = '{"a":"1","b":"2"}'; + canSerde(o, s); + }); +}); @json class Map4 { a: string; diff --git a/assembly/test.ts b/assembly/test.ts index 119e278..45a1537 100644 --- a/assembly/test.ts +++ b/assembly/test.ts @@ -1,7 +1,24 @@ import { JSON } from "./src/json"; -console.log(JSON.stringify(JSON.parse(`[ - 1, - 2, - 3 - ]`))); \ No newline at end of file +@json +class BaseObject { + a: string; + + constructor(a: string) { + this.a = a; + } +} + +@json +class DerivedObject extends BaseObject { + b: string; + + constructor(a: string, b: string) { + super(a); + this.b = b; + } +} + +const o = new DerivedObject("1", "2"); + +console.log(JSON.stringify(o)) \ No newline at end of file diff --git a/transform/lib/index.js b/transform/lib/index.js index ae4caa6..5c6c5f9 100644 --- a/transform/lib/index.js +++ b/transform/lib/index.js @@ -53,18 +53,27 @@ class AsJSONTransform extends BaseVisitor { setDataStmts: [], initializeStmts: [] }; - if (this.currentClass.parent.length > 0) { + if (this.currentClass.parent.length) { const parentSchema = this.schemasList.find((v) => v.name == this.currentClass.parent); if (parentSchema === null || parentSchema === void 0 ? void 0 : parentSchema.encodeStmts) { parentSchema === null || parentSchema === void 0 ? void 0 : parentSchema.encodeStmts.push((parentSchema === null || parentSchema === void 0 ? void 0 : parentSchema.encodeStmts.pop()) + ","); - this.currentClass.encodeStmts.push(...parentSchema === null || parentSchema === void 0 ? void 0 : parentSchema.encodeStmts); + for (let i = 0; i < parentSchema.keys.length; i++) { + const key = parentSchema.keys[i]; + if (node.members.filter(v => v.name.text == key) == undefined) + this.currentClass.encodeStmts.unshift(parentSchema.encodeStmts[i]); + } } } const parentSchema = this.schemasList.find((v) => v.name == this.currentClass.parent); const members = [ - ...node.members, - ...(parentSchema ? parentSchema.node.members : []), + ...node.members ]; + if (parentSchema) { + for (const mem of parentSchema.node.members) { + if (members.find(v => v.name === mem.name) == undefined) + members.unshift(mem); + } + } for (const mem of members) { // @ts-ignore if (mem.type && mem.type.name && mem.type.name.identifier.text) { diff --git a/transform/src/index.ts b/transform/src/index.ts index f0ec014..eca1888 100644 --- a/transform/src/index.ts +++ b/transform/src/index.ts @@ -61,24 +61,33 @@ class AsJSONTransform extends BaseVisitor { initializeStmts: [] }; - if (this.currentClass.parent.length > 0) { + if (this.currentClass.parent.length) { const parentSchema = this.schemasList.find( (v) => v.name == this.currentClass.parent ); if (parentSchema?.encodeStmts) { parentSchema?.encodeStmts.push(parentSchema?.encodeStmts.pop() + ","); - this.currentClass.encodeStmts.push(...parentSchema?.encodeStmts); + for (let i = 0; i < parentSchema.keys.length; i++) { + const key = parentSchema.keys[i]; + if (node.members.filter(v => v.name.text == key) == undefined) this.currentClass.encodeStmts.unshift(parentSchema.encodeStmts[i]!); + } } } const parentSchema = this.schemasList.find( (v) => v.name == this.currentClass.parent ); + const members = [ - ...node.members, - ...(parentSchema ? parentSchema.node.members : []), + ...node.members ]; + if (parentSchema) { + for (const mem of parentSchema.node.members) { + if (members.find(v => v.name === mem.name) == undefined) members.unshift(mem); + } + } + for (const mem of members) { // @ts-ignore if (mem.type && mem.type.name && mem.type.name.identifier.text) {