Skip to content

Commit

Permalink
fix name type, add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
holic committed Jul 5, 2024
1 parent 16525a2 commit afde0de
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 3 deletions.
50 changes: 49 additions & 1 deletion packages/store/ts/config/v2/table.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,32 @@ describe("resolveTable", () => {
attest<typeof expected>(table.deploy).equals(expected);
});

it("should truncate label to form name", () => {
const table = defineTable({
schema: { id: "address", name: "string", age: "uint256" },
key: ["age"],
label: "ThisIsALongTableName",
});

const expected = {
tableId: resourceToHex({ type: "table", namespace: "", name: "ThisIsALongTable" }),
schema: {
id: { type: "address", internalType: "address" },
name: { type: "string", internalType: "string" },
age: { type: "uint256", internalType: "uint256" },
},
key: ["age"],
label: "ThisIsALongTableName",
name: "ThisIsALongTable",
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 throw if the provided key is a dynamic ABI type", () => {
attest(() =>
defineTable({
Expand Down Expand Up @@ -252,7 +278,29 @@ describe("resolveTable", () => {
// @ts-expect-error Key `keySchema` does not exist in TableInput
keySchema: { id: "address" },
}),
).type.errors("Key `keySchema` does not exist in TableInput ");
).type.errors("Key `keySchema` does not exist in TableInput");
});

it("should throw if an invalid namespace is provided", () => {
attest(() =>
defineTable({
schema: { id: "address" },
key: ["id"],
label: "",
namespace: "ThisNamespaceIsTooLong",
}),
).throws('Table `namespace` must fit into a `bytes14`, but "ThisNamespaceIsTooLong" is too long.');
});

it("should throw if an invalid name is provided", () => {
attest(() =>
defineTable({
schema: { id: "address" },
key: ["id"],
label: "",
name: "ThisNameIsTooLong",
}),
).throws('Table `name` must fit into a `bytes16`, but "ThisNameIsTooLong" is too long.');
});
});

Expand Down
12 changes: 10 additions & 2 deletions packages/store/ts/config/v2/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,20 @@ export function resolveTableCodegen<input extends TableInput>(input: input): res
} satisfies TableCodegen as never;
}

// TODO: is this helper worth it for the strongly typed substring?
// https://stackoverflow.com/a/66218917
type truncate<
T extends string,
N extends number,
L extends unknown[] = [],
A extends string = "",
> = N extends L["length"] ? A : T extends `${infer F}${infer R}` ? truncate<R, N, [0, ...L], `${A}${F}`> : A;

export type resolveTable<input, scope extends Scope = Scope> = input extends TableInput
? {
readonly tableId: Hex;
readonly label: input["label"];
// TODO: return `string` instead of label, since we can't truncate label in TS
readonly name: undefined extends input["name"] ? input["label"] : input["name"];
readonly name: undefined extends input["name"] ? truncate<input["label"], 16> : input["name"];
readonly namespace: undefined extends input["namespace"] ? typeof TABLE_DEFAULTS.namespace : input["namespace"];
readonly type: undefined extends input["type"] ? typeof TABLE_DEFAULTS.type : input["type"];
readonly key: Readonly<input["key"]>;
Expand Down

0 comments on commit afde0de

Please sign in to comment.