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 7 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);
});
});
2 changes: 2 additions & 0 deletions packages/store/ts/config/v2/compat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export type storeToV1<store> = store extends Store
tables: {
[key in keyof store["tables"] as store["tables"][key]["name"]]: tableToV1<store["tables"][key]>;
};
v2: store;
}
: never;

Expand Down Expand Up @@ -69,5 +70,6 @@ export function storeToV1<store>(store: conform<store, Store>): storeToV1<store>
codegenDirectory: store.codegen.codegenDirectory,
codegenIndexFilename: store.codegen.codegenIndexFilename,
tables: resolvedTables,
v2: store,
} as unknown as storeToV1<store>;
}
4 changes: 4 additions & 0 deletions packages/store/ts/config/v2/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ export const TABLE_CODEGEN_DEFAULTS = {
storeArgument: false,
} as const;

export const TABLE_DEPLOY_DEFAULTS = {
disable: 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
5 changes: 5 additions & 0 deletions packages/store/ts/config/v2/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@ export type TableCodegen = {
readonly dataStruct: boolean;
};

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

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

export type Codegen = {
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: { disable: true },
});

const expected = { disable: 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
9 changes: 7 additions & 2 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 @@ -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);
});
});
21 changes: 11 additions & 10 deletions packages/world/ts/config/v2/compat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,18 @@ function systemsToV1<systems extends Systems>(systems: systems): systemsToV1<sys
}

export type worldToV1<world> = world extends World
? storeToV1<world> & {
? Omit<storeToV1<world>, "v2"> & {
systems: systemsToV1<world["systems"]>;
excludeSystems: mutable<world["excludeSystems"]>;
modules: modulesToV1<world["modules"]>;
worldContractName: world["deployment"]["customWorldContract"];
postDeployScript: world["deployment"]["postDeployScript"];
deploysDirectory: world["deployment"]["deploysDirectory"];
worldsFile: world["deployment"]["worldsFile"];
worldContractName: world["deploy"]["customWorldContract"];
postDeployScript: world["deploy"]["postDeployScript"];
deploysDirectory: world["deploy"]["deploysDirectory"];
worldsFile: world["deploy"]["worldsFile"];
worldInterfaceName: world["codegen"]["worldInterfaceName"];
worldgenDirectory: world["codegen"]["worldgenDirectory"];
worldImportPath: world["codegen"]["worldImportPath"];
v2: world;
}
: never;

Expand All @@ -45,14 +46,14 @@ export function worldToV1<world>(world: conform<world, World>): worldToV1<world>
systems: systemsToV1(world.systems),
excludeSystems: world.excludeSystems,
modules: modulesToV1(world.modules),
worldContractName: world.deployment.customWorldContract,
postDeployScript: world.deployment.postDeployScript,
deploysDirectory: world.deployment.deploysDirectory,
worldsFile: world.deployment.worldsFile,
worldContractName: world.deploy.customWorldContract,
postDeployScript: world.deploy.postDeployScript,
deploysDirectory: world.deploy.deploysDirectory,
worldsFile: world.deploy.worldsFile,
worldInterfaceName: world.codegen.worldInterfaceName,
worldgenDirectory: world.codegen.worldgenDirectory,
worldImportPath: world.codegen.worldImportPath,
};

return { ...storeToV1(world as Store), ...v1WorldConfig } as worldToV1<world>;
return { ...storeToV1(world as Store), ...v1WorldConfig, v2: world } as worldToV1<world>;
}
4 changes: 2 additions & 2 deletions packages/world/ts/config/v2/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const CODEGEN_DEFAULTS = {
worldImportPath: "@latticexyz/world/src/",
} as const;

export const DEPLOYMENT_DEFAULTS = {
export const DEPLOY_DEFAULTS = {
customWorldContract: undefined,
postDeployScript: "PostDeploy",
deploysDirectory: "./deploys",
Expand All @@ -23,5 +23,5 @@ export const CONFIG_DEFAULTS = {
excludeSystems: [] as string[],
modules: [],
codegen: CODEGEN_DEFAULTS,
deployment: DEPLOYMENT_DEFAULTS,
deploy: DEPLOY_DEFAULTS,
} as const;
10 changes: 10 additions & 0 deletions packages/world/ts/config/v2/deploy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { mergeIfUndefined, isObject } from "@latticexyz/store/config/v2";
import { DEPLOY_DEFAULTS } from "./defaults";

export type resolveDeploy<deploy> = deploy extends {}
? mergeIfUndefined<deploy, typeof DEPLOY_DEFAULTS>
: typeof DEPLOY_DEFAULTS;

export function resolveDeploy<deploy>(deploy: deploy): resolveDeploy<deploy> {
return (isObject(deploy) ? mergeIfUndefined(deploy, DEPLOY_DEFAULTS) : DEPLOY_DEFAULTS) as resolveDeploy<deploy>;
}
12 changes: 0 additions & 12 deletions packages/world/ts/config/v2/deployment.ts

This file was deleted.

Loading
Loading