-
Notifications
You must be signed in to change notification settings - Fork 196
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
WIP config parser #1561
Closed
Closed
WIP config parser #1561
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
c5dca9e
wip
holic c4a1a5c
hitting limits of zod
holic 1704157
just use TS
holic 6a4c157
more fiddling
holic e8df626
do config parsing in smaller steps
holic e35566b
add config parsing
holic af32ed9
rename
holic 870d725
move out tables parsing
holic b135aba
add namespaces
holic 2fcbc78
refactor tests
holic 44bfd09
Merge remote-tracking branch 'origin/main' into holic/config-parser
holic 158ad61
update after merge
holic fae8195
playing with namespaces/merging objects
holic 3b3b915
Readonly helper to readonly keys
holic 9234e3e
small tweaks
holic e3363d3
clean up
holic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
export type EmptyObject = { readonly [k: string]: never }; | ||
|
||
export type Prettify<T> = { | ||
[K in keyof T as T[K] extends never ? never : K]: T[K]; | ||
} & unknown; | ||
|
||
/** | ||
* Merges two object types into new type | ||
* | ||
* @param Object1 - Object to merge into | ||
* @param Object2 - Object to merge and override keys from {@link Object1} | ||
* @returns New object type with keys from {@link Object1} and {@link Object2}. If a key exists in both {@link Object1} and {@link Object2}, the key from {@link Object2} will be used. | ||
* | ||
* @example | ||
* type Result = Merge<{ foo: string }, { foo: number; bar: string }> | ||
* // ^? type Result = { foo: number; bar: string } | ||
*/ | ||
export type Merge<Object1, Object2> = Omit<Object1, keyof Object2> & Object2; | ||
|
||
/** @internal */ | ||
export function isPlainObject(value: unknown): value is Record<string, unknown> { | ||
return ( | ||
typeof value === "object" && | ||
value !== null && | ||
value.constructor === Object && | ||
Object.prototype.toString.call(value) === "[object Object]" | ||
); | ||
} |
144 changes: 144 additions & 0 deletions
144
packages/store/ts/config/experimental/parseConfig.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
import { describe, expect, expectTypeOf, it } from "vitest"; | ||
import { parseConfig } from "./parseConfig"; | ||
import { resourceIdToHex } from "@latticexyz/common"; | ||
|
||
describe("parseConfig", () => { | ||
it("outputs tables from config", () => { | ||
const output = parseConfig({ | ||
tables: { | ||
Exists: "bool", | ||
Position: { | ||
valueSchema: { x: "uint32", y: "uint32" }, | ||
}, | ||
Messages: { | ||
offchainOnly: true, | ||
valueSchema: { | ||
sender: "address", | ||
message: "string", | ||
}, | ||
}, | ||
}, | ||
} as const); | ||
|
||
const expectedOutput = { | ||
tables: { | ||
Exists: { | ||
type: "table", | ||
namespace: "", | ||
name: "Exists", | ||
tableId: resourceIdToHex({ type: "table", namespace: "", name: "Exists" }), | ||
keySchema: { | ||
key: "bytes32", | ||
}, | ||
valueSchema: { | ||
value: "bool", | ||
}, | ||
}, | ||
Position: { | ||
type: "table", | ||
namespace: "", | ||
name: "Position", | ||
tableId: resourceIdToHex({ type: "table", namespace: "", name: "Position" }), | ||
keySchema: { | ||
key: "bytes32", | ||
}, | ||
valueSchema: { | ||
x: "uint32", | ||
y: "uint32", | ||
}, | ||
}, | ||
Messages: { | ||
type: "offchainTable", | ||
namespace: "", | ||
name: "Messages", | ||
tableId: resourceIdToHex({ type: "offchainTable", namespace: "", name: "Messages" }), | ||
keySchema: { | ||
key: "bytes32", | ||
}, | ||
valueSchema: { | ||
message: "string", | ||
sender: "address", | ||
}, | ||
}, | ||
}, | ||
} as const; | ||
|
||
expect(output).toStrictEqual(expectedOutput); | ||
expectTypeOf(output).toEqualTypeOf(expectedOutput); | ||
expectTypeOf(output).toMatchTypeOf(expectedOutput); | ||
}); | ||
|
||
it("handles namespaces", () => { | ||
const output = parseConfig({ | ||
namespace: "DefaultNamespace", | ||
tables: { | ||
Exists: "bool", | ||
Position: { | ||
namespace: "TableNamespace", | ||
valueSchema: { x: "uint32", y: "uint32" }, | ||
}, | ||
}, | ||
namespaces: { | ||
MyNamespace: { | ||
tables: { | ||
PlayerNames: "string", | ||
Exists: { | ||
// TODO: disable overriding namespace here | ||
namespace: "OverrideNamespace", | ||
valueSchema: { | ||
exists: "bool", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} as const); | ||
|
||
const expectedOutput = { | ||
tables: { | ||
Exists: { | ||
type: "table", | ||
namespace: "OverrideNamespace", | ||
name: "Exists", | ||
tableId: resourceIdToHex({ type: "table", namespace: "OverrideNamespace", name: "Exists" }), | ||
keySchema: { | ||
key: "bytes32", | ||
}, | ||
valueSchema: { | ||
exists: "bool", | ||
}, | ||
}, | ||
Position: { | ||
type: "table", | ||
namespace: "TableNamespace", | ||
name: "Position", | ||
tableId: resourceIdToHex({ type: "table", namespace: "TableNamespace", name: "Position" }), | ||
keySchema: { | ||
key: "bytes32", | ||
}, | ||
valueSchema: { | ||
x: "uint32", | ||
y: "uint32", | ||
}, | ||
}, | ||
PlayerNames: { | ||
type: "table", | ||
namespace: "MyNamespace", | ||
name: "PlayerNames", | ||
tableId: resourceIdToHex({ type: "table", namespace: "MyNamespace", name: "PlayerNames" }), | ||
keySchema: { | ||
key: "bytes32", | ||
}, | ||
valueSchema: { | ||
value: "string", | ||
}, | ||
}, | ||
}, | ||
} as const; | ||
|
||
// TODO: why is PlayerNames disappearing? | ||
expect(output).toStrictEqual(expectedOutput); | ||
expectTypeOf(output).toEqualTypeOf(expectedOutput); | ||
expectTypeOf(output).toMatchTypeOf(expectedOutput); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { EmptyObject, Merge, Prettify } from "./common"; | ||
import { ParseNamespacesInput, ParseNamespacesOutput, parseNamespaces } from "./parseNamespaces"; | ||
import { ParseTablesInput, ParseTablesOutput, parseTables } from "./parseTables"; | ||
|
||
export type ParseConfigInput = { | ||
readonly namespace?: string; | ||
readonly tables?: ParseTablesInput; | ||
readonly namespaces?: ParseNamespacesInput; | ||
}; | ||
|
||
export type ParseConfigOutput< | ||
input extends ParseConfigInput, | ||
namespaces extends ParseNamespacesInput = input["namespaces"] extends ParseNamespacesInput | ||
? input["namespaces"] | ||
: EmptyObject | ||
> = { | ||
// TODO: ensure that tables of the same name get replaced and are not a union | ||
readonly tables: Prettify< | ||
ParseTablesOutput< | ||
input["namespace"] extends string ? input["namespace"] : "", | ||
input["tables"] extends ParseTablesInput ? input["tables"] : EmptyObject | ||
> & | ||
ParseNamespacesOutput<namespaces> | ||
>; | ||
}; | ||
|
||
export function parseConfig<input extends ParseConfigInput>(input: input): Prettify<ParseConfigOutput<input>> { | ||
const tables = Object.entries(parseTables(input.namespace ?? "", input.tables ?? {})); | ||
const namespacedTables = Object.entries(parseNamespaces(input.namespaces ?? {})); | ||
return { | ||
tables: Object.fromEntries([...tables, ...namespacedTables]), | ||
} as ParseConfigOutput<input>; | ||
} |
38 changes: 38 additions & 0 deletions
38
packages/store/ts/config/experimental/parseKeySchema.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { describe, expect, expectTypeOf, it } from "vitest"; | ||
import { parseKeySchema } from "./parseKeySchema"; | ||
|
||
// TODO: add tests for failing cases (dynamic ABI types) | ||
|
||
describe("parseKeySchema", () => { | ||
it("outputs a key schema for uint8", () => { | ||
const output = parseKeySchema("uint8"); | ||
const expectedOutput = { key: "uint8" } as const; | ||
expect(output).toStrictEqual(output); | ||
expectTypeOf(output).toEqualTypeOf(expectedOutput); | ||
expectTypeOf(output).toMatchTypeOf(expectedOutput); | ||
}); | ||
|
||
it("outputs a key schema for bool", () => { | ||
const output = parseKeySchema("bool"); | ||
const expectedOutput = { key: "bool" } as const; | ||
expect(output).toStrictEqual(output); | ||
expectTypeOf(output).toEqualTypeOf(expectedOutput); | ||
expectTypeOf(output).toMatchTypeOf(expectedOutput); | ||
}); | ||
|
||
it("returns a full key schema", () => { | ||
const output = parseKeySchema({ x: "uint32", y: "uint32" } as const); | ||
const expectedOutput = { x: "uint32", y: "uint32" } as const; | ||
expect(output).toStrictEqual(output); | ||
expectTypeOf(output).toEqualTypeOf(expectedOutput); | ||
expectTypeOf(output).toMatchTypeOf(expectedOutput); | ||
}); | ||
|
||
it("defaults key schema when undefined", () => { | ||
const output = parseKeySchema(undefined); | ||
const expectedOutput = { key: "bytes32" } as const; | ||
expect(output).toStrictEqual(output); | ||
expectTypeOf(output).toEqualTypeOf(expectedOutput); | ||
expectTypeOf(output).toMatchTypeOf(expectedOutput); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { StaticAbiType, isStaticAbiType } from "@latticexyz/schema-type"; | ||
|
||
export type KeySchema = { readonly [k: string]: StaticAbiType }; | ||
|
||
export const defaultKeySchema = { key: "bytes32" } as const satisfies KeySchema; | ||
|
||
export type ParseKeySchemaInput = StaticAbiType | KeySchema | undefined; | ||
|
||
export type ParseKeySchemaOutput<input extends ParseKeySchemaInput> = input extends undefined | ||
? typeof defaultKeySchema | ||
: input extends StaticAbiType | ||
? { readonly key: input } | ||
: input extends KeySchema | ||
? input | ||
: never; | ||
|
||
export function parseKeySchema<input extends ParseKeySchemaInput>(input: input): ParseKeySchemaOutput<input> { | ||
return ( | ||
input === undefined ? defaultKeySchema : isStaticAbiType(input) ? { key: input } : input | ||
) as ParseKeySchemaOutput<input>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { EmptyObject } from "./common"; | ||
import { ParseTableInput, ParseTableOutput, parseTable } from "./parseTable"; | ||
|
||
export type ParseNamespaceInput = { | ||
// TODO: omit namespace from table input | ||
readonly tables?: { readonly [k: string]: ParseTableInput }; | ||
}; | ||
|
||
export type ParseNamespaceOutput<namespace extends string, input extends ParseNamespaceInput> = { | ||
readonly tables: input["tables"] extends ParseTableInput | ||
? { | ||
readonly [name in keyof input["tables"]]: ParseTableOutput<namespace, name & string, input["tables"][name]>; | ||
} | ||
: EmptyObject; | ||
}; | ||
|
||
export function parseNamespace<namespace extends string, input extends ParseNamespaceInput>( | ||
namespace: namespace, | ||
input: input | ||
): ParseNamespaceOutput<namespace, input> { | ||
return { | ||
tables: Object.fromEntries( | ||
// TODO: remove namespace from tableInput or override with the namespace we have | ||
// though this may not be needed if our types omit it | ||
Object.entries(input.tables ?? {}).map(([name, tableInput]) => [name, parseTable(namespace, name, tableInput)]) | ||
), | ||
} as ParseNamespaceOutput<namespace, input>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { ParseNamespaceInput, ParseNamespaceOutput, parseNamespace } from "./parseNamespace"; | ||
|
||
export type ParseNamespacesInput = { readonly [k: string]: ParseNamespaceInput }; | ||
|
||
export type ParseNamespacesOutput<input extends ParseNamespacesInput> = { | ||
readonly [namespace in keyof input]: ParseNamespaceOutput<namespace & string, input[namespace]>; | ||
}[keyof input]["tables"]; | ||
|
||
// TODO: rename to parseNamespacedTables | ||
export function parseNamespaces<input extends ParseNamespacesInput>(input: input): ParseNamespacesOutput<input> { | ||
return Object.fromEntries( | ||
Object.entries(input).flatMap(([namespace, namespaceInput]) => { | ||
const { tables } = parseNamespace(namespace, namespaceInput); | ||
return Object.entries(tables); | ||
}) | ||
) as ParseNamespacesOutput<input>; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what's the difference between
toEqualTypeOf
andtoMatchTypeOf
?