Skip to content

Commit

Permalink
fix: schema parents should be added properly
Browse files Browse the repository at this point in the history
  • Loading branch information
JairusSW committed Jul 22, 2024
1 parent 1049bf7 commit faf2110
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 30 deletions.
47 changes: 21 additions & 26 deletions assembly/test.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,28 @@
// import { JSON } from ".";
import { JSON } from ".";
@json
class Vec3 {
x: f32 = 0.0;
y: f32 = 0.0;
z: f32 = 0.0;
class Message {
constructor(role: string, content: string) {
this._role = role;
this.content = content;
}


@alias("role")
protected _role: string;

get role(): string {
return this._role;
}

content: string;
}

@json
class Player {
firstName!: string;
lastName!: string;
lastActive!: i32[];
age!: i32;
pos!: JSON.Raw;
isVerified!: boolean;
class UserMessage extends Message {
constructor(content: string) {
super("user", content);
}
}

const player: Player = {
firstName: "Emmet",
lastName: "West",
lastActive: [8, 27, 2022],
age: 23,
pos: "{\"x\":3.4,\"y\":1.2,\"z\":8.3}",
isVerified: true
};

const stringified = JSON.stringify<Player>(player);
console.log(stringified);
console.log(idof<JSON.Raw>().toString());
console.log(idof<string>().toString())
// const parsed = JSON.parse<Player>(stringified);
console.log(JSON.stringify(new Message("user", "foo")))
console.log(JSON.stringify(new UserMessage("foo")));
5 changes: 3 additions & 2 deletions transform/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ class JSONTransform extends BaseVisitor {
if (node.extendsType) {
schema.parent = this.schemasList.find((v) => v.name == node.extendsType?.name.identifier.text);
if (schema.parent?.members) {
for (let i = 0; i < schema.parent.members.length; i++) {
for (let i = schema.parent.members.length - 1; i >= 0; i--) {
const replace = schema.members.find((v) => v.name == schema.parent?.members[i]?.name);
if (!replace) {
schema.members.unshift(schema.parent.members[i]);
//schema.members.unshift(schema.parent?.members[i]!);
members.unshift(schema.parent?.members[i].node);
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions transform/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,13 @@ class JSONTransform extends BaseVisitor {
) as SchemaData | null;

if (schema.parent?.members) {
for (let i = 0; i < schema.parent.members.length; i++) {
for (let i = schema.parent.members.length - 1; i >= 0; i--) {
const replace = schema.members.find(
(v) => v.name == schema.parent?.members[i]?.name
);
if (!replace) {
schema.members.unshift(schema.parent.members[i]!);
//schema.members.unshift(schema.parent?.members[i]!);
members.unshift(schema.parent?.members[i]!.node);
}
}
}
Expand Down

0 comments on commit faf2110

Please sign in to comment.