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

refactor(store,world): simplify table shorthands #2969

Merged
merged 8 commits into from
Jul 24, 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
6 changes: 6 additions & 0 deletions .changeset/wicked-numbers-check.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@latticexyz/store": patch
"@latticexyz/world": patch
---

Refactored how the config handles shorthand table definitions, greatly simplifying the codebase. This will make it easier to add support for multiple namespaces.
1 change: 0 additions & 1 deletion packages/store/ts/config/v2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ export * from "./generics";
export * from "./scope";
export * from "./schema";
export * from "./tableShorthand";
export * from "./storeWithShorthands";
export * from "./table";
export * from "./tables";
export * from "./store";
Expand Down
20 changes: 5 additions & 15 deletions packages/store/ts/config/v2/input.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Hex } from "viem";
import { Codegen, TableCodegen, TableDeploy, UserTypes } from "./output";
import { Scope } from "./scope";
import { show } from "@arktype/util";

export type EnumsInput = {
readonly [enumName: string]: readonly [string, ...string[]];
Expand Down Expand Up @@ -45,9 +44,12 @@ export type TableInput = {
readonly deploy?: TableDeployInput;
};

export type TableShorthandInput = SchemaInput | string;

export type TablesInput = {
// remove label and namespace as these are set contextually
readonly [label: string]: Omit<TableInput, "label" | "namespace">;
// remove label and namespace from table input as these are set contextually
// and allow defining a table using shorthand
readonly [label: string]: Omit<TableInput, "label" | "namespace"> | TableShorthandInput;
};

export type CodegenInput = Partial<Codegen>;
Expand Down Expand Up @@ -78,15 +80,3 @@ export type StoreInput = Omit<NamespaceInput, "label"> & {
readonly enums?: EnumsInput;
readonly codegen?: CodegenInput;
};

/******** Variations with shorthands ********/

export type TableShorthandInput = SchemaInput | string;

export type TablesWithShorthandsInput = {
readonly [label: string]: TablesInput[string] | TableShorthandInput;
};

export type StoreWithShorthandsInput = show<
Omit<StoreInput, "tables"> & { readonly tables?: TablesWithShorthandsInput }
>;
5 changes: 3 additions & 2 deletions packages/store/ts/config/v2/namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { hasOwnKey, mergeIfUndefined } from "./generics";
import { NamespaceInput } from "./input";
import { resolveTables, validateTables } from "./tables";
import { AbiTypeScope, Scope } from "./scope";
import { expandTableShorthand } from "./tableShorthand";

export type validateNamespace<input, scope extends Scope = AbiTypeScope> = {
[key in keyof input]: key extends "tables"
Expand Down Expand Up @@ -33,7 +34,7 @@ export type resolveNamespace<input, scope extends Scope = AbiTypeScope> = input
: resolveTables<
{
readonly [label in keyof input["tables"]]: mergeIfUndefined<
input["tables"][label],
expandTableShorthand<input["tables"][label]>,
{ readonly namespace: string }
>;
},
Expand All @@ -53,7 +54,7 @@ export function resolveNamespace<const input extends NamespaceInput, scope exten
namespace,
tables: resolveTables(
flatMorph(input.tables ?? {}, (label, table) => {
return [label, mergeIfUndefined(table, { namespace })];
return [label, mergeIfUndefined(expandTableShorthand(table, scope), { namespace })];
}),
scope,
),
Expand Down
4 changes: 2 additions & 2 deletions packages/store/ts/config/v2/namespacedTables.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { flatMorph } from "@arktype/util";
import { flatMorph, show } from "@arktype/util";
import { Tables } from "./output";

/**
Expand All @@ -16,7 +16,7 @@ export type resolveNamespacedTables<tables, namespace> = {
export function resolveNamespacedTables<tables, namespace>(
tables: tables,
namespace: namespace,
): resolveNamespacedTables<tables, namespace> {
): show<resolveNamespacedTables<tables, namespace>> {
return flatMorph(tables as Tables, (label, table) => [
namespace === "" ? label : `${namespace}__${label}`,
table,
Expand Down
Loading
Loading