diff --git a/packages/store/ts/config/experimental/resolveConfig.test-d.ts b/packages/store/ts/config/experimental/resolveConfig.test-d.ts deleted file mode 100644 index 80d8c8a960..0000000000 --- a/packages/store/ts/config/experimental/resolveConfig.test-d.ts +++ /dev/null @@ -1,62 +0,0 @@ -import { describe, expectTypeOf } from "vitest"; -import { mudConfig } from "../../register/mudConfig"; -import { resolveConfig } from "./resolveConfig"; -import { Table } from "../../common"; -import storeConfig from "../../../mud.config"; - -describe("resolveConfig", () => { - describe("inline config", () => { - 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", - filePath: "", - }, - }, - enums: { - ResourceType: ["namespace", "system", "table"], - }, - tables: { - Shorthand: { - keySchema: { - key: "ResourceId", - }, - valueSchema: "ResourceType", - }, - }, - }), - ); - - expectTypeOf().toEqualTypeOf<"the-namespace">(); - - expectTypeOf().toEqualTypeOf<"Shorthand">(); - - expectTypeOf().toEqualTypeOf<`0x${string}`>(); - - expectTypeOf().toEqualTypeOf<{ - key: { - internalType: "ResourceId"; - type: "bytes32"; - }; - }>(); - - expectTypeOf().toEqualTypeOf<{ - value: { - internalType: "ResourceType"; - type: "uint8"; - }; - }>(); - - expectTypeOf().toMatchTypeOf(); - }); - - describe("store config", () => { - const config = resolveConfig(storeConfig); - expectTypeOf().toMatchTypeOf<{ type: "bytes" }>(); - }); -}); diff --git a/packages/store/ts/config/experimental/resolveConfig.test.ts b/packages/store/ts/config/experimental/resolveConfig.test.ts deleted file mode 100644 index bcfe78a855..0000000000 --- a/packages/store/ts/config/experimental/resolveConfig.test.ts +++ /dev/null @@ -1,49 +0,0 @@ -import { describe, expect, it } from "vitest"; -import { mudConfig } from "../../register/mudConfig"; -import { resolveConfig } from "./resolveConfig"; -import { MUDCoreContext } from "@latticexyz/config/library"; -import { resourceToHex } from "@latticexyz/common"; -MUDCoreContext.createContext(); - -const config = resolveConfig( - mudConfig({ - namespace: "the-namespace", - 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.tables.Shorthand.namespace).toEqual("the-namespace"); - - expect(config.tables.Shorthand.name).toEqual("Shorthand"); - - expect(config.tables.Shorthand.tableId).toEqual( - resourceToHex({ type: "table", namespace: "the-namespace", name: "Shorthand" }), - ); - - expect(config.tables.Shorthand.keySchema).toEqual({ - key: { internalType: "ResourceId", type: "bytes32" }, - }); - - 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 deleted file mode 100644 index 221377c5df..0000000000 --- a/packages/store/ts/config/experimental/resolveConfig.ts +++ /dev/null @@ -1,172 +0,0 @@ -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"; -import { SchemaAbiType } from "@latticexyz/schema-type/internal"; - -/** - * @internal Internal only - * @deprecated Internal only - */ -export type ResolvedStoreConfig = { - tables: { - [TableKey in keyof TStoreConfig["tables"] & string]: ResolvedTableConfig< - TStoreConfig["tables"][TableKey], - TStoreConfig["userTypes"], - keyof TStoreConfig["enums"] & string, - TStoreConfig["namespace"], - TableKey - >; - }; -}; - -type ResolvedTableConfig< - TTableConfig extends TableConfig, - TUserTypes extends UserTypesConfig["userTypes"], - TEnumNames extends StringForUnion, - TNamespace extends string = string, - TName extends string = string, -> = { - keySchema: ResolvedKeySchema; - valueSchema: ResolvedValueSchema; - namespace: TNamespace; - name: TName; - tableId: `0x${string}`; -}; - -type ResolvedKeySchema< - TKeySchema extends TableConfig["keySchema"], - TUserTypes extends UserTypesConfig["userTypes"], - TEnumNames extends StringForUnion, -> = ResolvedSchema; - -type ResolvedValueSchema< - TValueSchema extends TableConfig["valueSchema"], - TUserTypes extends UserTypesConfig["userTypes"], - TEnumNames extends StringForUnion, -> = ResolvedSchema, TUserTypes, TEnumNames>; - -type ResolvedSchema< - TSchema extends Exclude, - TUserTypes extends UserTypesConfig["userTypes"], - TEnumNames extends StringForUnion, -> = { - [key in keyof TSchema]: { - type: TSchema[key] extends SchemaAbiType - ? TSchema[key] - : 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] extends TEnumNames - ? "uint8" - : never; - internalType: TSchema[key]; - }; -}; - -/** - * @internal Internal only - * @deprecated Internal only - */ -export function resolveConfig( - config: TStoreConfig, -): ResolvedStoreConfig { - const resolvedTables: Record> = {}; - - for (const key of Object.keys(config.tables)) { - resolvedTables[key] = resolveTable( - config.tables[key], - config.userTypes, - Object.keys(config.enums), - config.namespace, - key, - ) as ReturnType; - } - - return { - tables: resolvedTables as ResolvedStoreConfig["tables"], - }; -} - -function resolveTable< - TTableConfig extends TableConfig, - TUserTypes extends UserTypesConfig["userTypes"], - TEnums extends StringForUnion[], - TNamespace extends string, - TName extends string, ->( - tableConfig: TTableConfig, - userTypes: TUserTypes, - enums: TEnums, - namespace: TNamespace, - name: TName, -): ResolvedTableConfig { - const { keySchema, valueSchema } = tableConfig; - - return { - keySchema: resolveKeySchema(keySchema, userTypes, enums), - valueSchema: resolveValueSchema(valueSchema, userTypes, enums) as ResolvedSchema< - Exclude, - TUserTypes, - TEnums[number] - >, - namespace, - name, - tableId: resourceToHex({ type: tableConfig.offchainOnly ? "offchainTable" : "table", namespace, name }), - }; -} - -function resolveKeySchema< - TKeySchema extends TableConfig["keySchema"], - TUserTypes extends UserTypesConfig["userTypes"], - TEnums extends StringForUnion[], ->( - keySchema: TKeySchema, - userTypes: TUserTypes, - enums: TEnums, -): ResolvedKeySchema { - 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["userTypes"], - TEnums extends StringForUnion[], ->( - valueSchema: TValueSchema, - userTypes: TUserTypes, - enums: TEnums, -): ResolvedValueSchema { - const schema = ( - typeof valueSchema === "string" ? ({ value: valueSchema } as unknown as TValueSchema) : valueSchema - ) as Exclude; - return resolveSchema(schema, userTypes, enums); -} - -function resolveSchema< - TSchema extends Exclude | TableConfig["valueSchema"], string>, - TUserTypes extends UserTypesConfig["userTypes"], - TEnums extends StringForUnion[], ->(schema: TSchema, userTypes: TUserTypes, enums: TEnums): ResolvedSchema { - return mapObject>(schema, (value, key) => { - const isUserType = userTypes && value in userTypes; - const isEnum = enums.includes(value); - return { - type: (isUserType ? userTypes[value].internalType : isEnum ? ("uint8" as const) : value) as ResolvedSchema< - TSchema, - TUserTypes, - TEnums[number] - >[typeof key]["type"], - internalType: value, - }; - }); -} diff --git a/packages/store/ts/config/index.ts b/packages/store/ts/config/index.ts index a00a9a9cd7..5de71694e7 100644 --- a/packages/store/ts/config/index.ts +++ b/packages/store/ts/config/index.ts @@ -1,4 +1,2 @@ export * from "./defaults"; export * from "./storeConfig"; - -export * from "./experimental/resolveConfig";