-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(store): add types for v2 config resolvers (#2290)
Co-authored-by: David Blass <[email protected]>
- Loading branch information
1 parent
36a3185
commit 927fe85
Showing
16 changed files
with
1,569 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,3 +19,5 @@ templates/*/pnpm-lock.yaml | |
|
||
test-data/world-logs-bulk-*.json | ||
test-data/world-logs-query.json | ||
|
||
.attest |
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 @@ | ||
export type get<input, key> = key extends keyof input ? input[key] : undefined; |
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,36 @@ | ||
import { describe, it } from "vitest"; | ||
import { resolveSchema } from "./schema"; | ||
import { extendScope, AbiTypeScope } from "./scope"; | ||
import { attest } from "@arktype/attest"; | ||
|
||
describe("resolveSchema", () => { | ||
it("should map user types to their primitive type", () => { | ||
const scope = extendScope(AbiTypeScope, { CustomType: "address" }); | ||
const resolved = resolveSchema({ regular: "uint256", user: "CustomType" }, scope); | ||
const expected = { | ||
regular: { | ||
type: "uint256", | ||
internalType: "uint256", | ||
}, | ||
user: { | ||
type: "address", | ||
internalType: "CustomType", | ||
}, | ||
} as const; | ||
attest<typeof expected>(resolved).type.toString.snap( | ||
'{ regular: { type: "uint256"; internalType: "uint256"; }; user: { type: "address"; internalType: "CustomType"; }; }', | ||
); | ||
}); | ||
|
||
it("should throw if a type is not part of the scope", () => { | ||
const scope = extendScope(AbiTypeScope, { CustomType: "address" }); | ||
resolveSchema( | ||
{ | ||
regular: "uint256", | ||
// @ts-expect-error Type '"NotACustomType"' is not assignable to type 'AbiType | "CustomType"'. | ||
user: "NotACustomType", | ||
}, | ||
scope, | ||
); | ||
}); | ||
}); |
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,23 @@ | ||
import { evaluate } from "@arktype/util"; | ||
import { AbiTypeScope } from "./scope"; | ||
|
||
export type SchemaInput<scope extends AbiTypeScope = AbiTypeScope> = { | ||
[key: string]: keyof scope["types"]; | ||
}; | ||
|
||
export type resolveSchema<schema extends SchemaInput<scope>, scope extends AbiTypeScope> = evaluate<{ | ||
[key in keyof schema]: { | ||
/** the Solidity primitive ABI type */ | ||
type: scope["types"][schema[key]]; | ||
/** the user defined type or Solidity primitive ABI type */ | ||
internalType: schema[key]; | ||
}; | ||
}>; | ||
|
||
export function resolveSchema<schema extends SchemaInput<scope>, scope extends AbiTypeScope = AbiTypeScope>( | ||
schema: schema, | ||
scope?: scope, | ||
): resolveSchema<schema, scope> { | ||
// TODO: runtime implementation | ||
return {} as never; | ||
} |
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,39 @@ | ||
import { attest } from "@arktype/attest"; | ||
import { describe, it } from "vitest"; | ||
import { AbiTypeScope, EmptyScope, ScopeOptions, extendScope, getStaticAbiTypeKeys } from "./scope"; | ||
|
||
describe("extendScope", () => { | ||
it("should extend the provided scope", () => { | ||
const extendedScope = extendScope(EmptyScope, { static: "uint256", dynamic: "string" }); | ||
attest<ScopeOptions<{ static: "uint256"; dynamic: "string" }>>(extendedScope).type.toString.snap( | ||
'{ types: { static: "uint256"; dynamic: "string"; }; }', | ||
); | ||
|
||
const furtherExtendedScope = extendScope(extendedScope, { static2: "uint256", dynamic2: "string" }); | ||
attest<ScopeOptions<{ static: "uint256"; dynamic: "string"; static2: "uint256"; dynamic2: "string" }>>( | ||
furtherExtendedScope, | ||
).type.toString.snap( | ||
'{ types: { static: "uint256"; dynamic: "string"; static2: "uint256"; dynamic2: "string"; }; }', | ||
); | ||
}); | ||
}); | ||
|
||
describe("getStaticAbiTypeKeys", () => { | ||
it("returns only static keys", () => { | ||
attest< | ||
"static" | "otherStatic", | ||
getStaticAbiTypeKeys<{ static: "uint256"; otherStatic: "address"; dynamic: "string" }> | ||
>(); | ||
}); | ||
|
||
it("returns only static keys with a scope", () => { | ||
const extendedScope = extendScope(AbiTypeScope, { static: "uint256", dynamic: "string" }); | ||
attest< | ||
"static" | "customStatic", | ||
getStaticAbiTypeKeys< | ||
{ static: "uint256"; dynamic: "string"; customStatic: "static"; customDynamic: "dynamic" }, | ||
typeof extendedScope | ||
> | ||
>(); | ||
}); | ||
}); |
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,36 @@ | ||
import { Dict, evaluate } from "@arktype/util"; | ||
import { SchemaInput } from "./schema"; | ||
import { StaticAbiType, DynamicAbiType } from "@latticexyz/schema-type"; | ||
|
||
export type AbiType = StaticAbiType | DynamicAbiType; | ||
|
||
export const EmptyScope = { types: {} } as const satisfies ScopeOptions; | ||
export type EmptyScope = typeof EmptyScope; | ||
|
||
export type AbiTypeScope = ScopeOptions<{ [t in AbiType]: t }>; | ||
export const AbiTypeScope = { types: {} } as AbiTypeScope; // TODO: runtime implementation | ||
|
||
export type ScopeOptions<types extends Dict<string, AbiType> = Dict<string, AbiType>> = { | ||
types: types; | ||
}; | ||
|
||
export type getStaticAbiTypeKeys< | ||
types extends SchemaInput<scope>, | ||
scope extends AbiTypeScope = AbiTypeScope, | ||
> = SchemaInput extends types | ||
? string | ||
: { | ||
[key in keyof types]: scope["types"][types[key]] extends StaticAbiType ? key : never; | ||
}[keyof types]; | ||
|
||
export type extendScope<scope extends ScopeOptions, additionalTypes extends Dict<string, AbiType>> = evaluate< | ||
ScopeOptions<evaluate<scope["types"] & additionalTypes>> | ||
>; | ||
|
||
export function extendScope<scope extends ScopeOptions, additionalTypes extends Dict<string, AbiType>>( | ||
scope: scope, | ||
additionalTypes: additionalTypes, | ||
): extendScope<scope, additionalTypes> { | ||
// TODO: runtime implementation | ||
return {} as never; | ||
} |
Oops, something went wrong.