From d02e1cb1c6d7147a1aa4118360baf349b2e1d3e9 Mon Sep 17 00:00:00 2001 From: alvrs Date: Wed, 25 Oct 2023 19:35:19 +0100 Subject: [PATCH 01/22] feat(store): add experimental resolveConfig util --- packages/store/mud.config.ts | 31 ++++++++++++- .../ts/config/experimental/resolveConfig.ts | 45 +++++++++++++++++++ 2 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 packages/store/ts/config/experimental/resolveConfig.ts diff --git a/packages/store/mud.config.ts b/packages/store/mud.config.ts index 7fcfeab1e3..95ed34e3cb 100644 --- a/packages/store/mud.config.ts +++ b/packages/store/mud.config.ts @@ -1,6 +1,7 @@ +import { resolveConfig } from "./ts/config/experimental/resolveConfig"; import { mudConfig } from "./ts/register"; -export default mudConfig({ +const config = mudConfig({ storeImportPath: "../../", namespace: "store", userTypes: { @@ -47,5 +48,33 @@ export default mudConfig({ }, tableIdArgument: true, }, + Shorthand: { + keySchema: { + key: "ResourceId", + }, + valueSchema: "ResourceId", + }, }, }); + +export default config; + +const resolvedConfig = resolveConfig(config); + +resolvedConfig.tables.Shorthand.keySchema.key; +// ^? + +resolvedConfig.tables.Shorthand.valueSchema.value; +// ^? + +resolvedConfig.resolved.tables.Shorthand.keySchema.key.type; +// ^? + +resolvedConfig.resolved.tables.Shorthand.keySchema.key.internalType; +// ^? + +resolvedConfig.resolved.tables.Shorthand.valueSchema.value.type; +// ^? + +resolvedConfig.resolved.tables.Shorthand.valueSchema.value.internalType; +// ^? diff --git a/packages/store/ts/config/experimental/resolveConfig.ts b/packages/store/ts/config/experimental/resolveConfig.ts new file mode 100644 index 0000000000..179e767e33 --- /dev/null +++ b/packages/store/ts/config/experimental/resolveConfig.ts @@ -0,0 +1,45 @@ +import { StoreConfig, TableConfig, UserTypesConfig } from "../storeConfig"; +import { UserType } from "@latticexyz/common/codegen"; + +export type ResolvedStoreConfig = TStoreConfig & { + resolved: { + tables: { + [key in keyof TStoreConfig["tables"]]: ResolvedTableConfig< + TStoreConfig["tables"][key], + TStoreConfig["userTypes"] + >; + }; + }; +}; + +export type ResolvedTableConfig = Omit< + TTableConfig, + "keySchema" | "valueSchema" +> & { + keySchema: ResolvedSchema; + valueSchema: ResolvedSchema; +}; + +export type ResolvedSchema< + TSchema extends TableConfig["keySchema"] | TableConfig["valueSchema"], + TUserTypes extends UserTypesConfig +> = { + [key in keyof TSchema]: { + type: TSchema[key] extends keyof TUserTypes + ? TUserTypes[TSchema[key]] extends UserType + ? // Note: we mistakenly named the plain ABI type "internalType", + // while in Solidity ABIs the plain ABI type is called "type" and + // and the custom type "internalType". We're planning to + // change our version and align with Solidity ABIs going forward. + TUserTypes[TSchema[key]]["internalType"] + : never + : TSchema[key]; + internalType: TSchema[key]; + }; +}; + +export function resolveConfig( + config: TStoreConfig +): ResolvedStoreConfig { + return config as ResolvedStoreConfig; +} From 170ceb79167526eab48801427a2d5b0246282561 Mon Sep 17 00:00:00 2001 From: alvrs Date: Wed, 25 Oct 2023 19:35:58 +0100 Subject: [PATCH 02/22] remove dev code --- packages/store/mud.config.ts | 25 +------------------------ 1 file changed, 1 insertion(+), 24 deletions(-) diff --git a/packages/store/mud.config.ts b/packages/store/mud.config.ts index 95ed34e3cb..a6033497ab 100644 --- a/packages/store/mud.config.ts +++ b/packages/store/mud.config.ts @@ -1,7 +1,6 @@ -import { resolveConfig } from "./ts/config/experimental/resolveConfig"; import { mudConfig } from "./ts/register"; -const config = mudConfig({ +export default mudConfig({ storeImportPath: "../../", namespace: "store", userTypes: { @@ -56,25 +55,3 @@ const config = mudConfig({ }, }, }); - -export default config; - -const resolvedConfig = resolveConfig(config); - -resolvedConfig.tables.Shorthand.keySchema.key; -// ^? - -resolvedConfig.tables.Shorthand.valueSchema.value; -// ^? - -resolvedConfig.resolved.tables.Shorthand.keySchema.key.type; -// ^? - -resolvedConfig.resolved.tables.Shorthand.keySchema.key.internalType; -// ^? - -resolvedConfig.resolved.tables.Shorthand.valueSchema.value.type; -// ^? - -resolvedConfig.resolved.tables.Shorthand.valueSchema.value.internalType; -// ^? From 45a96bd6758848cb64ac70e5b8e227ef890bdb20 Mon Sep 17 00:00:00 2001 From: alvrs Date: Wed, 25 Oct 2023 19:55:48 +0100 Subject: [PATCH 03/22] add enum support --- packages/store/mud.config.ts | 30 +++++++++++++++++-- .../ts/config/experimental/resolveConfig.ts | 22 +++++++++----- 2 files changed, 42 insertions(+), 10 deletions(-) diff --git a/packages/store/mud.config.ts b/packages/store/mud.config.ts index a6033497ab..6468d86576 100644 --- a/packages/store/mud.config.ts +++ b/packages/store/mud.config.ts @@ -1,6 +1,7 @@ +import { resolveConfig } from "./ts/config/experimental/resolveConfig"; import { mudConfig } from "./ts/register"; -export default mudConfig({ +const config = mudConfig({ storeImportPath: "../../", namespace: "store", userTypes: { @@ -8,6 +9,9 @@ export default mudConfig({ FieldLayout: { filePath: "./src/FieldLayout.sol", internalType: "bytes32" }, Schema: { filePath: "./src/Schema.sol", internalType: "bytes32" }, }, + enums: { + Enum1: ["hello", "world"], + }, tables: { StoreHooks: { keySchema: { @@ -51,7 +55,29 @@ export default mudConfig({ keySchema: { key: "ResourceId", }, - valueSchema: "ResourceId", + valueSchema: "Enum1", }, }, }); + +export default config; + +const resolvedConfig = resolveConfig(config); + +resolvedConfig.tables.Shorthand.keySchema.key; +// ^? + +resolvedConfig.tables.Shorthand.valueSchema.value; +// ^? + +resolvedConfig.resolved.tables.Shorthand.keySchema.key.type; +// ^? + +resolvedConfig.resolved.tables.Shorthand.keySchema.key.internalType; +// ^? + +resolvedConfig.resolved.tables.Shorthand.valueSchema.value.type; +// ^? + +resolvedConfig.resolved.tables.Shorthand.valueSchema.value.internalType; +// ^? diff --git a/packages/store/ts/config/experimental/resolveConfig.ts b/packages/store/ts/config/experimental/resolveConfig.ts index 179e767e33..771e0efe24 100644 --- a/packages/store/ts/config/experimental/resolveConfig.ts +++ b/packages/store/ts/config/experimental/resolveConfig.ts @@ -1,3 +1,4 @@ +import { StringForUnion } from "@latticexyz/common/type-utils"; import { StoreConfig, TableConfig, UserTypesConfig } from "../storeConfig"; import { UserType } from "@latticexyz/common/codegen"; @@ -6,23 +7,26 @@ export type ResolvedStoreConfig = TStoreConfig tables: { [key in keyof TStoreConfig["tables"]]: ResolvedTableConfig< TStoreConfig["tables"][key], - TStoreConfig["userTypes"] + TStoreConfig["userTypes"], + keyof TStoreConfig["enums"] & string >; }; }; }; -export type ResolvedTableConfig = Omit< - TTableConfig, - "keySchema" | "valueSchema" -> & { - keySchema: ResolvedSchema; - valueSchema: ResolvedSchema; +export type ResolvedTableConfig< + TTableConfig extends TableConfig, + TUserTypes extends UserTypesConfig, + TEnumNames extends StringForUnion +> = Omit & { + keySchema: ResolvedSchema; + valueSchema: ResolvedSchema; }; export type ResolvedSchema< TSchema extends TableConfig["keySchema"] | TableConfig["valueSchema"], - TUserTypes extends UserTypesConfig + TUserTypes extends UserTypesConfig, + TEnumNames extends StringForUnion > = { [key in keyof TSchema]: { type: TSchema[key] extends keyof TUserTypes @@ -33,6 +37,8 @@ export type ResolvedSchema< // change our version and align with Solidity ABIs going forward. TUserTypes[TSchema[key]]["internalType"] : never + : TSchema[key] extends TEnumNames + ? "uint8" : TSchema[key]; internalType: TSchema[key]; }; From 5bcba0bef615911a612033f8bde9c270a69488eb Mon Sep 17 00:00:00 2001 From: alvrs Date: Wed, 25 Oct 2023 20:03:20 +0100 Subject: [PATCH 04/22] remove dev config --- packages/store/mud.config.ts | 31 +------------------------------ 1 file changed, 1 insertion(+), 30 deletions(-) diff --git a/packages/store/mud.config.ts b/packages/store/mud.config.ts index 6468d86576..a8df8d2e85 100644 --- a/packages/store/mud.config.ts +++ b/packages/store/mud.config.ts @@ -1,7 +1,6 @@ -import { resolveConfig } from "./ts/config/experimental/resolveConfig"; import { mudConfig } from "./ts/register"; -const config = mudConfig({ +export default mudConfig({ storeImportPath: "../../", namespace: "store", userTypes: { @@ -51,33 +50,5 @@ const config = mudConfig({ }, tableIdArgument: true, }, - Shorthand: { - keySchema: { - key: "ResourceId", - }, - valueSchema: "Enum1", - }, }, }); - -export default config; - -const resolvedConfig = resolveConfig(config); - -resolvedConfig.tables.Shorthand.keySchema.key; -// ^? - -resolvedConfig.tables.Shorthand.valueSchema.value; -// ^? - -resolvedConfig.resolved.tables.Shorthand.keySchema.key.type; -// ^? - -resolvedConfig.resolved.tables.Shorthand.keySchema.key.internalType; -// ^? - -resolvedConfig.resolved.tables.Shorthand.valueSchema.value.type; -// ^? - -resolvedConfig.resolved.tables.Shorthand.valueSchema.value.internalType; -// ^? From 3bf37f40b77779f0029a1635d93f75f62cd6f8e6 Mon Sep 17 00:00:00 2001 From: alvrs Date: Wed, 25 Oct 2023 20:03:45 +0100 Subject: [PATCH 05/22] remove test enum --- packages/store/mud.config.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/packages/store/mud.config.ts b/packages/store/mud.config.ts index a8df8d2e85..7fcfeab1e3 100644 --- a/packages/store/mud.config.ts +++ b/packages/store/mud.config.ts @@ -8,9 +8,6 @@ export default mudConfig({ FieldLayout: { filePath: "./src/FieldLayout.sol", internalType: "bytes32" }, Schema: { filePath: "./src/Schema.sol", internalType: "bytes32" }, }, - enums: { - Enum1: ["hello", "world"], - }, tables: { StoreHooks: { keySchema: { From 0bc6229cb5905121a808bab49d82e21b4892c667 Mon Sep 17 00:00:00 2001 From: alvrs Date: Wed, 25 Oct 2023 22:16:26 +0100 Subject: [PATCH 06/22] add actual resolver --- .../ts/config/experimental/resolveConfig.ts | 96 ++++++++++++++++++- 1 file changed, 92 insertions(+), 4 deletions(-) diff --git a/packages/store/ts/config/experimental/resolveConfig.ts b/packages/store/ts/config/experimental/resolveConfig.ts index 771e0efe24..bc1042c3d3 100644 --- a/packages/store/ts/config/experimental/resolveConfig.ts +++ b/packages/store/ts/config/experimental/resolveConfig.ts @@ -3,7 +3,8 @@ import { StoreConfig, TableConfig, UserTypesConfig } from "../storeConfig"; import { UserType } from "@latticexyz/common/codegen"; export type ResolvedStoreConfig = TStoreConfig & { - resolved: { + /** @deprecated Note: this property is experimental and expected to change */ + _resolved: { tables: { [key in keyof TStoreConfig["tables"]]: ResolvedTableConfig< TStoreConfig["tables"][key], @@ -19,10 +20,22 @@ export type ResolvedTableConfig< TUserTypes extends UserTypesConfig, TEnumNames extends StringForUnion > = Omit & { - keySchema: ResolvedSchema; - valueSchema: ResolvedSchema; + keySchema: ResolvedKeySchema; + valueSchema: ResolvedValueSchema; }; +export type ResolvedKeySchema< + TKeySchema extends TableConfig["keySchema"], + TUserTypes extends UserTypesConfig, + TEnumNames extends StringForUnion +> = ResolvedSchema; + +export type ResolvedValueSchema< + TValueSchema extends TableConfig["valueSchema"], + TUserTypes extends UserTypesConfig, + TEnumNames extends StringForUnion +> = ResolvedSchema; + export type ResolvedSchema< TSchema extends TableConfig["keySchema"] | TableConfig["valueSchema"], TUserTypes extends UserTypesConfig, @@ -47,5 +60,80 @@ export type ResolvedSchema< export function resolveConfig( config: TStoreConfig ): ResolvedStoreConfig { - return config as ResolvedStoreConfig; + const resolvedTables: Record> = {}; + + for (const key of Object.keys(config.tables)) { + resolvedTables[key] = resolveTable(config.tables[key], config.userTypes, Object.keys(config.enums)) as ReturnType< + typeof resolveTable + >; + } + + return { + ...config, + _resolved: { + tables: resolvedTables as ResolvedStoreConfig["_resolved"]["tables"], + }, + }; +} + +function resolveTable< + TTableConfig extends TableConfig, + TUserTypes extends UserTypesConfig, + TEnums extends StringForUnion[] +>( + tableConfig: TTableConfig, + userTypes: TUserTypes, + enums: TEnums +): ResolvedTableConfig { + return { + ...tableConfig, + keySchema: resolveKeySchema(tableConfig.keySchema ?? { key: "bytes32" }, userTypes, enums), + valueSchema: resolveValueSchema(tableConfig.valueSchema, userTypes, enums), + } as ResolvedTableConfig; +} + +function resolveKeySchema< + TKeySchema extends TableConfig["keySchema"], + TUserTypes extends UserTypesConfig, + TEnums extends StringForUnion[] +>( + keySchema: TKeySchema, + userTypes: TUserTypes, + enums: TEnums +): ResolvedKeySchema { + const schema = (keySchema ?? { key: "bytes32" }) as TKeySchema extends undefined ? { key: "bytes32" } : TKeySchema; + return resolveSchema(schema, userTypes, enums); +} + +function resolveValueSchema< + TValueSchema extends TableConfig["valueSchema"], + TUserTypes extends UserTypesConfig, + TEnums extends StringForUnion[] +>( + schema: TValueSchema, + userTypes: TUserTypes, + enums: TEnums +): ResolvedValueSchema { + return resolveSchema(schema, userTypes, enums); +} + +function resolveSchema< + TSchema extends NonNullable | TableConfig["valueSchema"], + TUserTypes extends UserTypesConfig, + TEnums extends StringForUnion[] +>(schema: TSchema, { userTypes }: TUserTypes, enums: TEnums): ResolvedSchema { + const resolvedSchema: Record = {}; + for (const [key, value] of Object.entries(schema)) { + resolvedSchema[key] = { + // This mirrors the logic in the `ResolvedSchema` type + type: + userTypes && key in userTypes + ? userTypes[key].internalType + : enums.includes(value) + ? ("uint8" as const) + : value, + internalType: value, + }; + } + return resolvedSchema as ResolvedValueSchema; } From 7ae1e57154198a243b08ac0e9c41b9061c1a7b19 Mon Sep 17 00:00:00 2001 From: alvrs Date: Thu, 26 Oct 2023 20:31:52 +0100 Subject: [PATCH 07/22] add type test --- .../experimental/resolveConfig.test-d.ts | 41 +++++++++++++++++++ .../config/experimental/resolveConfig.test.ts | 0 2 files changed, 41 insertions(+) create mode 100644 packages/store/ts/config/experimental/resolveConfig.test-d.ts create mode 100644 packages/store/ts/config/experimental/resolveConfig.test.ts diff --git a/packages/store/ts/config/experimental/resolveConfig.test-d.ts b/packages/store/ts/config/experimental/resolveConfig.test-d.ts new file mode 100644 index 0000000000..27ff3adf43 --- /dev/null +++ b/packages/store/ts/config/experimental/resolveConfig.test-d.ts @@ -0,0 +1,41 @@ +import { describe, expectTypeOf } from "vitest"; +import { mudConfig } from "../../register/mudConfig"; +import { resolveConfig } from "./resolveConfig"; + +const config = resolveConfig( + mudConfig({ + userTypes: { + ResourceId: { + internalType: "bytes32", + filePath: "", + }, + }, + enums: { + ResourceType: ["namespace", "system", "table"], + }, + tables: { + Shorthand: { + keySchema: { + key: "ResourceId", + }, + valueSchema: "ResourceType", + }, + }, + }) +); + +describe("resolveConfig", () => { + expectTypeOf().toEqualTypeOf<{ + key: { + internalType: "ResourceId"; + type: "bytes32"; + }; + }>(); + + expectTypeOf().toEqualTypeOf<{ + value: { + internalType: "ResourceType"; + type: "uint8"; + }; + }>(); +}); diff --git a/packages/store/ts/config/experimental/resolveConfig.test.ts b/packages/store/ts/config/experimental/resolveConfig.test.ts new file mode 100644 index 0000000000..e69de29bb2 From 8a018283e9549956dad3f8339693fec1a9167a40 Mon Sep 17 00:00:00 2001 From: alvrs Date: Thu, 26 Oct 2023 21:00:41 +0100 Subject: [PATCH 08/22] add tests and fixes --- .../config/experimental/resolveConfig.test.ts | 38 +++++++++++++++++++ .../ts/config/experimental/resolveConfig.ts | 36 +++++++++--------- 2 files changed, 57 insertions(+), 17 deletions(-) diff --git a/packages/store/ts/config/experimental/resolveConfig.test.ts b/packages/store/ts/config/experimental/resolveConfig.test.ts index e69de29bb2..cd684fde86 100644 --- a/packages/store/ts/config/experimental/resolveConfig.test.ts +++ b/packages/store/ts/config/experimental/resolveConfig.test.ts @@ -0,0 +1,38 @@ +import { describe, expect, it } from "vitest"; +import { mudConfig } from "../../register/mudConfig"; +import { resolveConfig } from "./resolveConfig"; +import { MUDCoreContext } from "@latticexyz/config"; +MUDCoreContext.createContext(); + +const config = resolveConfig( + mudConfig({ + userTypes: { + ResourceId: { + internalType: "bytes32", + filePath: "", + }, + }, + enums: { + ResourceType: ["namespace", "system", "table"], + }, + tables: { + Shorthand: { + keySchema: { + key: "ResourceId", + }, + valueSchema: "ResourceType", + }, + }, + }) +); + +describe("resolveConfig", () => { + it("should resolve userTypes and enums", () => { + expect(config._resolved.tables.Shorthand.keySchema).toEqual({ + key: { internalType: "ResourceId", type: "bytes32" }, + }); + expect(config._resolved.tables.Shorthand.valueSchema).toEqual({ + value: { internalType: "ResourceType", type: "uint8" }, + }); + }); +}); diff --git a/packages/store/ts/config/experimental/resolveConfig.ts b/packages/store/ts/config/experimental/resolveConfig.ts index bc1042c3d3..eae0ff72bc 100644 --- a/packages/store/ts/config/experimental/resolveConfig.ts +++ b/packages/store/ts/config/experimental/resolveConfig.ts @@ -17,7 +17,7 @@ export type ResolvedStoreConfig = TStoreConfig export type ResolvedTableConfig< TTableConfig extends TableConfig, - TUserTypes extends UserTypesConfig, + TUserTypes extends UserTypesConfig["userTypes"], TEnumNames extends StringForUnion > = Omit & { keySchema: ResolvedKeySchema; @@ -26,19 +26,19 @@ export type ResolvedTableConfig< export type ResolvedKeySchema< TKeySchema extends TableConfig["keySchema"], - TUserTypes extends UserTypesConfig, + TUserTypes extends UserTypesConfig["userTypes"], TEnumNames extends StringForUnion > = ResolvedSchema; export type ResolvedValueSchema< TValueSchema extends TableConfig["valueSchema"], - TUserTypes extends UserTypesConfig, + TUserTypes extends UserTypesConfig["userTypes"], TEnumNames extends StringForUnion > = ResolvedSchema; export type ResolvedSchema< TSchema extends TableConfig["keySchema"] | TableConfig["valueSchema"], - TUserTypes extends UserTypesConfig, + TUserTypes extends UserTypesConfig["userTypes"], TEnumNames extends StringForUnion > = { [key in keyof TSchema]: { @@ -78,7 +78,7 @@ export function resolveConfig( function resolveTable< TTableConfig extends TableConfig, - TUserTypes extends UserTypesConfig, + TUserTypes extends UserTypesConfig["userTypes"], TEnums extends StringForUnion[] >( tableConfig: TTableConfig, @@ -94,44 +94,46 @@ function resolveTable< function resolveKeySchema< TKeySchema extends TableConfig["keySchema"], - TUserTypes extends UserTypesConfig, + TUserTypes extends UserTypesConfig["userTypes"], TEnums extends StringForUnion[] >( keySchema: TKeySchema, userTypes: TUserTypes, enums: TEnums ): ResolvedKeySchema { - const schema = (keySchema ?? { key: "bytes32" }) as TKeySchema extends undefined ? { key: "bytes32" } : TKeySchema; + // It's concerning that the type says the schema is expanded, but in reality it's not + const schema = ( + keySchema == null ? { key: "bytes32" } : typeof keySchema === "string" ? { key: keySchema } : keySchema + ) as TKeySchema extends undefined ? { key: "bytes32" } : TKeySchema; return resolveSchema(schema, userTypes, enums); } function resolveValueSchema< TValueSchema extends TableConfig["valueSchema"], - TUserTypes extends UserTypesConfig, + TUserTypes extends UserTypesConfig["userTypes"], TEnums extends StringForUnion[] >( - schema: TValueSchema, + valueSchema: TValueSchema, userTypes: TUserTypes, enums: TEnums ): ResolvedValueSchema { + // It's concerning that the type says the schema is expanded, but in reality it's not + const schema = typeof valueSchema === "string" ? ({ value: valueSchema } as unknown as TValueSchema) : valueSchema; return resolveSchema(schema, userTypes, enums); } function resolveSchema< TSchema extends NonNullable | TableConfig["valueSchema"], - TUserTypes extends UserTypesConfig, + TUserTypes extends UserTypesConfig["userTypes"], TEnums extends StringForUnion[] ->(schema: TSchema, { userTypes }: TUserTypes, enums: TEnums): ResolvedSchema { +>(schema: TSchema, userTypes: TUserTypes, enums: TEnums): ResolvedSchema { const resolvedSchema: Record = {}; for (const [key, value] of Object.entries(schema)) { + const isUserType = userTypes && value in userTypes; + const isEnum = enums.includes(value); resolvedSchema[key] = { // This mirrors the logic in the `ResolvedSchema` type - type: - userTypes && key in userTypes - ? userTypes[key].internalType - : enums.includes(value) - ? ("uint8" as const) - : value, + type: isUserType ? userTypes[value].internalType : isEnum ? ("uint8" as const) : value, internalType: value, }; } From 390a282a5e375ad8fba5d4353744919ab1c139a7 Mon Sep 17 00:00:00 2001 From: alvrs Date: Thu, 26 Oct 2023 22:27:20 +0100 Subject: [PATCH 09/22] add mapObject util type --- packages/common/src/utils/index.ts | 1 + packages/common/src/utils/mapObject.test.ts | 23 +++++++++++++++++++++ packages/common/src/utils/mapObject.ts | 13 ++++++++++++ 3 files changed, 37 insertions(+) create mode 100644 packages/common/src/utils/mapObject.test.ts create mode 100644 packages/common/src/utils/mapObject.ts diff --git a/packages/common/src/utils/index.ts b/packages/common/src/utils/index.ts index 1ccd90fe05..c11ae82e51 100644 --- a/packages/common/src/utils/index.ts +++ b/packages/common/src/utils/index.ts @@ -8,6 +8,7 @@ export * from "./identity"; export * from "./isDefined"; export * from "./isNotNull"; export * from "./iteratorToArray"; +export * from "./mapObject"; export * from "./uniqueBy"; export * from "./wait"; export * from "./waitForIdle"; diff --git a/packages/common/src/utils/mapObject.test.ts b/packages/common/src/utils/mapObject.test.ts new file mode 100644 index 0000000000..a02ae55ded --- /dev/null +++ b/packages/common/src/utils/mapObject.test.ts @@ -0,0 +1,23 @@ +import { describe, expect, expectTypeOf, it } from "vitest"; +import { mapObject } from "./mapObject"; +import { assertExhaustive } from "./assertExhaustive"; + +describe("mapObject", () => { + it("should map the source to the target", () => { + const source = { + hello: "world", + foo: "bar", + } as const; + + type Mapped> = { [key in keyof T]: `mapped-${T[key]}` }; + + const target = mapObject>(source, (key) => { + if (key === "hello") return `mapped-${source[key]}`; + if (key === "foo") return `mapped-${source[key]}`; + assertExhaustive(key); + }); + + expect(target).toEqual({ hello: `mapped-world` }); + expectTypeOf().toEqualTypeOf>(); + }); +}); diff --git a/packages/common/src/utils/mapObject.ts b/packages/common/src/utils/mapObject.ts new file mode 100644 index 0000000000..82319852e9 --- /dev/null +++ b/packages/common/src/utils/mapObject.ts @@ -0,0 +1,13 @@ +/** + * Map each key of a source object via a given valueMap function + */ +export function mapObject, Target extends { [key in keyof Source]: unknown }>( + source: Source, + valueMap: (key: keyof Source, value: Source[typeof key]) => Target[typeof key] +): Target { + const target: Record = {}; + for (const [key, value] of Object.entries(source)) { + target[key] = valueMap(key, value as Source[keyof Source]); + } + return target as Target; +} From 55bb2277b0d1023ea08e2c0fac30194d46e41eb0 Mon Sep 17 00:00:00 2001 From: alvrs Date: Fri, 27 Oct 2023 00:45:55 +0200 Subject: [PATCH 10/22] clean up --- packages/common/src/utils/mapObject.ts | 8 ++-- .../ts/config/experimental/resolveConfig.ts | 44 +++++++++++-------- 2 files changed, 30 insertions(+), 22 deletions(-) diff --git a/packages/common/src/utils/mapObject.ts b/packages/common/src/utils/mapObject.ts index 82319852e9..15c286f6af 100644 --- a/packages/common/src/utils/mapObject.ts +++ b/packages/common/src/utils/mapObject.ts @@ -1,10 +1,10 @@ /** * Map each key of a source object via a given valueMap function */ -export function mapObject, Target extends { [key in keyof Source]: unknown }>( - source: Source, - valueMap: (key: keyof Source, value: Source[typeof key]) => Target[typeof key] -): Target { +export function mapObject< + Source extends Record, + Target extends { [key in keyof Source]: unknown } +>(source: Source, valueMap: (key: keyof Source, value: Source[typeof key]) => Target[typeof key]): Target { const target: Record = {}; for (const [key, value] of Object.entries(source)) { target[key] = valueMap(key, value as Source[keyof Source]); diff --git a/packages/store/ts/config/experimental/resolveConfig.ts b/packages/store/ts/config/experimental/resolveConfig.ts index eae0ff72bc..a97eb24550 100644 --- a/packages/store/ts/config/experimental/resolveConfig.ts +++ b/packages/store/ts/config/experimental/resolveConfig.ts @@ -1,6 +1,7 @@ import { StringForUnion } from "@latticexyz/common/type-utils"; import { StoreConfig, TableConfig, UserTypesConfig } from "../storeConfig"; import { UserType } from "@latticexyz/common/codegen"; +import { mapObject } from "@latticexyz/common/utils"; export type ResolvedStoreConfig = TStoreConfig & { /** @deprecated Note: this property is experimental and expected to change */ @@ -34,10 +35,10 @@ export type ResolvedValueSchema< TValueSchema extends TableConfig["valueSchema"], TUserTypes extends UserTypesConfig["userTypes"], TEnumNames extends StringForUnion -> = ResolvedSchema; +> = ResolvedSchema, TUserTypes, TEnumNames>; export type ResolvedSchema< - TSchema extends TableConfig["keySchema"] | TableConfig["valueSchema"], + TSchema extends Exclude, TUserTypes extends UserTypesConfig["userTypes"], TEnumNames extends StringForUnion > = { @@ -84,12 +85,18 @@ function resolveTable< tableConfig: TTableConfig, userTypes: TUserTypes, enums: TEnums -): ResolvedTableConfig { +): ResolvedTableConfig { + const { keySchema: _, valueSchema: __, ...rest } = tableConfig; + return { - ...tableConfig, - keySchema: resolveKeySchema(tableConfig.keySchema ?? { key: "bytes32" }, userTypes, enums), - valueSchema: resolveValueSchema(tableConfig.valueSchema, userTypes, enums), - } as ResolvedTableConfig; + ...rest, + keySchema: resolveKeySchema(tableConfig.keySchema, userTypes, enums), + valueSchema: resolveValueSchema(tableConfig.valueSchema, userTypes, enums) as ResolvedSchema< + Exclude, + TUserTypes, + TEnums[number] + >, + }; } function resolveKeySchema< @@ -101,7 +108,6 @@ function resolveKeySchema< userTypes: TUserTypes, enums: TEnums ): ResolvedKeySchema { - // It's concerning that the type says the schema is expanded, but in reality it's not const schema = ( keySchema == null ? { key: "bytes32" } : typeof keySchema === "string" ? { key: keySchema } : keySchema ) as TKeySchema extends undefined ? { key: "bytes32" } : TKeySchema; @@ -117,25 +123,27 @@ function resolveValueSchema< userTypes: TUserTypes, enums: TEnums ): ResolvedValueSchema { - // It's concerning that the type says the schema is expanded, but in reality it's not - const schema = typeof valueSchema === "string" ? ({ value: valueSchema } as unknown as TValueSchema) : valueSchema; + const schema = ( + typeof valueSchema === "string" ? ({ value: valueSchema } as unknown as TValueSchema) : valueSchema + ) as Exclude; return resolveSchema(schema, userTypes, enums); } function resolveSchema< - TSchema extends NonNullable | TableConfig["valueSchema"], + TSchema extends Exclude | TableConfig["valueSchema"], string>, TUserTypes extends UserTypesConfig["userTypes"], TEnums extends StringForUnion[] >(schema: TSchema, userTypes: TUserTypes, enums: TEnums): ResolvedSchema { - const resolvedSchema: Record = {}; - for (const [key, value] of Object.entries(schema)) { + return mapObject>(schema, (key, value) => { const isUserType = userTypes && value in userTypes; const isEnum = enums.includes(value); - resolvedSchema[key] = { - // This mirrors the logic in the `ResolvedSchema` type - type: isUserType ? userTypes[value].internalType : isEnum ? ("uint8" as const) : value, + return { + type: (isUserType ? userTypes[value].internalType : isEnum ? ("uint8" as const) : value) as ResolvedSchema< + TSchema, + TUserTypes, + TEnums[number] + >[typeof key]["type"], internalType: value, }; - } - return resolvedSchema as ResolvedValueSchema; + }); } From 6aa8b3a90a53d0009634a5a656996832f80dd82c Mon Sep 17 00:00:00 2001 From: alvrs Date: Fri, 27 Oct 2023 11:19:11 +0200 Subject: [PATCH 11/22] minor clean up --- packages/store/ts/config/experimental/resolveConfig.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/store/ts/config/experimental/resolveConfig.ts b/packages/store/ts/config/experimental/resolveConfig.ts index a97eb24550..395c8610c6 100644 --- a/packages/store/ts/config/experimental/resolveConfig.ts +++ b/packages/store/ts/config/experimental/resolveConfig.ts @@ -86,12 +86,12 @@ function resolveTable< userTypes: TUserTypes, enums: TEnums ): ResolvedTableConfig { - const { keySchema: _, valueSchema: __, ...rest } = tableConfig; + const { keySchema, valueSchema, ...rest } = tableConfig; return { ...rest, - keySchema: resolveKeySchema(tableConfig.keySchema, userTypes, enums), - valueSchema: resolveValueSchema(tableConfig.valueSchema, userTypes, enums) as ResolvedSchema< + keySchema: resolveKeySchema(keySchema, userTypes, enums), + valueSchema: resolveValueSchema(valueSchema, userTypes, enums) as ResolvedSchema< Exclude, TUserTypes, TEnums[number] From 5ecd86815ee9ea4291121fac85254018b547776a Mon Sep 17 00:00:00 2001 From: alvrs Date: Fri, 27 Oct 2023 11:20:12 +0200 Subject: [PATCH 12/22] fix test --- packages/common/src/utils/mapObject.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/common/src/utils/mapObject.test.ts b/packages/common/src/utils/mapObject.test.ts index a02ae55ded..ac015109e9 100644 --- a/packages/common/src/utils/mapObject.test.ts +++ b/packages/common/src/utils/mapObject.test.ts @@ -17,7 +17,7 @@ describe("mapObject", () => { assertExhaustive(key); }); - expect(target).toEqual({ hello: `mapped-world` }); + expect(target).toEqual({ hello: `mapped-world`, foo: `mapped-bar` }); expectTypeOf().toEqualTypeOf>(); }); }); From c894a6d8c4486d3c3348c766fc792b06802d24d5 Mon Sep 17 00:00:00 2001 From: alvrs Date: Fri, 27 Oct 2023 12:04:01 +0200 Subject: [PATCH 13/22] add namespace, name and tableId --- .../experimental/resolveConfig.test-d.ts | 10 +++++ .../config/experimental/resolveConfig.test.ts | 11 ++++++ .../ts/config/experimental/resolveConfig.ts | 37 ++++++++++++++----- 3 files changed, 49 insertions(+), 9 deletions(-) diff --git a/packages/store/ts/config/experimental/resolveConfig.test-d.ts b/packages/store/ts/config/experimental/resolveConfig.test-d.ts index 27ff3adf43..9dd10407ba 100644 --- a/packages/store/ts/config/experimental/resolveConfig.test-d.ts +++ b/packages/store/ts/config/experimental/resolveConfig.test-d.ts @@ -4,6 +4,10 @@ import { resolveConfig } from "./resolveConfig"; const config = resolveConfig( mudConfig({ + // Seems like we need `as const` here to keep the strong type. + // Note it resolves to the strong `""` type if no namespace is provided. + // TODO: require the entire input config to be `const` + namespace: "the-namespace" as const, userTypes: { ResourceId: { internalType: "bytes32", @@ -25,6 +29,12 @@ const config = resolveConfig( ); describe("resolveConfig", () => { + expectTypeOf().toEqualTypeOf<"the-namespace">(); + + expectTypeOf().toEqualTypeOf<"Shorthand">(); + + expectTypeOf().toEqualTypeOf<`0x${string}`>(); + expectTypeOf().toEqualTypeOf<{ key: { internalType: "ResourceId"; diff --git a/packages/store/ts/config/experimental/resolveConfig.test.ts b/packages/store/ts/config/experimental/resolveConfig.test.ts index cd684fde86..313df7594d 100644 --- a/packages/store/ts/config/experimental/resolveConfig.test.ts +++ b/packages/store/ts/config/experimental/resolveConfig.test.ts @@ -2,10 +2,12 @@ import { describe, expect, it } from "vitest"; import { mudConfig } from "../../register/mudConfig"; import { resolveConfig } from "./resolveConfig"; import { MUDCoreContext } from "@latticexyz/config"; +import { resourceToHex } from "@latticexyz/common"; MUDCoreContext.createContext(); const config = resolveConfig( mudConfig({ + namespace: "the-namespace", userTypes: { ResourceId: { internalType: "bytes32", @@ -28,9 +30,18 @@ const config = resolveConfig( describe("resolveConfig", () => { it("should resolve userTypes and enums", () => { + expect(config._resolved.tables.Shorthand.namespace).toEqual("the-namespace"); + + expect(config._resolved.tables.Shorthand.name).toEqual("Shorthand"); + + expect(config._resolved.tables.Shorthand.tableId).toEqual( + resourceToHex({ type: "table", namespace: "the-namespace", name: "Shorthand" }) + ); + expect(config._resolved.tables.Shorthand.keySchema).toEqual({ key: { internalType: "ResourceId", type: "bytes32" }, }); + expect(config._resolved.tables.Shorthand.valueSchema).toEqual({ value: { internalType: "ResourceType", type: "uint8" }, }); diff --git a/packages/store/ts/config/experimental/resolveConfig.ts b/packages/store/ts/config/experimental/resolveConfig.ts index 395c8610c6..ed72f1c99a 100644 --- a/packages/store/ts/config/experimental/resolveConfig.ts +++ b/packages/store/ts/config/experimental/resolveConfig.ts @@ -2,15 +2,18 @@ import { StringForUnion } from "@latticexyz/common/type-utils"; import { StoreConfig, TableConfig, UserTypesConfig } from "../storeConfig"; import { UserType } from "@latticexyz/common/codegen"; import { mapObject } from "@latticexyz/common/utils"; +import { resourceToHex } from "@latticexyz/common"; export type ResolvedStoreConfig = TStoreConfig & { /** @deprecated Note: this property is experimental and expected to change */ _resolved: { tables: { - [key in keyof TStoreConfig["tables"]]: ResolvedTableConfig< - TStoreConfig["tables"][key], + [TableKey in keyof TStoreConfig["tables"] & string]: ResolvedTableConfig< + TStoreConfig["tables"][TableKey], TStoreConfig["userTypes"], - keyof TStoreConfig["enums"] & string + keyof TStoreConfig["enums"] & string, + TStoreConfig["namespace"], + TableKey >; }; }; @@ -19,10 +22,15 @@ export type ResolvedStoreConfig = TStoreConfig export type ResolvedTableConfig< TTableConfig extends TableConfig, TUserTypes extends UserTypesConfig["userTypes"], - TEnumNames extends StringForUnion + TEnumNames extends StringForUnion, + TNamespace extends string = string, + TName extends string = string > = Omit & { keySchema: ResolvedKeySchema; valueSchema: ResolvedValueSchema; + namespace: TNamespace; + name: TName; + tableId: `0x${string}`; }; export type ResolvedKeySchema< @@ -64,9 +72,13 @@ export function resolveConfig( const resolvedTables: Record> = {}; for (const key of Object.keys(config.tables)) { - resolvedTables[key] = resolveTable(config.tables[key], config.userTypes, Object.keys(config.enums)) as ReturnType< - typeof resolveTable - >; + resolvedTables[key] = resolveTable( + config.tables[key], + config.userTypes, + Object.keys(config.enums), + config.namespace, + key + ) as ReturnType; } return { @@ -80,11 +92,15 @@ export function resolveConfig( function resolveTable< TTableConfig extends TableConfig, TUserTypes extends UserTypesConfig["userTypes"], - TEnums extends StringForUnion[] + TEnums extends StringForUnion[], + TNamespace extends string, + TName extends string >( tableConfig: TTableConfig, userTypes: TUserTypes, - enums: TEnums + enums: TEnums, + namespace: TNamespace, + name: TName ): ResolvedTableConfig { const { keySchema, valueSchema, ...rest } = tableConfig; @@ -96,6 +112,9 @@ function resolveTable< TUserTypes, TEnums[number] >, + namespace, + name, + tableId: resourceToHex({ type: "table", namespace, name }), }; } From 5fe80f47e6b764de0e055f96d49de88e62d8bfdc Mon Sep 17 00:00:00 2001 From: alvarius Date: Fri, 27 Oct 2023 12:06:07 +0200 Subject: [PATCH 14/22] Create three-scissors-smile.md --- .changeset/three-scissors-smile.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/three-scissors-smile.md diff --git a/.changeset/three-scissors-smile.md b/.changeset/three-scissors-smile.md new file mode 100644 index 0000000000..6626fe6a83 --- /dev/null +++ b/.changeset/three-scissors-smile.md @@ -0,0 +1,5 @@ +--- +"@latticexyz/common": minor +--- + +Added a `mapObject` helper to map the value of each property of an object to a new value. From fc2560c98e8041dd246bc742a85d963902734f9b Mon Sep 17 00:00:00 2001 From: alvarius Date: Fri, 27 Oct 2023 12:08:54 +0200 Subject: [PATCH 15/22] Create dirty-cows-lie.md --- .changeset/dirty-cows-lie.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changeset/dirty-cows-lie.md diff --git a/.changeset/dirty-cows-lie.md b/.changeset/dirty-cows-lie.md new file mode 100644 index 0000000000..8ccdd12348 --- /dev/null +++ b/.changeset/dirty-cows-lie.md @@ -0,0 +1,6 @@ +--- +"@latticexyz/store": minor +--- + +Added an experimental `resolveConfig` helper to refine the output type of `mudConfig` and simplify downstream consumption. +Note that it's not recommended to use this helper externally yet, since the format is expected to change soon while we're refactoring the config parsing. From 839f31377cefd1f09b8c31f311e4f713f5aa71c5 Mon Sep 17 00:00:00 2001 From: alvrs Date: Fri, 27 Oct 2023 12:11:27 +0200 Subject: [PATCH 16/22] update mapObject --- packages/common/src/utils/mapObject.ts | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/packages/common/src/utils/mapObject.ts b/packages/common/src/utils/mapObject.ts index 15c286f6af..03ddf4e49a 100644 --- a/packages/common/src/utils/mapObject.ts +++ b/packages/common/src/utils/mapObject.ts @@ -5,9 +5,7 @@ export function mapObject< Source extends Record, Target extends { [key in keyof Source]: unknown } >(source: Source, valueMap: (key: keyof Source, value: Source[typeof key]) => Target[typeof key]): Target { - const target: Record = {}; - for (const [key, value] of Object.entries(source)) { - target[key] = valueMap(key, value as Source[keyof Source]); - } - return target as Target; + return Object.fromEntries( + Object.entries(source).map(([key, value]) => [key, valueMap(key, value as Source[keyof Source])]) + ) as Target; } From 30d1d1307fd3019049cdc64baa5b24e8eef4f5ae Mon Sep 17 00:00:00 2001 From: alvrs Date: Fri, 27 Oct 2023 12:14:29 +0200 Subject: [PATCH 17/22] reverse argument order on mapObject --- packages/common/src/utils/mapObject.test.ts | 6 +++--- packages/common/src/utils/mapObject.ts | 4 ++-- packages/store/ts/config/experimental/resolveConfig.ts | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/common/src/utils/mapObject.test.ts b/packages/common/src/utils/mapObject.test.ts index ac015109e9..9247791364 100644 --- a/packages/common/src/utils/mapObject.test.ts +++ b/packages/common/src/utils/mapObject.test.ts @@ -11,9 +11,9 @@ describe("mapObject", () => { type Mapped> = { [key in keyof T]: `mapped-${T[key]}` }; - const target = mapObject>(source, (key) => { - if (key === "hello") return `mapped-${source[key]}`; - if (key === "foo") return `mapped-${source[key]}`; + const target = mapObject>(source, (value, key) => { + if (key === "hello") return `mapped-${value}`; + if (key === "foo") return `mapped-${value}`; assertExhaustive(key); }); diff --git a/packages/common/src/utils/mapObject.ts b/packages/common/src/utils/mapObject.ts index 03ddf4e49a..189f568657 100644 --- a/packages/common/src/utils/mapObject.ts +++ b/packages/common/src/utils/mapObject.ts @@ -4,8 +4,8 @@ export function mapObject< Source extends Record, Target extends { [key in keyof Source]: unknown } ->(source: Source, valueMap: (key: keyof Source, value: Source[typeof key]) => Target[typeof key]): Target { +>(source: Source, valueMap: (value: Source[typeof key], key: keyof Source) => Target[typeof key]): Target { return Object.fromEntries( - Object.entries(source).map(([key, value]) => [key, valueMap(key, value as Source[keyof Source])]) + Object.entries(source).map(([key, value]) => [key, valueMap(value as Source[keyof Source], key)]) ) as Target; } diff --git a/packages/store/ts/config/experimental/resolveConfig.ts b/packages/store/ts/config/experimental/resolveConfig.ts index ed72f1c99a..0601c76384 100644 --- a/packages/store/ts/config/experimental/resolveConfig.ts +++ b/packages/store/ts/config/experimental/resolveConfig.ts @@ -153,7 +153,7 @@ function resolveSchema< TUserTypes extends UserTypesConfig["userTypes"], TEnums extends StringForUnion[] >(schema: TSchema, userTypes: TUserTypes, enums: TEnums): ResolvedSchema { - return mapObject>(schema, (key, value) => { + return mapObject>(schema, (value, key) => { const isUserType = userTypes && value in userTypes; const isEnum = enums.includes(value); return { From 81ebb25e1b4728559086913c2c46ffee13e82420 Mon Sep 17 00:00:00 2001 From: alvrs Date: Fri, 27 Oct 2023 12:16:51 +0200 Subject: [PATCH 18/22] remove nested _resolve key --- .../experimental/resolveConfig.test-d.ts | 10 +++---- .../config/experimental/resolveConfig.test.ts | 10 +++---- .../ts/config/experimental/resolveConfig.ts | 26 +++++++------------ 3 files changed, 20 insertions(+), 26 deletions(-) diff --git a/packages/store/ts/config/experimental/resolveConfig.test-d.ts b/packages/store/ts/config/experimental/resolveConfig.test-d.ts index 9dd10407ba..1390bbd9fb 100644 --- a/packages/store/ts/config/experimental/resolveConfig.test-d.ts +++ b/packages/store/ts/config/experimental/resolveConfig.test-d.ts @@ -29,20 +29,20 @@ const config = resolveConfig( ); describe("resolveConfig", () => { - expectTypeOf().toEqualTypeOf<"the-namespace">(); + expectTypeOf().toEqualTypeOf<"the-namespace">(); - expectTypeOf().toEqualTypeOf<"Shorthand">(); + expectTypeOf().toEqualTypeOf<"Shorthand">(); - expectTypeOf().toEqualTypeOf<`0x${string}`>(); + expectTypeOf().toEqualTypeOf<`0x${string}`>(); - expectTypeOf().toEqualTypeOf<{ + expectTypeOf().toEqualTypeOf<{ key: { internalType: "ResourceId"; type: "bytes32"; }; }>(); - expectTypeOf().toEqualTypeOf<{ + expectTypeOf().toEqualTypeOf<{ value: { internalType: "ResourceType"; type: "uint8"; diff --git a/packages/store/ts/config/experimental/resolveConfig.test.ts b/packages/store/ts/config/experimental/resolveConfig.test.ts index 313df7594d..d6187ca73c 100644 --- a/packages/store/ts/config/experimental/resolveConfig.test.ts +++ b/packages/store/ts/config/experimental/resolveConfig.test.ts @@ -30,19 +30,19 @@ const config = resolveConfig( describe("resolveConfig", () => { it("should resolve userTypes and enums", () => { - expect(config._resolved.tables.Shorthand.namespace).toEqual("the-namespace"); + expect(config.tables.Shorthand.namespace).toEqual("the-namespace"); - expect(config._resolved.tables.Shorthand.name).toEqual("Shorthand"); + expect(config.tables.Shorthand.name).toEqual("Shorthand"); - expect(config._resolved.tables.Shorthand.tableId).toEqual( + expect(config.tables.Shorthand.tableId).toEqual( resourceToHex({ type: "table", namespace: "the-namespace", name: "Shorthand" }) ); - expect(config._resolved.tables.Shorthand.keySchema).toEqual({ + expect(config.tables.Shorthand.keySchema).toEqual({ key: { internalType: "ResourceId", type: "bytes32" }, }); - expect(config._resolved.tables.Shorthand.valueSchema).toEqual({ + expect(config.tables.Shorthand.valueSchema).toEqual({ value: { internalType: "ResourceType", type: "uint8" }, }); }); diff --git a/packages/store/ts/config/experimental/resolveConfig.ts b/packages/store/ts/config/experimental/resolveConfig.ts index 0601c76384..81b3198620 100644 --- a/packages/store/ts/config/experimental/resolveConfig.ts +++ b/packages/store/ts/config/experimental/resolveConfig.ts @@ -4,18 +4,15 @@ import { UserType } from "@latticexyz/common/codegen"; import { mapObject } from "@latticexyz/common/utils"; import { resourceToHex } from "@latticexyz/common"; -export type ResolvedStoreConfig = TStoreConfig & { - /** @deprecated Note: this property is experimental and expected to change */ - _resolved: { - tables: { - [TableKey in keyof TStoreConfig["tables"] & string]: ResolvedTableConfig< - TStoreConfig["tables"][TableKey], - TStoreConfig["userTypes"], - keyof TStoreConfig["enums"] & string, - TStoreConfig["namespace"], - TableKey - >; - }; +export type ResolvedStoreConfig = { + tables: { + [TableKey in keyof TStoreConfig["tables"] & string]: ResolvedTableConfig< + TStoreConfig["tables"][TableKey], + TStoreConfig["userTypes"], + keyof TStoreConfig["enums"] & string, + TStoreConfig["namespace"], + TableKey + >; }; }; @@ -82,10 +79,7 @@ export function resolveConfig( } return { - ...config, - _resolved: { - tables: resolvedTables as ResolvedStoreConfig["_resolved"]["tables"], - }, + tables: resolvedTables as ResolvedStoreConfig["tables"], }; } From 6f659fa6cd51026e56f06e74c179d1977b1a06ee Mon Sep 17 00:00:00 2001 From: alvrs Date: Fri, 27 Oct 2023 12:19:13 +0200 Subject: [PATCH 19/22] remove internal changeset --- .changeset/dirty-cows-lie.md | 6 ------ 1 file changed, 6 deletions(-) delete mode 100644 .changeset/dirty-cows-lie.md diff --git a/.changeset/dirty-cows-lie.md b/.changeset/dirty-cows-lie.md deleted file mode 100644 index 8ccdd12348..0000000000 --- a/.changeset/dirty-cows-lie.md +++ /dev/null @@ -1,6 +0,0 @@ ---- -"@latticexyz/store": minor ---- - -Added an experimental `resolveConfig` helper to refine the output type of `mudConfig` and simplify downstream consumption. -Note that it's not recommended to use this helper externally yet, since the format is expected to change soon while we're refactoring the config parsing. From 22ab29904fa0ec59367a788b0041731c3c45bfb0 Mon Sep 17 00:00:00 2001 From: alvrs Date: Fri, 27 Oct 2023 12:20:14 +0200 Subject: [PATCH 20/22] add const to store and world --- packages/store/mud.config.ts | 2 +- packages/world/mud.config.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/store/mud.config.ts b/packages/store/mud.config.ts index 7fcfeab1e3..63463c29f7 100644 --- a/packages/store/mud.config.ts +++ b/packages/store/mud.config.ts @@ -2,7 +2,7 @@ import { mudConfig } from "./ts/register"; export default mudConfig({ storeImportPath: "../../", - namespace: "store", + namespace: "store" as const, userTypes: { ResourceId: { filePath: "./src/ResourceId.sol", internalType: "bytes32" }, FieldLayout: { filePath: "./src/FieldLayout.sol", internalType: "bytes32" }, diff --git a/packages/world/mud.config.ts b/packages/world/mud.config.ts index dbafd51d2a..bd15594a38 100644 --- a/packages/world/mud.config.ts +++ b/packages/world/mud.config.ts @@ -4,7 +4,7 @@ export default mudConfig({ worldImportPath: "../../", worldgenDirectory: "interfaces", worldInterfaceName: "IBaseWorld", - namespace: "world", // NOTE: this namespace is only used for tables, the core system is deployed in the root namespace. + namespace: "world" as const, // NOTE: this namespace is only used for tables, the core system is deployed in the root namespace. userTypes: { ResourceId: { filePath: "@latticexyz/store/src/ResourceId.sol", internalType: "bytes32" }, }, From 4f601039af650f54257d4e748f5338651bfae962 Mon Sep 17 00:00:00 2001 From: alvarius Date: Fri, 27 Oct 2023 12:23:00 +0200 Subject: [PATCH 21/22] Update packages/store/ts/config/experimental/resolveConfig.ts Co-authored-by: Kevin Ingersoll --- packages/store/ts/config/experimental/resolveConfig.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/store/ts/config/experimental/resolveConfig.ts b/packages/store/ts/config/experimental/resolveConfig.ts index 81b3198620..86f113c332 100644 --- a/packages/store/ts/config/experimental/resolveConfig.ts +++ b/packages/store/ts/config/experimental/resolveConfig.ts @@ -63,6 +63,11 @@ export type ResolvedSchema< }; }; + +/** + * @internal Internal only + * @deprecated Internal only + */ export function resolveConfig( config: TStoreConfig ): ResolvedStoreConfig { From 5295bb2a04374541e6ac3c4979c43087cfa3a333 Mon Sep 17 00:00:00 2001 From: alvrs Date: Fri, 27 Oct 2023 13:17:08 +0200 Subject: [PATCH 22/22] prettier --- packages/store/ts/config/experimental/resolveConfig.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/store/ts/config/experimental/resolveConfig.ts b/packages/store/ts/config/experimental/resolveConfig.ts index 86f113c332..b02cb2e713 100644 --- a/packages/store/ts/config/experimental/resolveConfig.ts +++ b/packages/store/ts/config/experimental/resolveConfig.ts @@ -63,7 +63,6 @@ export type ResolvedSchema< }; }; - /** * @internal Internal only * @deprecated Internal only