Skip to content
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

Merged
merged 7 commits into from
Mar 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@
"typechain": "^8.1.1",
"typescript": "^4.9.5",
"yargs": "^17.7.1",
"zod": "^3.20.6",
"zod-validation-error": "^0.3.2"
"zod": "^3.21.4",
"zod-validation-error": "^1.0.1"
},
"gitHead": "914a1e0ae4a573d685841ca2ea921435057deb8f"
}
35 changes: 16 additions & 19 deletions packages/cli/scripts/codegen.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,38 @@
import { SchemaType } from "@latticexyz/schema-type";
import { parseStoreConfig, StoreUserConfig } from "../src/config/index.js";
import { storeConfig, parseStoreConfig } from "../src/config/index.js";
import { tablegen } from "../src/render-solidity/tablegen.js";
import { logError } from "../src/utils/errors.js";
import { getSrcDirectory } from "../src/utils/foundry.js";

// This config is used only for tests
const config: StoreUserConfig = {
const config = storeConfig({
tables: {
Table1: {
primaryKeys: {
k1: SchemaType.UINT256,
k2: SchemaType.INT32,
k3: SchemaType.BYTES16,
k4: SchemaType.ADDRESS,
k5: SchemaType.BOOL,
k1: "uint256",
k2: "int32",
k3: "bytes16",
k4: "address",
k5: "bool",
k6: "Enum1",
k7: "Enum2",
},
schema: {
v1: SchemaType.UINT256,
v2: SchemaType.INT32,
v3: SchemaType.BYTES16,
v4: SchemaType.ADDRESS,
v5: SchemaType.BOOL,
v1: "uint256",
v2: "int32",
v3: "bytes16",
v4: "address",
v5: "bool",
v6: "Enum1",
v7: "Enum2",
},
},
},

userTypes: {
enums: {
Enum1: ["E1", "E2", "E3"],
Enum2: ["E1"],
},
enums: {
Enum1: ["E1", "E2", "E3"],
Enum2: ["E1"],
},
};
});

// Aside from avoiding `mud.config.mts` in cli package (could cause issues),
// this also tests that tablegen can work as a standalone function
Expand Down
7 changes: 1 addition & 6 deletions packages/cli/src/config/commonSchemas.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getStaticByteLength, SchemaType } from "@latticexyz/schema-type";
import { AbiType, AbiTypes, StaticAbiTypes } from "@latticexyz/schema-type";
import { z } from "zod";
import {
validateBaseRoute,
Expand Down Expand Up @@ -31,10 +31,5 @@ export const BaseRoute = z.string().superRefine(validateBaseRoute);
/** A valid Ethereum address */
export const EthereumAddress = z.string().superRefine(validateEthereumAddress);

/** Static subset of SchemaType enum */
export const StaticSchemaType = z
.nativeEnum(SchemaType)
.refine((arg) => getStaticByteLength(arg) > 0, "SchemaType must be static");

/** A selector for namespace/file/resource */
export const Selector = z.string().superRefine(validateSelector);
9 changes: 8 additions & 1 deletion packages/cli/src/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import { StringForUnion } from "../utils/typeUtils.js";
import { StoreUserConfig, StoreConfig } from "./parseStoreConfig.js";
import { WorldUserConfig, ResolvedWorldConfig } from "./world/index.js";

export type MUDUserConfig = StoreUserConfig & WorldUserConfig;
export type MUDUserConfig<EnumNames extends StringForUnion = StringForUnion> = StoreUserConfig<EnumNames> &
WorldUserConfig;
export type MUDConfig = StoreConfig & ResolvedWorldConfig;

/** Type helper for defining MUDUserConfig */
export function mudConfig<EnumNames extends StringForUnion = StringForUnion>(config: MUDUserConfig<EnumNames>) {
return config;
}

export * from "./commonSchemas.js";
export * from "./loadConfig.js";
export * from "./loadStoreConfig.js";
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/src/config/parseStoreConfig.test-d.ts
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
});
151 changes: 90 additions & 61 deletions packages/cli/src/config/parseStoreConfig.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,58 @@
import { SchemaType } from "@latticexyz/schema-type";
import { AbiType, AbiTypes, StaticAbiType, StaticAbiTypes } 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, 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.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
// (user types are refined later, based on the appropriate config options)
const zPrimaryKey = 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;
/**
Expand All @@ -58,20 +67,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) => {
Expand All @@ -84,25 +93,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];
Expand All @@ -119,19 +131,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 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 UserTypesConfig = z
.object({
path: z.string().default("Types"),
enums: z.record(UserEnumName, UserEnum).default({}),
})
.default({});
export const zEnumsConfig = z.object({
enums: z.record(UserEnumName, UserEnum).default({}),
});

/************************************************************************
*
Expand All @@ -140,7 +158,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/" */
Expand All @@ -151,22 +169,29 @@ 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).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not part of this PR, but I think we should remove aka ECS component. Having a single value is not the distinguishing factor for an ECS component, since components can have multiple (related) values (eg. PositionComponent = {x: int32, y: int32 }). The distinguishing factor for ECS components vs regular tables would rather be having a single key vs a composite key.

* - 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 storeConfig<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);
Expand Down Expand Up @@ -197,7 +222,8 @@ 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 staticUserTypeNames = Object.keys(config.enums);
const userTypeNames = staticUserTypeNames;
const globalNames = [...tableNames, ...userTypeNames];
const duplicateGlobalNames = getDuplicates(globalNames);
if (duplicateGlobalNames.length > 0) {
Expand All @@ -209,25 +235,28 @@ 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);
validateStaticAbiOrUserType(staticUserTypeNames, 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>>;
function validateStaticAbiOrUserType(staticUserTypeNames: string[], type: string, ctx: RefinementCtx) {
if (!(StaticAbiTypes as string[]).includes(type) && !staticUserTypeNames.includes(type)) {
ctx.addIssue({
code: ZodIssueCode.custom,
message: `${type} is not a static type`,
});
}
}
Loading