Skip to content

Commit

Permalink
fix: #71
Browse files Browse the repository at this point in the history
  • Loading branch information
JairusSW committed Jun 9, 2024
1 parent 371b885 commit 1ece076
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 13 deletions.
25 changes: 25 additions & 0 deletions assembly/__tests__/as-json.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,32 @@ function canSer<T>(data: T, toBe: string): void {
const serialized = JSON.stringify<T>(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;
Expand Down
27 changes: 22 additions & 5 deletions assembly/test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
import { JSON } from "./src/json";

console.log(JSON.stringify(JSON.parse<f64[]>(`[
1,
2,
3
]`)));
@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))
17 changes: 13 additions & 4 deletions transform/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
17 changes: 13 additions & 4 deletions transform/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 1ece076

Please sign in to comment.