From f2876e17ca65959b103afc4353c392ab68addce7 Mon Sep 17 00:00:00 2001 From: alvrs Date: Sun, 24 Sep 2023 14:31:49 +0100 Subject: [PATCH 1/5] add internalType property to user types for type inference --- .../src/codegen/utils/loadUserTypesFile.ts | 24 ++++++++++++++++--- packages/store/ts/config/storeConfig.ts | 16 +++++++++---- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/packages/common/src/codegen/utils/loadUserTypesFile.ts b/packages/common/src/codegen/utils/loadUserTypesFile.ts index 8573e8c165..8977cac44a 100644 --- a/packages/common/src/codegen/utils/loadUserTypesFile.ts +++ b/packages/common/src/codegen/utils/loadUserTypesFile.ts @@ -1,14 +1,21 @@ import { readFileSync } from "fs"; import path from "path"; import { SolidityUserDefinedType, extractUserTypes } from "./extractUserTypes"; +import { SchemaAbiType } from "@latticexyz/schema-type"; +import { MUDError } from "../../errors"; + +export type UserType = { + filePath: string; + internalType: SchemaAbiType; +}; export function loadAndExtractUserTypes( - userTypes: Record, + userTypes: Record, outputBaseDirectory: string, remappings: [string, string][] ): Record { const userTypesPerFile: Record = {}; - for (const [userTypeName, unresolvedFilePath] of Object.entries(userTypes)) { + for (const [userTypeName, { filePath: unresolvedFilePath }] of Object.entries(userTypes)) { if (!(unresolvedFilePath in userTypesPerFile)) { userTypesPerFile[unresolvedFilePath] = []; } @@ -17,7 +24,18 @@ export function loadAndExtractUserTypes( let extractedUserTypes: Record = {}; for (const [unresolvedFilePath, userTypeNames] of Object.entries(userTypesPerFile)) { const { filePath, data } = loadUserTypesFile(outputBaseDirectory, unresolvedFilePath, remappings); - extractedUserTypes = Object.assign(userTypes, extractUserTypes(data, userTypeNames, filePath)); + const userTypesInFile = extractUserTypes(data, userTypeNames, filePath); + + // Verify the actual user type matches the internalType specified in the config + for (const [userTypeName, userType] of Object.entries(userTypesInFile)) { + if (userType.internalTypeId !== userTypes[userTypeName].internalType) { + throw new MUDError( + `User type "${userTypeName}" has internal type "${userType.internalTypeId}" but config specifies "${userTypes[userTypeName].internalType}"` + ); + } + } + + extractedUserTypes = Object.assign(extractedUserTypes, userTypesInFile); } return extractedUserTypes; } diff --git a/packages/store/ts/config/storeConfig.ts b/packages/store/ts/config/storeConfig.ts index b7df302df9..cd8959311f 100644 --- a/packages/store/ts/config/storeConfig.ts +++ b/packages/store/ts/config/storeConfig.ts @@ -22,6 +22,7 @@ import { zName, } from "@latticexyz/config"; import { DEFAULTS, PATH_DEFAULTS, TABLE_DEFAULTS } from "./defaults"; +import { UserType } from "@latticexyz/common/codegen"; const zTableName = zObjectName; const zKeyName = zValueName; @@ -241,7 +242,7 @@ export const zEnumsConfig = z.object({ * ************************************************************************/ -export type UserTypesConfig = never extends UserTypeNames +export type UserTypesConfig = never extends UserTypeNames ? { /** * User types mapped to file paths from which to import them. @@ -250,7 +251,7 @@ export type UserTypesConfig = never extend * * (user types are inferred to be absent) */ - userTypes?: Record; + userTypes?: Record; } : StringForUnion extends UserTypeNames ? { @@ -261,7 +262,7 @@ export type UserTypesConfig = never extend * * (user types aren't inferred - use `mudConfig` or `storeConfig` helper, and `as const` for variables) */ - userTypes?: Record; + userTypes?: Record; } : { /** @@ -271,15 +272,20 @@ export type UserTypesConfig = never extend * * User types defined here can be used as types in table schemas/keys */ - userTypes: Record; + userTypes: Record; }; export type FullUserTypesConfig = { userTypes: Record; }; +const zUserTypeConfig = z.object({ + filePath: z.string(), + internalType: z.string(), +}); + export const zUserTypesConfig = z.object({ - userTypes: z.record(zUserTypeName, z.string()).default(DEFAULTS.userTypes), + userTypes: z.record(zUserTypeName, zUserTypeConfig).default(DEFAULTS.userTypes), }); /************************************************************************ From 2cd3c92c90ecb555052db562115e918d1d9bd37b Mon Sep 17 00:00:00 2001 From: alvrs Date: Sun, 24 Sep 2023 14:35:48 +0100 Subject: [PATCH 2/5] update test table config --- packages/cli/scripts/generate-test-tables.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/cli/scripts/generate-test-tables.ts b/packages/cli/scripts/generate-test-tables.ts index 1d14474bd2..450dda72fc 100644 --- a/packages/cli/scripts/generate-test-tables.ts +++ b/packages/cli/scripts/generate-test-tables.ts @@ -83,11 +83,11 @@ try { }, userTypes: { - TestTypeAddress: "./contracts/src/types.sol", - TestTypeInt64: "./contracts/src/types.sol", - TestTypeBool: "./contracts/src/types.sol", - TestTypeUint128: "./contracts/src/types.sol", - ResourceId: "@latticexyz/store/src/ResourceId.sol", + TestTypeAddress: { filePath: "./contracts/src/types.sol", internalType: "address" }, + TestTypeInt64: { filePath: "./contracts/src/types.sol", internalType: "int64" }, + TestTypeBool: { filePath: "./contracts/src/types.sol", internalType: "bool" }, + TestTypeUint128: { filePath: "./contracts/src/types.sol", internalType: "uint128" }, + ResourceId: { filePath: "@latticexyz/store/src/ResourceId.sol", internalType: "bytes32" }, }, }); } catch (error: unknown) { From 24efe6a998c80d6db47d925918a69a9abcc8b384 Mon Sep 17 00:00:00 2001 From: alvarius Date: Sun, 24 Sep 2023 14:39:56 +0100 Subject: [PATCH 3/5] Create thin-chairs-compare.md --- .changeset/thin-chairs-compare.md | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 .changeset/thin-chairs-compare.md diff --git a/.changeset/thin-chairs-compare.md b/.changeset/thin-chairs-compare.md new file mode 100644 index 0000000000..1270e414a6 --- /dev/null +++ b/.changeset/thin-chairs-compare.md @@ -0,0 +1,7 @@ +--- +"@latticexyz/cli": patch +"@latticexyz/common": patch +"@latticexyz/store": major +--- + +Changed the `userTypes` property to accept `{ filePath: string, internalType: SchemaAbiType }` to enable strong inference of types from the config. From 15a03103877c1c9ce2a56d1f02a9baebadf0a965 Mon Sep 17 00:00:00 2001 From: alvrs Date: Sun, 24 Sep 2023 14:42:32 +0100 Subject: [PATCH 4/5] reduce strictness of type until mudConfig refactor --- packages/common/src/codegen/utils/loadUserTypesFile.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/common/src/codegen/utils/loadUserTypesFile.ts b/packages/common/src/codegen/utils/loadUserTypesFile.ts index 8977cac44a..699575da6d 100644 --- a/packages/common/src/codegen/utils/loadUserTypesFile.ts +++ b/packages/common/src/codegen/utils/loadUserTypesFile.ts @@ -1,12 +1,11 @@ import { readFileSync } from "fs"; import path from "path"; import { SolidityUserDefinedType, extractUserTypes } from "./extractUserTypes"; -import { SchemaAbiType } from "@latticexyz/schema-type"; import { MUDError } from "../../errors"; export type UserType = { filePath: string; - internalType: SchemaAbiType; + internalType: string; }; export function loadAndExtractUserTypes( From b3501a5b157110d262414d2feaa37e2b12d1eff2 Mon Sep 17 00:00:00 2001 From: alvarius Date: Sun, 24 Sep 2023 15:12:06 +0100 Subject: [PATCH 5/5] Update thin-chairs-compare.md --- .changeset/thin-chairs-compare.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/thin-chairs-compare.md b/.changeset/thin-chairs-compare.md index 1270e414a6..6d76a5ab99 100644 --- a/.changeset/thin-chairs-compare.md +++ b/.changeset/thin-chairs-compare.md @@ -4,4 +4,4 @@ "@latticexyz/store": major --- -Changed the `userTypes` property to accept `{ filePath: string, internalType: SchemaAbiType }` to enable strong inference of types from the config. +Changed the `userTypes` property to accept `{ filePath: string, internalType: SchemaAbiType }` to enable strong type inference from the config.