-
Notifications
You must be signed in to change notification settings - Fork 52
fix: recursive ToJSON #174
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
Open
johannes-lindgren
wants to merge
6
commits into
colyseus:master
Choose a base branch
from
johannes-lindgren:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
caa651b
fix: recursive ToJSON
johannes-lindgren 58b014b
fix: recursive ToJSON
johannes-lindgren 160428e
fix: implementation for all types
johannes-lindgren 9af6637
chore: remove unused imports
johannes-lindgren 0591a6d
fix: recursive schemas with ToJson
johannes-lindgren f208847
refactor: remove duplicate utility type
johannes-lindgren File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// Used to check if two types are equal. | ||
// If they are equal, the expression evaluates to true, otherwise false | ||
export type Equals<T1, T2> = T1 extends T2 | ||
? T2 extends T1 | ||
? true | ||
: false | ||
: false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,236 @@ | ||
import { ArraySchema, CollectionSchema, MapSchema, Schema, SetSchema, ToJSON, type } from "../../src"; | ||
import { Equals } from "../helpers/Equals"; | ||
|
||
// Reused across multiple tests | ||
export class VecSchema extends Schema { | ||
@type('number') x: number | ||
@type('number') y: number | ||
} | ||
|
||
describe("ToJSON type tests", () => { | ||
it("Omits methods", () => { | ||
class C extends Schema { | ||
@type('number') time: number | ||
rewind(arg: number){} | ||
} | ||
const _t1: Equals<ToJSON<C>, { | ||
time: number | ||
}> = true | ||
}) | ||
|
||
it("Does not transform primitive types", () => { | ||
// Primitive types have methods, and these should not be omitted | ||
const _tString: Equals<ToJSON<string>, string> = true | ||
const _tNumber: Equals<ToJSON<number>, number> = true | ||
const _tBoolean: Equals<ToJSON<boolean>, boolean> = true | ||
const _tBigInt: Equals<ToJSON<bigint>, bigint> = true | ||
const _tSymbol: Equals<ToJSON<symbol>, symbol> = true | ||
const _tUndefined: Equals<ToJSON<undefined>, undefined> = true | ||
const _tNull: Equals<ToJSON<null>, null> = true | ||
}) | ||
|
||
it("Does not transform non-schema types", () => { | ||
class C extends Schema { | ||
time: number | ||
pos: { x: number, y: number } | ||
} | ||
const _t1: Equals<ToJSON<C>, { | ||
time: number | ||
pos: { x: number, y: number } | ||
}> = true | ||
const _t2: ToJSON<C> = { | ||
time: 1, | ||
pos: { x: 1, y: 2 } | ||
} | ||
}) | ||
|
||
it("Primitive type on root", () => { | ||
class C extends Schema { | ||
@type('number') time: number | ||
@type('string') name: string | ||
} | ||
const _t1: Equals<ToJSON<C>, { | ||
time: number | ||
name: string | ||
}> = true | ||
const _t2: ToJSON<C> = { | ||
time: 1, | ||
name: "name" | ||
} | ||
}) | ||
|
||
it("Schema type on root", () => { | ||
class C extends Schema { | ||
@type(VecSchema) ballPos: VecSchema | ||
} | ||
const _t1: Equals<ToJSON<C>, { | ||
ballPos: { | ||
x: number | ||
y: number | ||
} | ||
}> = true | ||
const _t2: ToJSON<C> = { | ||
ballPos: { | ||
x: 1, | ||
y: 2 | ||
} | ||
} | ||
}) | ||
|
||
describe("MapSchema", () => { | ||
it("allows MapSchema on root", () => { | ||
class C extends Schema { | ||
@type({map: VecSchema}) positions: MapSchema<VecSchema> | ||
} | ||
|
||
const _t1: Equals<ToJSON<C>, { | ||
positions: Record<string, { | ||
x: number | ||
y: number | ||
}> | ||
}> = true | ||
const _t2: ToJSON<C> = { | ||
positions: { | ||
a: { | ||
x: 1, | ||
y: 2 | ||
} | ||
} | ||
} | ||
}) | ||
|
||
it("allows recursive schemas", () => { | ||
class C extends Schema { | ||
@type({ map: C }) mapToSelf: MapSchema<C> | ||
} | ||
type T1 = { | ||
mapToSelf: Record<string, T1> | ||
} | ||
const _t1: Equals<ToJSON<C>, T1> = true | ||
const _t2: ToJSON<C> = { | ||
mapToSelf: { | ||
a: { | ||
mapToSelf: { | ||
} | ||
} | ||
} | ||
} | ||
}) | ||
}) | ||
|
||
describe("ArraySchema", () => { | ||
it("allows ArraySchema on root", () => { | ||
class C extends Schema { | ||
@type({array: VecSchema}) positions: ArraySchema<VecSchema> | ||
} | ||
const _t1: Equals<ToJSON<C>, { | ||
positions: Array<{ | ||
x: number | ||
y: number | ||
}> | ||
}> = true | ||
const _t2: ToJSON<C> = { | ||
positions: [{ | ||
x: 1, | ||
y: 2 | ||
}] | ||
} | ||
}) | ||
|
||
it("allows recursive schemas", () => { | ||
class C extends Schema { | ||
@type({ array: C }) arrayOfSelf: ArraySchema<C> | ||
} | ||
type T1 = { | ||
arrayOfSelf: Array<T1> | ||
} | ||
const _t1: Equals<ToJSON<C>, T1> = true | ||
const _t2: ToJSON<C> = { | ||
arrayOfSelf: [{ | ||
arrayOfSelf: [] | ||
}, { | ||
arrayOfSelf: [{ | ||
arrayOfSelf: [] | ||
}] | ||
}] | ||
} | ||
}) | ||
}) | ||
|
||
describe("SetSchema", () => { | ||
it("allows SetSchema on root", () => { | ||
class C extends Schema { | ||
@type({set: VecSchema}) positions: SetSchema<VecSchema> | ||
} | ||
const _t1: Equals<ToJSON<C>, { | ||
positions: Array<{ | ||
x: number | ||
y: number | ||
}> | ||
}> = true | ||
const _t2: ToJSON<C> = { | ||
positions: [{ | ||
x: 1, | ||
y: 2 | ||
}] | ||
} | ||
}) | ||
it("allows recursive schemas", () => { | ||
class C extends Schema { | ||
@type({ set: C }) setOfSelf: SetSchema<C> | ||
} | ||
type T1 = { | ||
setOfSelf: Array<T1> | ||
} | ||
const _t1: Equals<ToJSON<C>, T1> = true | ||
const _t2: ToJSON<C> = { | ||
setOfSelf: [{ | ||
setOfSelf: [] | ||
}, { | ||
setOfSelf: [{ | ||
setOfSelf: [] | ||
}] | ||
}] | ||
} | ||
}) | ||
}) | ||
|
||
describe("CollectionSchema", () => { | ||
it("allows CollectionSchema on root", () => { | ||
class C extends Schema { | ||
@type({collection: VecSchema}) positions: CollectionSchema<VecSchema> | ||
} | ||
const _t1: Equals<ToJSON<C>, { | ||
positions: Array<{ | ||
x: number | ||
y: number | ||
}> | ||
}> = true | ||
const _t2: ToJSON<C> = { | ||
positions: [{ | ||
x: 1, | ||
y: 2 | ||
}] | ||
} | ||
}) | ||
|
||
it("allows recursive schemas", () => { | ||
class C extends Schema { | ||
@type({ collection: C }) collectionOfSelf: CollectionSchema<C> | ||
} | ||
type T1 = { | ||
collectionOfSelf: Array<T1> | ||
} | ||
const _t1: Equals<ToJSON<C>, T1> = true | ||
const _t2: ToJSON<C> = { | ||
collectionOfSelf: [{ | ||
collectionOfSelf: [] | ||
}, { | ||
collectionOfSelf: [{ | ||
collectionOfSelf: [] | ||
}] | ||
}] | ||
} | ||
}) | ||
}) | ||
}) |
4 changes: 2 additions & 2 deletions
4
test/TypeScriptTypes.test.ts → test/src/TypeScriptTypes.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 understand this part: why is there an underscore?