-
Notifications
You must be signed in to change notification settings - Fork 202
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
feat(cli): use abi types in store config #507
Changes from 5 commits
4412aab
d0f2c71
f319d00
4c5f70d
282163c
aef0586
d7fb8a3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
import { describe, expectTypeOf } from "vitest"; | ||
import { z } from "zod"; | ||
import { StoreConfig, StoreUserConfig, UserTypesConfig } from "./parseStoreConfig.js"; | ||
import { StoreConfig, StoreUserConfig } from "./parseStoreConfig.js"; | ||
|
||
describe("StoreUserConfig", () => { | ||
// Typecheck manual interfaces against zod | ||
expectTypeOf<StoreUserConfig>().toEqualTypeOf<z.input<typeof StoreConfig>>(); | ||
// type equality isn't deep for optionals | ||
expectTypeOf<StoreUserConfig["tables"][string]>().toEqualTypeOf<z.input<typeof StoreConfig>["tables"][string]>(); | ||
expectTypeOf<NonNullable<UserTypesConfig["enums"]>[string]>().toEqualTypeOf< | ||
NonNullable<NonNullable<UserTypesConfig>["enums"]>[string] | ||
expectTypeOf<NonNullable<StoreUserConfig["enums"]>[string]>().toEqualTypeOf< | ||
NonNullable<NonNullable<z.input<typeof StoreConfig>>["enums"]>[string] | ||
>(); | ||
// TODO If more nested schemas are added, provide separate tests for them | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,57 @@ | ||
import { SchemaType } from "@latticexyz/schema-type"; | ||
import { AbiType, AbiTypes, StaticAbiType } from "@latticexyz/schema-type"; | ||
import { RefinementCtx, z, ZodIssueCode } from "zod"; | ||
import { ObjectName, Selector, StaticSchemaType, UserEnum, ValueName } from "./commonSchemas.js"; | ||
import { RequireKeys, StringForUnion } from "../utils/typeUtils.js"; | ||
import { ObjectName, Selector, zAbiType, zStaticAbiType, UserEnum, ValueName } from "./commonSchemas.js"; | ||
import { getDuplicates } from "./validation.js"; | ||
|
||
const TableName = ObjectName; | ||
const KeyName = ValueName; | ||
const ColumnName = ValueName; | ||
const UserEnumName = ObjectName; | ||
|
||
// Fields can use SchemaType or one of user defined wrapper types | ||
const FieldData = z.union([z.nativeEnum(SchemaType), UserEnumName]); | ||
// Fields can use AbiType or one of user-defined wrapper types | ||
// (user types are refined later, based on the appropriate config options) | ||
const zFieldData = z.union([zAbiType, z.string()]); | ||
|
||
// Primary keys allow only static types, but allow static user defined types | ||
const PrimaryKey = z.union([StaticSchemaType, UserEnumName]); | ||
const PrimaryKeys = z.record(KeyName, PrimaryKey).default({ key: SchemaType.BYTES32 }); | ||
type FieldData<UserTypes extends StringForUnion = StringForUnion> = AbiType | UserTypes; | ||
|
||
// Primary keys allow only static types | ||
const zPrimaryKey = z.union([zStaticAbiType, z.string()]); | ||
const zPrimaryKeys = z.record(KeyName, zPrimaryKey).default({ key: "bytes32" }); | ||
|
||
type PrimaryKey<StaticUserTypes extends StringForUnion = StringForUnion> = StaticAbiType | StaticUserTypes; | ||
|
||
/************************************************************************ | ||
* | ||
* TABLE SCHEMA | ||
* | ||
************************************************************************/ | ||
|
||
export type FullSchemaConfig = Record<string, z.input<typeof FieldData>>; | ||
export type ShorthandSchemaConfig = z.input<typeof FieldData>; | ||
export type SchemaConfig = FullSchemaConfig | ShorthandSchemaConfig; | ||
export type FullSchemaConfig<UserTypes extends StringForUnion = StringForUnion> = Record<string, FieldData<UserTypes>>; | ||
export type ShorthandSchemaConfig<UserTypes extends StringForUnion = StringForUnion> = FieldData<UserTypes>; | ||
export type SchemaConfig<UserTypes extends StringForUnion = StringForUnion> = | ||
| FullSchemaConfig<UserTypes> | ||
| ShorthandSchemaConfig<UserTypes>; | ||
|
||
const FullSchemaConfig = z | ||
.record(ColumnName, FieldData) | ||
const zFullSchemaConfig = z | ||
.record(ColumnName, zFieldData) | ||
.refine((arg) => Object.keys(arg).length > 0, "Table schema may not be empty"); | ||
|
||
const ShorthandSchemaConfig = FieldData.transform((fieldData) => { | ||
return FullSchemaConfig.parse({ | ||
const zShorthandSchemaConfig = zFieldData.transform((fieldData) => { | ||
return zFullSchemaConfig.parse({ | ||
value: fieldData, | ||
}); | ||
}); | ||
|
||
export const SchemaConfig = FullSchemaConfig.or(ShorthandSchemaConfig); | ||
export const zSchemaConfig = zFullSchemaConfig.or(zShorthandSchemaConfig); | ||
|
||
/************************************************************************ | ||
* | ||
* TABLE | ||
* | ||
************************************************************************/ | ||
|
||
export interface TableConfig { | ||
export interface TableConfig<UserTypes extends StringForUnion = StringForUnion> { | ||
/** Output directory path for the file. Default is "tables" */ | ||
directory?: string; | ||
/** | ||
|
@@ -58,20 +66,20 @@ export interface TableConfig { | |
storeArgument?: boolean; | ||
/** Include a data struct and methods for it. Default is false for 1-column tables; true for multi-column tables. */ | ||
dataStruct?: boolean; | ||
/** Table's primary key names mapped to their types. Default is `{ key: SchemaType.BYTES32 }` */ | ||
primaryKeys?: Record<string, z.input<typeof PrimaryKey>>; | ||
/** Table's primary key names mapped to their types. Default is `{ key: "bytes32" }` */ | ||
primaryKeys?: Record<string, PrimaryKey<UserTypes>>; | ||
/** Table's column names mapped to their types. Table name's 1st letter should be lowercase. */ | ||
schema: SchemaConfig; | ||
schema: SchemaConfig<UserTypes>; | ||
} | ||
|
||
const FullTableConfig = z | ||
const zFullTableConfig = z | ||
.object({ | ||
directory: z.string().default("tables"), | ||
fileSelector: Selector.optional(), | ||
tableIdArgument: z.boolean().default(false), | ||
storeArgument: z.boolean().default(false), | ||
primaryKeys: PrimaryKeys, | ||
schema: SchemaConfig, | ||
primaryKeys: zPrimaryKeys, | ||
schema: zSchemaConfig, | ||
dataStruct: z.boolean().optional(), | ||
}) | ||
.transform((arg) => { | ||
|
@@ -84,25 +92,28 @@ const FullTableConfig = z | |
return arg as RequireKeys<typeof arg, "dataStruct">; | ||
}); | ||
|
||
const ShorthandTableConfig = FieldData.transform((fieldData) => { | ||
return FullTableConfig.parse({ | ||
const zShorthandTableConfig = zFieldData.transform((fieldData) => { | ||
return zFullTableConfig.parse({ | ||
schema: { | ||
value: fieldData, | ||
}, | ||
}); | ||
}); | ||
|
||
export const TableConfig = FullTableConfig.or(ShorthandTableConfig); | ||
export const zTableConfig = zFullTableConfig.or(zShorthandTableConfig); | ||
|
||
/************************************************************************ | ||
* | ||
* TABLES | ||
* | ||
************************************************************************/ | ||
|
||
export type TablesConfig = Record<string, TableConfig | z.input<typeof FieldData>>; | ||
export type TablesConfig<UserTypes extends StringForUnion = StringForUnion> = Record< | ||
string, | ||
TableConfig<UserTypes> | FieldData<UserTypes> | ||
>; | ||
|
||
export const TablesConfig = z.record(TableName, TableConfig).transform((tables) => { | ||
export const zTablesConfig = z.record(TableName, zTableConfig).transform((tables) => { | ||
// default fileSelector depends on tableName | ||
for (const tableName of Object.keys(tables)) { | ||
const table = tables[tableName]; | ||
|
@@ -119,19 +130,25 @@ export const TablesConfig = z.record(TableName, TableConfig).transform((tables) | |
* | ||
************************************************************************/ | ||
|
||
export interface UserTypesConfig<Enums extends Record<string, string[]> = Record<string, string[]>> { | ||
/** Path to the file where common types will be generated and imported from. Default is "Types" */ | ||
path?: string; | ||
/** Enum names mapped to lists of their member names */ | ||
enums?: Enums; | ||
} | ||
|
||
export const UserTypesConfig = z | ||
.object({ | ||
path: z.string().default("Types"), | ||
enums: z.record(UserEnumName, UserEnum).default({}), | ||
}) | ||
.default({}); | ||
export type EnumsConfig<EnumNames extends StringForUnion = StringForUnion> = string extends EnumNames | ||
? { | ||
/** | ||
* Enum names mapped to lists of their member names | ||
*/ | ||
enums?: Record<EnumNames, string[]>; | ||
} | ||
: { | ||
/** | ||
* Enum names mapped to lists of their member names | ||
* | ||
* Required if used in tables | ||
*/ | ||
enums: Record<EnumNames, string[]>; | ||
}; | ||
|
||
export const zEnumsConfig = z.object({ | ||
enums: z.record(UserEnumName, UserEnum).default({}), | ||
}); | ||
|
||
/************************************************************************ | ||
* | ||
|
@@ -140,7 +157,7 @@ export const UserTypesConfig = z | |
************************************************************************/ | ||
|
||
// zod doesn't preserve doc comments | ||
export interface StoreUserConfig { | ||
export type StoreUserConfig<EnumNames extends StringForUnion = StringForUnion> = EnumsConfig<EnumNames> & { | ||
/** The namespace for table ids. Default is "" (empty string) */ | ||
namespace?: string; | ||
/** Path for store package imports. Default is "@latticexyz/store/src/" */ | ||
|
@@ -151,22 +168,31 @@ export interface StoreUserConfig { | |
* The key is the table name (capitalized). | ||
* | ||
* The value: | ||
* - `SchemaType | userType` for a single-value table (aka ECS component). | ||
* - abi or user type for a single-value table (aka ECS component). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not part of this PR, but I think we should remove |
||
* - FullTableConfig object for multi-value tables (or for customizable options). | ||
*/ | ||
tables: TablesConfig; | ||
/** User-defined types that will be generated and may be used in table schemas instead of `SchemaType` */ | ||
userTypes?: UserTypesConfig; | ||
tables: TablesConfig<EnumNames>; | ||
/** Path to the file where common user types will be generated and imported from. Default is "Types" */ | ||
userTypesPath?: string; | ||
}; | ||
|
||
/** Type helper for defining StoreUserConfig */ | ||
export function defineStoreUserConfig<EnumNames extends StringForUnion = StringForUnion>( | ||
config: StoreUserConfig<EnumNames> | ||
) { | ||
return config; | ||
} | ||
|
||
export type StoreConfig = z.output<typeof StoreConfig>; | ||
|
||
const StoreConfigUnrefined = z.object({ | ||
namespace: Selector.default(""), | ||
storeImportPath: z.string().default("@latticexyz/store/src/"), | ||
tables: TablesConfig, | ||
userTypes: UserTypesConfig, | ||
}); | ||
const StoreConfigUnrefined = z | ||
.object({ | ||
namespace: Selector.default(""), | ||
storeImportPath: z.string().default("@latticexyz/store/src/"), | ||
tables: zTablesConfig, | ||
userTypesPath: z.string().default("Types"), | ||
}) | ||
.merge(zEnumsConfig); | ||
|
||
// finally validate global conditions | ||
export const StoreConfig = StoreConfigUnrefined.superRefine(validateStoreConfig); | ||
|
@@ -197,7 +223,7 @@ function validateStoreConfig(config: z.output<typeof StoreConfigUnrefined>, ctx: | |
} | ||
// Global names must be unique | ||
const tableNames = Object.keys(config.tables); | ||
const userTypeNames = Object.keys(config.userTypes.enums); | ||
const userTypeNames = Object.keys(config.enums); | ||
const globalNames = [...tableNames, ...userTypeNames]; | ||
const duplicateGlobalNames = getDuplicates(globalNames); | ||
if (duplicateGlobalNames.length > 0) { | ||
|
@@ -209,25 +235,19 @@ function validateStoreConfig(config: z.output<typeof StoreConfigUnrefined>, ctx: | |
// User types must exist | ||
for (const table of Object.values(config.tables)) { | ||
for (const primaryKeyType of Object.values(table.primaryKeys)) { | ||
validateIfUserType(userTypeNames, primaryKeyType, ctx); | ||
validateAbiOrUserType(userTypeNames, primaryKeyType, ctx); | ||
} | ||
for (const fieldType of Object.values(table.schema)) { | ||
validateIfUserType(userTypeNames, fieldType, ctx); | ||
validateAbiOrUserType(userTypeNames, fieldType, ctx); | ||
} | ||
} | ||
} | ||
|
||
function validateIfUserType( | ||
userTypeNames: string[], | ||
type: z.output<typeof FieldData> | z.output<typeof PrimaryKey>, | ||
ctx: RefinementCtx | ||
) { | ||
if (typeof type === "string" && !userTypeNames.includes(type)) { | ||
function validateAbiOrUserType(userTypeNames: string[], type: string, ctx: RefinementCtx) { | ||
if (!(AbiTypes as string[]).includes(type) && !userTypeNames.includes(type)) { | ||
ctx.addIssue({ | ||
code: ZodIssueCode.custom, | ||
message: `User type ${type} is not defined in userTypes`, | ||
message: `${type} is not a valid abi type, and is not defined in userTypes`, | ||
}); | ||
} | ||
} | ||
|
||
type RequireKeys<T extends Record<string, unknown>, P extends string> = T & Required<Pick<T, P>>; |
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.
nit: would be great if we could just call this
mudConfig({...})
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.
For consistency we should probably capitalize
MUD
here.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.
should
storeConfig
beStoreConfig
then to consistently capitalize them too?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.
disagree! would much rather we loosen how/when we capitalize MUD than change context-meaningful capitalization of code (i.e. types/components vs variables/functions), otherwise it's gonna be really confusing
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.
doing a little refactoring PR rn, capitalized func names don't feel good. And with MUDConfig I also get conflicts with types and zod stuff
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.
defineMUDConfig
would be a better alternative, but I'd still prefermudConfig
as it's shorter