Adjust lazyVariants with internal $type (#5081)

* Adjust lazyVariants with internal $type

* Adjust

* Allow for empty paths

* Allow any $ override

* Adjust

* Small usage fixes, dedupe
This commit is contained in:
Jaco
2022-07-18 09:12:14 +03:00
committed by GitHub
parent 12750bc83d
commit 14f2a9da39
16 changed files with 46 additions and 33 deletions
+8 -2
View File
@@ -3,6 +3,7 @@
import type { SiLookupTypeId, SiVariant } from '../interfaces';
import type { PortableRegistry } from '../metadata';
import type { SectionMetadata } from './types';
import { lazyMethod } from '@polkadot/util';
@@ -11,12 +12,17 @@ interface TypeHolder {
}
export function lazyVariants <T> (lookup: PortableRegistry, { type }: TypeHolder, getName: (v: SiVariant) => string, creator: (v: SiVariant) => T): Record<string, T> {
const result: Record<string, T> = {};
const variants = lookup.getSiType(type).def.asVariant.variants;
const result: Record<string, T> & SectionMetadata = {};
const { def, path } = lookup.getSiType(type);
const variants = def.asVariant.variants;
for (let i = 0; i < variants.length; i++) {
lazyMethod(result, variants[i], creator, getName);
}
result.$path = path.length
? path.join('::')
: undefined;
return result;
}
+10 -10
View File
@@ -1,11 +1,11 @@
// Copyright 2017-2022 @polkadot/types authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { Codec, CodecClass, IU8a } from '@polkadot/types-codec/types';
import type { AnyString, Codec, CodecClass, IU8a } from '@polkadot/types-codec/types';
import type { CreateOptions, TypeDef } from '@polkadot/types-create/types';
import type { ExtDef } from '../extrinsic/signedExtensions/types';
import type { ChainProperties, DispatchErrorModule, DispatchErrorModuleU8, DispatchErrorModuleU8a, EventMetadataLatest, Hash, MetadataLatest, SiField, SiLookupTypeId, SiVariant } from '../interfaces/types';
import type { CallFunction, CodecHasher, Definitions, DetectCodec, RegisteredTypes, Registry, RegistryError, RegistryTypes } from '../types';
import type { CallFunction, CodecHasher, Definitions, DetectCodec, RegisteredTypes, Registry, RegistryError, RegistryTypes, SectionMetadata } from '../types';
import { DoNotConstruct, Json, Raw } from '@polkadot/types-codec';
import { constructTypeClass, createClassUnsafe, createTypeUnsafe } from '@polkadot/types-create';
@@ -57,7 +57,7 @@ function getVariantStringIdx ({ index }: SiVariant): string {
}
// create error mapping from metadata
function injectErrors (_: TypeRegistry, { lookup, pallets }: MetadataLatest, version: number, result: Record<string, Record<string, RegistryError>>): void {
function injectErrors (_: TypeRegistry, { lookup, pallets }: MetadataLatest, version: number, result: Record<string, Record<string, RegistryError> & SectionMetadata>): void {
clearRecord(result);
for (let i = 0; i < pallets.length; i++) {
@@ -82,7 +82,7 @@ function injectErrors (_: TypeRegistry, { lookup, pallets }: MetadataLatest, ver
}
// create event classes from metadata
function injectEvents (registry: TypeRegistry, { lookup, pallets }: MetadataLatest, version: number, result: Record<string, Record<string, CodecClass<GenericEventData>>>): void {
function injectEvents (registry: TypeRegistry, { lookup, pallets }: MetadataLatest, version: number, result: Record<string, Record<string, CodecClass<GenericEventData> & SectionMetadata>>): void {
const filtered = pallets.filter(filterEventsSome);
clearRecord(result);
@@ -105,7 +105,7 @@ function injectEvents (registry: TypeRegistry, { lookup, pallets }: MetadataLate
}
// create extrinsic mapping from metadata
function injectExtrinsics (registry: TypeRegistry, { lookup, pallets }: MetadataLatest, version: number, result: Record<string, Record<string, CallFunction>>): void {
function injectExtrinsics (registry: TypeRegistry, { lookup, pallets }: MetadataLatest, version: number, result: Record<string, Record<string, CallFunction> & SectionMetadata>): void {
const filtered = pallets.filter(filterCallsSome);
clearRecord(result);
@@ -150,11 +150,11 @@ export class TypeRegistry implements Registry {
#metadataVersion = 0;
readonly #metadataCalls: Record<string, Record<string, CallFunction>> = {};
readonly #metadataCalls: Record<string, Record<string, CallFunction> & SectionMetadata> = {};
readonly #metadataErrors: Record<string, Record<string, RegistryError>> = {};
readonly #metadataErrors: Record<string, Record<string, RegistryError> & SectionMetadata> = {};
readonly #metadataEvents: Record<string, Record<string, CodecClass<GenericEventData>>> = {};
readonly #metadataEvents: Record<string, Record<string, CodecClass<GenericEventData>> & SectionMetadata> = {};
#unknownTypes = new Map<string, boolean>();
@@ -403,8 +403,8 @@ export class TypeRegistry implements Registry {
return this.#definitions.get(typeName);
}
public getModuleInstances (specName: string, moduleName: string): string[] | undefined {
return this.#knownTypes?.typesBundle?.spec?.[specName]?.instances?.[moduleName];
public getModuleInstances (specName: AnyString, moduleName: string): string[] | undefined {
return this.#knownTypes?.typesBundle?.spec?.[specName.toString()]?.instances?.[moduleName];
}
public getOrThrow <T extends Codec = Codec, K extends string = string, R = DetectCodec<T, K>> (name: K, msg?: string): CodecClass<R> {
+4
View File
@@ -5,3 +5,7 @@ export type { CodecCreateOptions as CreateOptions } from '@polkadot/types-codec/
export type { TypeDef } from '@polkadot/types-create/types';
export { TypeDefInfo } from '@polkadot/types-create';
export interface SectionMetadata {
$path?: string;
}
+2 -2
View File
@@ -6,7 +6,7 @@
import '@polkadot/types-codec/types/registry';
import '@polkadot/types-create/types/augmentRegistry';
import type { Codec, CodecClass } from '@polkadot/types-codec/types';
import type { AnyString, Codec, CodecClass } from '@polkadot/types-codec/types';
import type { TypeDef } from '@polkadot/types-create/types';
import type { ExtDef } from '../extrinsic/signedExtensions/types';
import type { MetadataLatest } from '../interfaces/metadata';
@@ -41,7 +41,7 @@ declare module '@polkadot/types-codec/types/registry' {
get <T extends Codec = Codec, K extends string = string> (name: K, withUnknown?: boolean, knownTypeDef?: TypeDef): CodecClass<DetectCodec<T, K>> | undefined;
getChainProperties (): ChainProperties | undefined;
getDefinition (typeName: string): string | undefined;
getModuleInstances (specName: string, moduleName: string): string[] | undefined;
getModuleInstances (specName: AnyString, moduleName: string): string[] | undefined;
setKnownTypes (types: RegisteredTypes): void;
setChainProperties (properties?: ChainProperties): void;