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, world): add table deploy config, add v2 to v1 compat config #2482

Merged
merged 9 commits into from
Mar 20, 2024
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
11 changes: 7 additions & 4 deletions packages/store/ts/config/v2/compat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { Store } from "./output";

describe("configToV1", () => {
it("should transform the broad v2 output to the broad v1 output", () => {
attest<StoreConfigV1, storeToV1<Store>>();
attest<storeToV1<Store>, StoreConfigV1>();
attest<StoreConfigV1, Omit<storeToV1<Store>, "v2">>();
attest<Omit<storeToV1<Store>, "v2">, StoreConfigV1>();
});

it("should transform a v2 store config output to the v1 config output", () => {
Expand Down Expand Up @@ -89,7 +89,10 @@ describe("configToV1", () => {
},
});

attest<typeof configV1>(storeToV1(configV2)).equals(configV1);
attest<storeToV1<typeof configV2>>(configV1);
const { v2, ...v1FromV2 } = storeToV1(configV2);

attest<typeof configV1>(v1FromV2).equals(configV1);
attest<typeof v1FromV2>(configV1).equals(v1FromV2);
attest<typeof configV2>(v2).equals(configV2);
});
});
14 changes: 8 additions & 6 deletions packages/store/ts/config/v2/compat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,19 @@ export type storeToV1<store> = store extends Store
};
storeImportPath: store["codegen"]["storeImportPath"];
userTypesFilename: store["codegen"]["userTypesFilename"];
codegenDirectory: store["codegen"]["codegenDirectory"];
codegenIndexFilename: store["codegen"]["codegenIndexFilename"];
codegenDirectory: store["codegen"]["outputDirectory"];
codegenIndexFilename: store["codegen"]["indexFilename"];
tables: {
[key in keyof store["tables"] as store["tables"][key]["name"]]: tableToV1<store["tables"][key]>;
};
v2: store;
}
: never;

type schemaToV1<schema extends Schema> = { [key in keyof schema]: schema[key]["internalType"] };

export type tableToV1<table extends Table> = {
directory: table["codegen"]["directory"];
directory: table["codegen"]["outputDirectory"];
dataStruct: table["codegen"]["dataStruct"];
tableIdArgument: table["codegen"]["tableIdArgument"];
storeArgument: table["codegen"]["storeArgument"];
Expand All @@ -47,7 +48,7 @@ export function storeToV1<store>(store: conform<store, Store>): storeToV1<store>
return [
table.name,
{
directory: table.codegen.directory,
directory: table.codegen.outputDirectory,
dataStruct: table.codegen.dataStruct,
tableIdArgument: table.codegen.tableIdArgument,
storeArgument: table.codegen.storeArgument,
Expand All @@ -66,8 +67,9 @@ export function storeToV1<store>(store: conform<store, Store>): storeToV1<store>
userTypes: resolvedUserTypes,
storeImportPath: store.codegen.storeImportPath,
userTypesFilename: store.codegen.userTypesFilename,
codegenDirectory: store.codegen.codegenDirectory,
codegenIndexFilename: store.codegen.codegenIndexFilename,
codegenDirectory: store.codegen.outputDirectory,
codegenIndexFilename: store.codegen.indexFilename,
tables: resolvedTables,
v2: store,
} as unknown as storeToV1<store>;
}
10 changes: 7 additions & 3 deletions packages/store/ts/config/v2/defaults.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
export const CODEGEN_DEFAULTS = {
storeImportPath: "@latticexyz/store/src/",
userTypesFilename: "common.sol",
codegenDirectory: "codegen",
codegenIndexFilename: "index.sol",
outputDirectory: "codegen",
indexFilename: "index.sol",
} as const;

export const TABLE_CODEGEN_DEFAULTS = {
directory: "tables",
outputDirectory: "tables",
tableIdArgument: false,
storeArgument: false,
} as const;

export const TABLE_DEPLOY_DEFAULTS = {
disabled: false,
} as const;

export const TABLE_DEFAULTS = {
namespace: "",
type: "table",
Expand Down
3 changes: 2 additions & 1 deletion packages/store/ts/config/v2/input.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Hex } from "viem";
import { Codegen, Enums, TableCodegen, UserTypes } from "./output";
import { Codegen, Enums, TableCodegen, TableDeploy, UserTypes } from "./output";
import { Scope } from "./scope";

export type SchemaInput = {
Expand All @@ -18,6 +18,7 @@ export type TableInput = {
readonly namespace?: string;
readonly type?: "table" | "offchainTable";
readonly codegen?: Partial<TableCodegen>;
readonly deploy?: Partial<TableDeploy>;
};

export type TablesInput = {
Expand Down
11 changes: 8 additions & 3 deletions packages/store/ts/config/v2/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,26 @@ export type Enums = {
};

export type TableCodegen = {
readonly directory: string;
readonly outputDirectory: string;
readonly tableIdArgument: boolean;
readonly storeArgument: boolean;
readonly dataStruct: boolean;
};

export type TableDeploy = {
readonly disabled: boolean;
};

export type Table = BaseTable & {
readonly codegen: TableCodegen;
readonly deploy: TableDeploy;
};

export type Codegen = {
readonly storeImportPath: string;
readonly userTypesFilename: string;
readonly codegenDirectory: string;
readonly codegenIndexFilename: string;
readonly outputDirectory: string;
readonly indexFilename: string;
};

export type Store = {
Expand Down
10 changes: 9 additions & 1 deletion packages/store/ts/config/v2/store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { describe, it } from "vitest";
import { defineStore } from "./store";
import { attest } from "@arktype/attest";
import { resourceToHex } from "@latticexyz/common";
import { CODEGEN_DEFAULTS, TABLE_CODEGEN_DEFAULTS } from "./defaults";
import { CODEGEN_DEFAULTS, TABLE_CODEGEN_DEFAULTS, TABLE_DEPLOY_DEFAULTS } from "./defaults";
import { Store } from "./output";

describe("defineStore", () => {
Expand Down Expand Up @@ -39,6 +39,7 @@ describe("defineStore", () => {
namespace: "",
codegen: { ...TABLE_CODEGEN_DEFAULTS, dataStruct: true as boolean },
type: "table",
deploy: TABLE_DEPLOY_DEFAULTS,
},
},
userTypes: {},
Expand Down Expand Up @@ -87,6 +88,7 @@ describe("defineStore", () => {
namespace: "",
codegen: { ...TABLE_CODEGEN_DEFAULTS, dataStruct: true as boolean },
type: "table",
deploy: TABLE_DEPLOY_DEFAULTS,
},
},
userTypes: {
Expand Down Expand Up @@ -134,6 +136,7 @@ describe("defineStore", () => {
namespace: "",
codegen: { ...TABLE_CODEGEN_DEFAULTS, dataStruct: false as boolean },
type: "table",
deploy: TABLE_DEPLOY_DEFAULTS,
},
},
userTypes: {},
Expand Down Expand Up @@ -182,6 +185,7 @@ describe("defineStore", () => {
namespace: "",
codegen: { ...TABLE_CODEGEN_DEFAULTS, dataStruct: false as boolean },
type: "table",
deploy: TABLE_DEPLOY_DEFAULTS,
},
Second: {
tableId: resourceToHex({ type: "table", namespace: "", name: "Second" }),
Expand All @@ -204,6 +208,7 @@ describe("defineStore", () => {
namespace: "",
codegen: { ...TABLE_CODEGEN_DEFAULTS, dataStruct: false as boolean },
type: "table",
deploy: TABLE_DEPLOY_DEFAULTS,
},
},
userTypes: {},
Expand Down Expand Up @@ -256,6 +261,7 @@ describe("defineStore", () => {
namespace: "",
codegen: { ...TABLE_CODEGEN_DEFAULTS, dataStruct: false as boolean },
type: "table",
deploy: TABLE_DEPLOY_DEFAULTS,
},
Second: {
tableId: resourceToHex({ type: "table", namespace: "", name: "Second" }),
Expand All @@ -278,6 +284,7 @@ describe("defineStore", () => {
namespace: "",
codegen: { ...TABLE_CODEGEN_DEFAULTS, dataStruct: false as boolean },
type: "table",
deploy: TABLE_DEPLOY_DEFAULTS,
},
},
userTypes: {
Expand Down Expand Up @@ -386,6 +393,7 @@ describe("defineStore", () => {
namespace: "",
codegen: { ...TABLE_CODEGEN_DEFAULTS, dataStruct: true as boolean },
type: "table",
deploy: TABLE_DEPLOY_DEFAULTS,
},
},
userTypes: {
Expand Down
6 changes: 5 additions & 1 deletion packages/store/ts/config/v2/storeWithShorthands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { describe, it } from "vitest";
import { defineStoreWithShorthands } from "./storeWithShorthands";
import { attest } from "@arktype/attest";
import { resourceToHex } from "@latticexyz/common";
import { CODEGEN_DEFAULTS, TABLE_CODEGEN_DEFAULTS } from "./defaults";
import { CODEGEN_DEFAULTS, TABLE_CODEGEN_DEFAULTS, TABLE_DEPLOY_DEFAULTS } from "./defaults";
import { defineStore } from "./store";

describe("defineStoreWithShorthands", () => {
Expand All @@ -27,6 +27,7 @@ describe("defineStoreWithShorthands", () => {
namespace: "",
codegen: { ...TABLE_CODEGEN_DEFAULTS, dataStruct: false as boolean },
type: "table",
deploy: TABLE_DEPLOY_DEFAULTS,
},
},
userTypes: {},
Expand Down Expand Up @@ -62,6 +63,7 @@ describe("defineStoreWithShorthands", () => {
namespace: "",
codegen: { ...TABLE_CODEGEN_DEFAULTS, dataStruct: false as boolean },
type: "table",
deploy: TABLE_DEPLOY_DEFAULTS,
},
},
userTypes: { CustomType: { type: "address", filePath: "path/to/file" } },
Expand Down Expand Up @@ -101,6 +103,7 @@ describe("defineStoreWithShorthands", () => {
namespace: "",
codegen: { ...TABLE_CODEGEN_DEFAULTS, dataStruct: true as boolean },
type: "table",
deploy: TABLE_DEPLOY_DEFAULTS,
},
},
userTypes: {},
Expand Down Expand Up @@ -140,6 +143,7 @@ describe("defineStoreWithShorthands", () => {
namespace: "",
codegen: { ...TABLE_CODEGEN_DEFAULTS, dataStruct: true as boolean },
type: "table",
deploy: TABLE_DEPLOY_DEFAULTS,
},
},
userTypes: {},
Expand Down
18 changes: 17 additions & 1 deletion packages/store/ts/config/v2/table.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { attest } from "@arktype/attest";
import { describe, it } from "vitest";
import { getStaticAbiTypeKeys, AbiTypeScope, extendScope } from "./scope";
import { validateKeys, defineTable } from "./table";
import { TABLE_CODEGEN_DEFAULTS } from "./defaults";
import { TABLE_CODEGEN_DEFAULTS, TABLE_DEPLOY_DEFAULTS } from "./defaults";
import { resourceToHex } from "@latticexyz/common";
import { getKeySchema, getValueSchema } from "@latticexyz/protocol-parser/internal";

Expand Down Expand Up @@ -65,6 +65,7 @@ describe("resolveTable", () => {
namespace: "",
codegen: { ...TABLE_CODEGEN_DEFAULTS, dataStruct: true as boolean },
type: "table",
deploy: TABLE_DEPLOY_DEFAULTS,
} as const;

attest<typeof expected>(table).equals(expected);
Expand All @@ -88,6 +89,7 @@ describe("resolveTable", () => {
namespace: "",
codegen: { ...TABLE_CODEGEN_DEFAULTS, dataStruct: false as boolean },
type: "table",
deploy: TABLE_DEPLOY_DEFAULTS,
} as const;

attest<typeof expected>(table).equals(expected);
Expand Down Expand Up @@ -117,11 +119,25 @@ describe("resolveTable", () => {
namespace: "",
codegen: { ...TABLE_CODEGEN_DEFAULTS, dataStruct: true as boolean },
type: "table",
deploy: TABLE_DEPLOY_DEFAULTS,
} as const;

attest<typeof expected>(table).equals(expected);
});

it("should pass through deploy config", () => {
const table = defineTable({
schema: { id: "address" },
key: ["id"],
name: "",
deploy: { disabled: true },
});

const expected = { disabled: true } as const;

attest<typeof expected>(table.deploy).equals(expected);
});

it("should throw if the provided key is a dynamic ABI type", () => {
attest(() =>
defineTable({
Expand Down
11 changes: 8 additions & 3 deletions packages/store/ts/config/v2/table.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { ErrorMessage, conform, narrow, requiredKeyOf } from "@arktype/util";
import { isStaticAbiType } from "@latticexyz/schema-type/internal";
import { Hex } from "viem";
import { get, hasOwnKey } from "./generics";
import { get, hasOwnKey, mergeIfUndefined } from "./generics";
import { resolveSchema, validateSchema } from "./schema";
import { AbiTypeScope, Scope, getStaticAbiTypeKeys } from "./scope";
import { TableCodegen } from "./output";
import { TABLE_CODEGEN_DEFAULTS, TABLE_DEFAULTS } from "./defaults";
import { TABLE_CODEGEN_DEFAULTS, TABLE_DEFAULTS, TABLE_DEPLOY_DEFAULTS } from "./defaults";
import { resourceToHex } from "@latticexyz/common";
import { SchemaInput, TableInput } from "./input";

Expand Down Expand Up @@ -121,7 +121,7 @@ export type resolveTableCodegen<input extends TableInput> = {
export function resolveTableCodegen<input extends TableInput>(input: input): resolveTableCodegen<input> {
const options = input.codegen;
return {
directory: get(options, "directory") ?? TABLE_CODEGEN_DEFAULTS.directory,
outputDirectory: get(options, "outputDirectory") ?? TABLE_CODEGEN_DEFAULTS.outputDirectory,
tableIdArgument: get(options, "tableIdArgument") ?? TABLE_CODEGEN_DEFAULTS.tableIdArgument,
storeArgument: get(options, "storeArgument") ?? TABLE_CODEGEN_DEFAULTS.storeArgument,
// dataStruct is true if there are at least 2 value fields
Expand All @@ -138,6 +138,10 @@ export type resolveTable<input, scope extends Scope = Scope> = input extends Tab
readonly key: Readonly<input["key"]>;
readonly schema: resolveSchema<input["schema"], scope>;
readonly codegen: resolveTableCodegen<input>;
readonly deploy: mergeIfUndefined<
undefined extends input["deploy"] ? {} : input["deploy"],
typeof TABLE_DEPLOY_DEFAULTS
>;
}
: never;

Expand All @@ -158,6 +162,7 @@ export function resolveTable<input extends TableInput, scope extends Scope = Abi
key: input.key,
schema: resolveSchema(input.schema, scope),
codegen: resolveTableCodegen(input),
deploy: mergeIfUndefined(input.deploy ?? {}, TABLE_DEPLOY_DEFAULTS),
} as unknown as resolveTable<input, scope>;
}

Expand Down
11 changes: 7 additions & 4 deletions packages/world/ts/config/v2/compat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import { defineWorld } from "./world";
describe("configToV1", () => {
it("should transform the broad v2 output to the broad v1 output", () => {
// Making the `worldContractName` prop required here since it is required on the output of `mudConfig`
attest<WorldConfigV1 & StoreConfigV1 & { worldContractName: string | undefined }, worldToV1<World>>();
attest<worldToV1<World>, WorldConfigV1 & StoreConfigV1 & { worldContractName: string | undefined }>();
attest<WorldConfigV1 & StoreConfigV1 & { worldContractName: string | undefined }, Omit<worldToV1<World>, "v2">>();
attest<Omit<worldToV1<World>, "v2">, WorldConfigV1 & StoreConfigV1 & { worldContractName: string | undefined }>();
});

it("should transform a v2 store config output to the v1 config output", () => {
Expand Down Expand Up @@ -90,7 +90,10 @@ describe("configToV1", () => {
},
});

attest<typeof configV1>(worldToV1(configV2)).equals(configV1);
attest<worldToV1<typeof configV2>>(configV1);
const { v2, ...v1FromV2 } = worldToV1(configV2);

attest<typeof configV1>(v1FromV2).equals(configV1);
attest<typeof v1FromV2>(configV1).equals(v1FromV2);
attest<typeof v2>(configV2).equals(v2);
});
});
Loading
Loading