-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(store,world): add
namespaces
to config output (#2958)
- Loading branch information
Showing
12 changed files
with
574 additions
and
260 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { ErrorMessage, flatMorph } from "@arktype/util"; | ||
import { hasOwnKey, mergeIfUndefined } from "./generics"; | ||
import { NamespaceInput } from "./input"; | ||
import { resolveTables, validateTables } from "./tables"; | ||
import { AbiTypeScope, Scope } from "./scope"; | ||
|
||
export type validateNamespace<input, scope extends Scope = AbiTypeScope> = { | ||
[key in keyof input]: key extends "tables" | ||
? validateTables<input[key], scope> | ||
: key extends keyof NamespaceInput | ||
? NamespaceInput[key] | ||
: ErrorMessage<`\`${key & string}\` is not a valid namespace config option.`>; | ||
}; | ||
|
||
export function validateNamespace<scope extends Scope = AbiTypeScope>( | ||
input: unknown, | ||
scope: scope, | ||
): asserts input is NamespaceInput { | ||
if (hasOwnKey(input, "namespace") && typeof input.namespace === "string" && input.namespace.length > 14) { | ||
throw new Error(`\`namespace\` must fit into a \`bytes14\`, but "${input.namespace}" is too long.`); | ||
} | ||
if (hasOwnKey(input, "tables")) { | ||
validateTables(input.tables, scope); | ||
} | ||
} | ||
|
||
export type resolveNamespace<input, scope extends Scope = AbiTypeScope> = input extends NamespaceInput | ||
? { | ||
readonly label: input["label"]; | ||
readonly namespace: string; | ||
readonly tables: undefined extends input["tables"] | ||
? {} | ||
: resolveTables< | ||
{ | ||
readonly [label in keyof input["tables"]]: mergeIfUndefined< | ||
input["tables"][label], | ||
{ readonly namespace: string } | ||
>; | ||
}, | ||
scope | ||
>; | ||
} | ||
: never; | ||
|
||
export function resolveNamespace<const input extends NamespaceInput, scope extends Scope = AbiTypeScope>( | ||
input: input, | ||
scope: scope, | ||
): resolveNamespace<input, scope> { | ||
const label = input.label; | ||
const namespace = input.namespace ?? label.slice(0, 14); | ||
return { | ||
label, | ||
namespace, | ||
tables: resolveTables( | ||
flatMorph(input.tables ?? {}, (label, table) => { | ||
return [label, mergeIfUndefined(table, { namespace })]; | ||
}), | ||
scope, | ||
), | ||
} 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
Oops, something went wrong.