-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
66 additions
and
30 deletions.
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
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
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,48 @@ | ||
import { SchemaAbiType, SchemaAbiTypeToPrimitiveType, StaticAbiType } from "@latticexyz/schema-type"; | ||
import { FieldData, FullSchemaConfig, StoreConfig } from "./config"; | ||
|
||
export type KeySchema = Record<string, StaticAbiType>; | ||
export type ValueSchema = Record<string, SchemaAbiType>; | ||
|
||
/** Map a table schema like `{ value: "uint256" }` to its primitive types like `{ value: bigint }` */ | ||
export type SchemaToPrimitives<TSchema extends ValueSchema> = { | ||
[key in keyof TSchema]: SchemaAbiTypeToPrimitiveType<TSchema[key]>; | ||
}; | ||
|
||
export type TableRecord<TKeySchema extends KeySchema = KeySchema, TValueSchema extends ValueSchema = ValueSchema> = { | ||
key: SchemaToPrimitives<TKeySchema>; | ||
value: SchemaToPrimitives<TValueSchema>; | ||
}; | ||
|
||
export type ConfigFieldTypeToPrimitiveType<T extends FieldData<string>> = T extends SchemaAbiType | ||
? SchemaAbiTypeToPrimitiveType<T> | ||
: T extends `${string}[${string}]` // field type might include enums and enum arrays, which are mapped to uint8/uint8[] | ||
? number[] // map enum arrays to `number[]` | ||
: number; // map enums to `number` | ||
|
||
/** Map a table schema config like `{ value: "uint256", type: "SomeEnum" }` to its primitive types like `{ value: bigint, type: number }` */ | ||
export type SchemaConfigToPrimitives<T extends FullSchemaConfig> = { | ||
[key in keyof T]: ConfigFieldTypeToPrimitiveType<T[key]>; | ||
}; | ||
|
||
export type ConfigToTablesPrimitives<C extends StoreConfig> = { | ||
[key in keyof C["tables"]]: { | ||
key: SchemaConfigToPrimitives<C["tables"][key]["keySchema"]>; | ||
value: SchemaConfigToPrimitives<C["tables"][key]["schema"]>; | ||
}; | ||
}; | ||
|
||
export type ConfigToKeyPrimitives< | ||
C extends StoreConfig, | ||
Table extends keyof ConfigToTablesPrimitives<C> | ||
> = ConfigToTablesPrimitives<C>[Table]["key"]; | ||
|
||
export type ConfigToValuePrimitives< | ||
C extends StoreConfig, | ||
Table extends keyof ConfigToTablesPrimitives<C> | ||
> = ConfigToTablesPrimitives<C>[Table]["value"]; | ||
|
||
export type ConfigToRecordPrimitives<C extends StoreConfig, Table extends keyof ConfigToTablesPrimitives<C>> = { | ||
key: ConfigToKeyPrimitives<C, Table>; | ||
value: ConfigToValuePrimitives<C, Table>; | ||
}; |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.