Skip to content

Commit

Permalink
MapSchema: force keys to be of string type. colyseus/colyseus#561
Browse files Browse the repository at this point in the history
  • Loading branch information
endel committed Jul 23, 2023
1 parent 8562af6 commit e29b4d2
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 16 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@colyseus/schema",
"version": "2.0.10",
"version": "2.0.11",
"description": "Binary state serializer with delta encoding for games",
"bin": {
"schema-codegen": "./bin/schema-codegen"
Expand Down
4 changes: 4 additions & 0 deletions src/types/MapSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ export class MapSchema<V=any, K extends string = string> implements Map<K, V>, S
throw new Error(`MapSchema#set('${key}', ${value}): trying to set ${value} value on '${key}'.`);
}

// Force "key" as string
// See: https://github.com/colyseus/colyseus/issues/561#issuecomment-1646733468
key = key.toString() as K;

// get "index" for this value.
const hasIndex = typeof(this.$changes.indexes[key]) !== "undefined";
const index = (hasIndex)
Expand Down
13 changes: 0 additions & 13 deletions test/ChangeTreeTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,6 @@ import { ChangeTree } from "../src/changes/ChangeTree";
import { Schema, type, MapSchema, ArraySchema } from "../src";

describe("ChangeTree", () => {
xit("should not error when updating a detached structure", async () => {
class Player extends Schema {
@type("number") pos: number = 0;
}
class State extends Schema {
@type({ map: Player }) players = new MapSchema<Player>();
}
const state = new State();
// @ts-ignore
state.players.set(3, new Player());
assert.doesNotThrow(() => state.encodeAll());
});

it("instances should share parent/root references", () => {
class Skill extends Schema {
@type("number") damage: number;
Expand Down
13 changes: 13 additions & 0 deletions test/MapSchemaTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@ describe("MapSchema Tests", () => {
assert.deepEqual(Array.from(decodedState.mapOfPlayers.keys()), ['jake', 'katarina']);
});

it("using number as key should not throw error", async () => {
class Player extends Schema {
@type("number") pos: number = 0;
}
class State extends Schema {
@type({ map: Player }) players = new MapSchema<Player>();
}
const state = new State();
// @ts-ignore
state.players.set(3, new Player());
assert.doesNotThrow(() => state.encodeAll());
});

it("forEach()", () => {
const map = new MapSchema<number>();
map.set('one', 1);
Expand Down

0 comments on commit e29b4d2

Please sign in to comment.