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(store): add experimental config resolve helper #1826

Merged
merged 22 commits into from
Oct 27, 2023
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
add tests and fixes
  • Loading branch information
alvrs committed Oct 26, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 8a018283e9549956dad3f8339693fec1a9167a40
38 changes: 38 additions & 0 deletions packages/store/ts/config/experimental/resolveConfig.test.ts
Original file line number Diff line number Diff line change
@@ -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" },
});
});
});
36 changes: 19 additions & 17 deletions packages/store/ts/config/experimental/resolveConfig.ts
Original file line number Diff line number Diff line change
@@ -17,7 +17,7 @@ export type ResolvedStoreConfig<TStoreConfig extends StoreConfig> = TStoreConfig

export type ResolvedTableConfig<
TTableConfig extends TableConfig,
TUserTypes extends UserTypesConfig,
TUserTypes extends UserTypesConfig["userTypes"],
TEnumNames extends StringForUnion
> = Omit<TTableConfig, "keySchema" | "valueSchema"> & {
keySchema: ResolvedKeySchema<TTableConfig["keySchema"], TUserTypes, TEnumNames>;
@@ -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<TKeySchema, TUserTypes, TEnumNames>;

export type ResolvedValueSchema<
TValueSchema extends TableConfig["valueSchema"],
TUserTypes extends UserTypesConfig,
TUserTypes extends UserTypesConfig["userTypes"],
TEnumNames extends StringForUnion
> = ResolvedSchema<TValueSchema, TUserTypes, TEnumNames>;

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<TStoreConfig extends StoreConfig>(

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<TKeySchema extends undefined ? { key: "bytes32" } : TKeySchema, TUserTypes, TEnums[number]> {
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<TValueSchema, TUserTypes, TEnums[number]> {
// 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["keySchema"]> | TableConfig["valueSchema"],
TUserTypes extends UserTypesConfig,
TUserTypes extends UserTypesConfig["userTypes"],
TEnums extends StringForUnion[]
>(schema: TSchema, { userTypes }: TUserTypes, enums: TEnums): ResolvedSchema<TSchema, TUserTypes, TEnums[number]> {
>(schema: TSchema, userTypes: TUserTypes, enums: TEnums): ResolvedSchema<TSchema, TUserTypes, TEnums[number]> {
const resolvedSchema: Record<string, { internalType: string; type: string }> = {};
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,
};
}