Skip to content

Commit

Permalink
feat(protocol-parser): add new schema helpers (#2478)
Browse files Browse the repository at this point in the history
  • Loading branch information
holic authored Mar 20, 2024
1 parent e3f0abe commit 6e88c77
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 25 deletions.
1 change: 1 addition & 0 deletions packages/protocol-parser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
},
"dependencies": {
"@latticexyz/common": "workspace:*",
"@latticexyz/config": "workspace:*",
"@latticexyz/schema-type": "workspace:*",
"abitype": "1.0.0",
"viem": "2.7.12"
Expand Down
4 changes: 4 additions & 0 deletions packages/protocol-parser/src/exports/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ export * from "../schemaToHex";
export * from "../staticDataLength";
export * from "../valueSchemaToFieldLayoutHex";
export * from "../valueSchemaToHex";

export * from "../getKeySchema";
export * from "../getValueSchema";
export * from "../getSchemaTypes";
9 changes: 9 additions & 0 deletions packages/protocol-parser/src/getKeySchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Table } from "@latticexyz/config";

export type getKeySchema<table extends Table> = {
[fieldName in table["key"][number]]: table["schema"][fieldName];
};

export function getKeySchema<table extends Table>(table: table): getKeySchema<table> {
return Object.fromEntries(table.key.map((fieldName) => [fieldName, table.schema[fieldName]])) as getKeySchema<table>;
}
10 changes: 10 additions & 0 deletions packages/protocol-parser/src/getSchemaTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Schema } from "@latticexyz/config";
import { mapObject } from "@latticexyz/common/utils";

export type getSchemaTypes<schema extends Schema> = {
readonly [k in keyof schema]: schema[k]["type"];
};

export function getSchemaTypes<schema extends Schema>(schema: schema): getSchemaTypes<schema> {
return mapObject(schema, (value) => value.type);
}
11 changes: 11 additions & 0 deletions packages/protocol-parser/src/getValueSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Table } from "@latticexyz/config";

export type getValueSchema<table extends Table> = {
[fieldName in Exclude<keyof table["schema"], table["key"][number]>]: table["schema"][fieldName];
};

export function getValueSchema<table extends Table>(table: table): getValueSchema<table> {
return Object.fromEntries(
Object.entries(table.schema).filter(([fieldName]) => !table.key.includes(fieldName)),
) as getValueSchema<table>;
}
12 changes: 9 additions & 3 deletions packages/store-sync/src/query-cache/createStorageAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@ import { StorageAdapter } from "../common";
import { QueryCacheStore, RawTableRecord, TableRecord } from "./createStore";
import { hexToResource, resourceToLabel, spliceHex } from "@latticexyz/common";
import { Hex, concatHex, size } from "viem";
import { decodeKey, decodeValueArgs } from "@latticexyz/protocol-parser/internal";
import {
KeySchema,
decodeKey,
decodeValueArgs,
getKeySchema,
getValueSchema,
} from "@latticexyz/protocol-parser/internal";
import { flattenSchema } from "../flattenSchema";
import debug from "debug";
import { Tables } from "./common";
import { getKeySchema, getValueSchema } from "@latticexyz/store/config/v2";

function getRecordId({ tableId, keyTuple }: { tableId: Hex; keyTuple: readonly Hex[] }): string {
return `${tableId}:${concatHex(keyTuple)}`;
Expand Down Expand Up @@ -110,7 +115,8 @@ export function createStorageAdapter({ store }: CreateStorageAdapterOptions<Quer
const records: readonly TableRecord[] = [
...previousRecords.filter((record) => !touchedIds.has(record.id)),
...Object.values(updatedRawRecords).map((rawRecord): TableRecord => {
const key = decodeKey(flattenSchema(getKeySchema(rawRecord.table)), rawRecord.keyTuple);
const keySchema = flattenSchema(getKeySchema(rawRecord.table));
const key = decodeKey(keySchema as KeySchema, rawRecord.keyTuple);
const value = decodeValueArgs(flattenSchema(getValueSchema(rawRecord.table)), rawRecord);

return {
Expand Down
3 changes: 2 additions & 1 deletion packages/store-sync/src/query-cache/createStore.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { StoreApi, UseBoundStore, create } from "zustand";
import { Table, getKeySchema, getValueSchema } from "@latticexyz/store/config/v2";
import { Table } from "@latticexyz/store/config/v2";
import { Tables } from "./common";
import { Hex } from "viem";
import { StaticPrimitiveType } from "@latticexyz/schema-type/internal";
import { SchemaToPrimitives } from "@latticexyz/store";
import { getKeySchema, getValueSchema } from "@latticexyz/protocol-parser/internal";

export type RawTableRecord<table extends Table = Table> = {
readonly table: table;
Expand Down
2 changes: 1 addition & 1 deletion packages/store/ts/config/v2/compat.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { conform } from "@arktype/util";
import { Schema, Store, Table } from "./output";
import { mapObject } from "@latticexyz/common/utils";
import { getKeySchema, getValueSchema } from "./table";
import { getKeySchema, getValueSchema } from "@latticexyz/protocol-parser/internal";

export type storeToV1<store> = store extends Store
? {
Expand Down
5 changes: 4 additions & 1 deletion packages/store/ts/config/v2/table.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { attest } from "@arktype/attest";
import { describe, it } from "vitest";
import { getStaticAbiTypeKeys, AbiTypeScope, extendScope } from "./scope";
import { validateKeys, defineTable, getKeySchema, getValueSchema } from "./table";
import { validateKeys, defineTable } from "./table";
import { TABLE_CODEGEN_DEFAULTS } from "./defaults";
import { resourceToHex } from "@latticexyz/common";
import { getKeySchema, getValueSchema } from "@latticexyz/protocol-parser/internal";

describe("validateKeys", () => {
it("should return a tuple of valid keys", () => {
Expand Down Expand Up @@ -186,6 +187,7 @@ describe("resolveTable", () => {
});
});

// TODO: move tests to protocol parser after we add arktype
describe("getKeySchema", () => {
it("should return the fields of the schema that are part of the key", () => {
const scope = extendScope(AbiTypeScope, { Static: "address", Dynamic: "string" });
Expand All @@ -209,6 +211,7 @@ describe("getKeySchema", () => {
});
});

// TODO: move tests to protocol parser after we add arktype
describe("getValueSchema", () => {
it("should return the fields of the schema that are not part of the key", () => {
const scope = extendScope(AbiTypeScope, { Static: "address", Dynamic: "string" });
Expand Down
19 changes: 0 additions & 19 deletions packages/store/ts/config/v2/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { TableCodegen } from "./output";
import { TABLE_CODEGEN_DEFAULTS, TABLE_DEFAULTS } from "./defaults";
import { resourceToHex } from "@latticexyz/common";
import { SchemaInput, TableInput } from "./input";
import { Table } from "@latticexyz/config";

export type ValidKeys<schema extends SchemaInput, scope extends Scope> = readonly [
getStaticAbiTypeKeys<schema, scope>,
Expand Down Expand Up @@ -160,21 +159,3 @@ export function defineTable<input, scope extends Scope = AbiTypeScope>(
validateTable(input, scope);
return resolveTable(input, scope) as resolveTable<input, scope>;
}

export type getKeySchema<table extends Table> = {
[fieldName in table["key"][number]]: table["schema"][fieldName];
};

export function getKeySchema<table extends Table>(table: table): getKeySchema<table> {
return Object.fromEntries(table.key.map((fieldName) => [fieldName, table.schema[fieldName]])) as getKeySchema<table>;
}

export type getValueSchema<table extends Table> = {
[fieldName in Exclude<keyof table["schema"], table["key"][number]>]: table["schema"][fieldName];
};

export function getValueSchema<table extends Table>(table: table): getValueSchema<table> {
return Object.fromEntries(
Object.entries(table.schema).filter(([fieldName]) => !table.key.includes(fieldName)),
) as getValueSchema<table>;
}
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6e88c77

Please sign in to comment.