Augment Registry (w/o CodecRegistry & CreateRegistry) (#4463)
* Augment Registry (w/o CodecRegistry & CreateRegistry) * Build fixes * Final build fixes * licence headers
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
// Copyright 2017-2022 @polkadot/types-create authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { Codec, CodecClass, CodecRegistry, U8aBitLength, UIntBitLength } from '@polkadot/types-codec/types';
|
||||
import type { CreateRegistry, TypeDef } from '../types';
|
||||
import type { Codec, CodecClass, Registry, U8aBitLength, UIntBitLength } from '@polkadot/types-codec/types';
|
||||
import type { TypeDef } from '../types';
|
||||
|
||||
import { BTreeMap, BTreeSet, Bytes, CodecSet, Compact, DoNotConstruct, Enum, HashMap, Int, Null, Option, Range, RangeInclusive, Result, Struct, Tuple, U8aFixed, UInt, Vec, VecFixed, WrapperKeepOpaque, WrapperOpaque } from '@polkadot/types-codec';
|
||||
import { assert, isNumber, stringify } from '@polkadot/util';
|
||||
@@ -63,20 +63,20 @@ function createWithSub (Clazz: { with: (t: string) => CodecClass<Codec> }, value
|
||||
return Clazz.with(getSubType(value));
|
||||
}
|
||||
|
||||
const infoMapping: Record<TypeDefInfo, (registry: CodecRegistry, value: TypeDef) => CodecClass<Codec>> = {
|
||||
[TypeDefInfo.BTreeMap]: (registry: CodecRegistry, value: TypeDef): CodecClass<Codec> =>
|
||||
const infoMapping: Record<TypeDefInfo, (registry: Registry, value: TypeDef) => CodecClass<Codec>> = {
|
||||
[TypeDefInfo.BTreeMap]: (registry: Registry, value: TypeDef): CodecClass<Codec> =>
|
||||
createHashMap(BTreeMap, value),
|
||||
|
||||
[TypeDefInfo.BTreeSet]: (registry: CodecRegistry, value: TypeDef): CodecClass<Codec> =>
|
||||
[TypeDefInfo.BTreeSet]: (registry: Registry, value: TypeDef): CodecClass<Codec> =>
|
||||
createWithSub(BTreeSet, value),
|
||||
|
||||
[TypeDefInfo.Compact]: (registry: CodecRegistry, value: TypeDef): CodecClass<Codec> =>
|
||||
[TypeDefInfo.Compact]: (registry: Registry, value: TypeDef): CodecClass<Codec> =>
|
||||
createWithSub(Compact, value),
|
||||
|
||||
[TypeDefInfo.DoNotConstruct]: (registry: CodecRegistry, value: TypeDef): CodecClass<Codec> =>
|
||||
[TypeDefInfo.DoNotConstruct]: (registry: Registry, value: TypeDef): CodecClass<Codec> =>
|
||||
DoNotConstruct.with(value.displayName || value.type),
|
||||
|
||||
[TypeDefInfo.Enum]: (registry: CodecRegistry, value: TypeDef): CodecClass<Codec> => {
|
||||
[TypeDefInfo.Enum]: (registry: Registry, value: TypeDef): CodecClass<Codec> => {
|
||||
const subs = getSubDefArray(value);
|
||||
|
||||
return Enum.with(
|
||||
@@ -90,14 +90,14 @@ const infoMapping: Record<TypeDefInfo, (registry: CodecRegistry, value: TypeDef)
|
||||
);
|
||||
},
|
||||
|
||||
[TypeDefInfo.HashMap]: (registry: CodecRegistry, value: TypeDef): CodecClass<Codec> =>
|
||||
[TypeDefInfo.HashMap]: (registry: Registry, value: TypeDef): CodecClass<Codec> =>
|
||||
createHashMap(HashMap, value),
|
||||
|
||||
[TypeDefInfo.Int]: (registry: CodecRegistry, value: TypeDef): CodecClass<Codec> =>
|
||||
[TypeDefInfo.Int]: (registry: Registry, value: TypeDef): CodecClass<Codec> =>
|
||||
createInt(Int, value),
|
||||
|
||||
// We have circular deps between Linkage & Struct
|
||||
[TypeDefInfo.Linkage]: (registry: CodecRegistry, value: TypeDef): CodecClass<Codec> => {
|
||||
[TypeDefInfo.Linkage]: (registry: Registry, value: TypeDef): CodecClass<Codec> => {
|
||||
const type = `Option<${getSubType(value)}>`;
|
||||
// eslint-disable-next-line sort-keys
|
||||
const Clazz = Struct.with({ previous: type, next: type } as any);
|
||||
@@ -112,26 +112,26 @@ const infoMapping: Record<TypeDefInfo, (registry: CodecRegistry, value: TypeDef)
|
||||
},
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
[TypeDefInfo.Null]: (registry: CodecRegistry, _: TypeDef): CodecClass<Codec> =>
|
||||
[TypeDefInfo.Null]: (registry: Registry, _: TypeDef): CodecClass<Codec> =>
|
||||
Null,
|
||||
|
||||
[TypeDefInfo.Option]: (registry: CodecRegistry, value: TypeDef): CodecClass<Codec> =>
|
||||
[TypeDefInfo.Option]: (registry: Registry, value: TypeDef): CodecClass<Codec> =>
|
||||
createWithSub(Option, value),
|
||||
|
||||
[TypeDefInfo.Plain]: (registry: CodecRegistry, value: TypeDef): CodecClass<Codec> =>
|
||||
[TypeDefInfo.Plain]: (registry: Registry, value: TypeDef): CodecClass<Codec> =>
|
||||
registry.getOrUnknown(value.type),
|
||||
|
||||
[TypeDefInfo.Range]: (registry: CodecRegistry, value: TypeDef): CodecClass<Codec> =>
|
||||
[TypeDefInfo.Range]: (registry: Registry, value: TypeDef): CodecClass<Codec> =>
|
||||
(value.type.includes('RangeInclusive') ? RangeInclusive : Range).with(getSubType(value)),
|
||||
|
||||
[TypeDefInfo.Result]: (registry: CodecRegistry, value: TypeDef): CodecClass<Codec> => {
|
||||
[TypeDefInfo.Result]: (registry: Registry, value: TypeDef): CodecClass<Codec> => {
|
||||
const [Ok, Err] = getTypeClassArray(value);
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
||||
return Result.with({ Err, Ok });
|
||||
},
|
||||
|
||||
[TypeDefInfo.Set]: (registry: CodecRegistry, value: TypeDef): CodecClass<Codec> =>
|
||||
[TypeDefInfo.Set]: (registry: Registry, value: TypeDef): CodecClass<Codec> =>
|
||||
CodecSet.with(
|
||||
getSubDefArray(value).reduce<Record<string, number>>((result, { index, name }) => {
|
||||
result[name as string] = index as number;
|
||||
@@ -141,19 +141,19 @@ const infoMapping: Record<TypeDefInfo, (registry: CodecRegistry, value: TypeDef)
|
||||
value.length
|
||||
),
|
||||
|
||||
[TypeDefInfo.Si]: (registry: CodecRegistry, value: TypeDef): CodecClass<Codec> =>
|
||||
getTypeClass(registry, (registry as CreateRegistry).lookup.getTypeDef(value.type)),
|
||||
[TypeDefInfo.Si]: (registry: Registry, value: TypeDef): CodecClass<Codec> =>
|
||||
getTypeClass(registry, registry.lookup.getTypeDef(value.type)),
|
||||
|
||||
[TypeDefInfo.Struct]: (registry: CodecRegistry, value: TypeDef): CodecClass<Codec> =>
|
||||
[TypeDefInfo.Struct]: (registry: Registry, value: TypeDef): CodecClass<Codec> =>
|
||||
Struct.with(getTypeClassMap(value), value.alias),
|
||||
|
||||
[TypeDefInfo.Tuple]: (registry: CodecRegistry, value: TypeDef): CodecClass<Codec> =>
|
||||
[TypeDefInfo.Tuple]: (registry: Registry, value: TypeDef): CodecClass<Codec> =>
|
||||
Tuple.with(getTypeClassArray(value)),
|
||||
|
||||
[TypeDefInfo.UInt]: (registry: CodecRegistry, value: TypeDef): CodecClass<Codec> =>
|
||||
[TypeDefInfo.UInt]: (registry: Registry, value: TypeDef): CodecClass<Codec> =>
|
||||
createInt(UInt, value),
|
||||
|
||||
[TypeDefInfo.Vec]: (registry: CodecRegistry, { sub }: TypeDef): CodecClass<Codec> => {
|
||||
[TypeDefInfo.Vec]: (registry: Registry, { sub }: TypeDef): CodecClass<Codec> => {
|
||||
assert(sub && !Array.isArray(sub), 'Expected type information for vector');
|
||||
|
||||
return (
|
||||
@@ -163,7 +163,7 @@ const infoMapping: Record<TypeDefInfo, (registry: CodecRegistry, value: TypeDef)
|
||||
);
|
||||
},
|
||||
|
||||
[TypeDefInfo.VecFixed]: (registry: CodecRegistry, { displayName, length, sub }: TypeDef): CodecClass<Codec> => {
|
||||
[TypeDefInfo.VecFixed]: (registry: Registry, { displayName, length, sub }: TypeDef): CodecClass<Codec> => {
|
||||
assert(sub && isNumber(length) && !Array.isArray(sub), 'Expected length & type information for fixed vector');
|
||||
|
||||
return (
|
||||
@@ -173,14 +173,14 @@ const infoMapping: Record<TypeDefInfo, (registry: CodecRegistry, value: TypeDef)
|
||||
);
|
||||
},
|
||||
|
||||
[TypeDefInfo.WrapperKeepOpaque]: (registry: CodecRegistry, value: TypeDef): CodecClass<Codec> =>
|
||||
[TypeDefInfo.WrapperKeepOpaque]: (registry: Registry, value: TypeDef): CodecClass<Codec> =>
|
||||
createWithSub(WrapperKeepOpaque, value),
|
||||
|
||||
[TypeDefInfo.WrapperOpaque]: (registry: CodecRegistry, value: TypeDef): CodecClass<Codec> =>
|
||||
[TypeDefInfo.WrapperOpaque]: (registry: Registry, value: TypeDef): CodecClass<Codec> =>
|
||||
createWithSub(WrapperOpaque, value)
|
||||
};
|
||||
|
||||
export function constructTypeClass<T extends Codec = Codec> (registry: CodecRegistry, typeDef: TypeDef): CodecClass<T> {
|
||||
export function constructTypeClass<T extends Codec = Codec> (registry: Registry, typeDef: TypeDef): CodecClass<T> {
|
||||
try {
|
||||
const Type = infoMapping[typeDef.info](registry, typeDef);
|
||||
|
||||
@@ -200,15 +200,15 @@ export function constructTypeClass<T extends Codec = Codec> (registry: CodecRegi
|
||||
}
|
||||
|
||||
// Returns the type Class for construction
|
||||
export function getTypeClass<T extends Codec = Codec> (registry: CodecRegistry, typeDef: TypeDef): CodecClass<T> {
|
||||
return (registry as CreateRegistry).getUnsafe(typeDef.type, false, typeDef) as CodecClass<T>;
|
||||
export function getTypeClass<T extends Codec = Codec> (registry: Registry, typeDef: TypeDef): CodecClass<T> {
|
||||
return registry.getUnsafe(typeDef.type, false, typeDef) as CodecClass<T>;
|
||||
}
|
||||
|
||||
export function createClassUnsafe<T extends Codec = Codec, K extends string = string> (registry: CodecRegistry, type: K): CodecClass<T> {
|
||||
export function createClassUnsafe<T extends Codec = Codec, K extends string = string> (registry: Registry, type: K): CodecClass<T> {
|
||||
return getTypeClass(
|
||||
registry,
|
||||
registry.isLookupType(type)
|
||||
? (registry as CreateRegistry).lookup.getTypeDef(type)
|
||||
? registry.lookup.getTypeDef(type)
|
||||
: getTypeDef(type)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// Copyright 2017-2022 @polkadot/types-create authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { Codec, CodecClass, CodecRegistry, IU8a } from '@polkadot/types-codec/types';
|
||||
import type { Codec, CodecClass, IU8a, Registry } from '@polkadot/types-codec/types';
|
||||
import type { CreateOptions } from '../types';
|
||||
|
||||
import { Bytes, Option } from '@polkadot/types-codec';
|
||||
@@ -20,7 +20,7 @@ function checkInstance (created: Codec, matcher: Uint8Array): void {
|
||||
(
|
||||
// on a length-prefixed type, just check the actual length
|
||||
['Bytes', 'Text', 'Type'].includes(rawType) &&
|
||||
matcher.length === (created as Bytes).length
|
||||
matcher.length === (created as unknown as Bytes).length
|
||||
) ||
|
||||
(
|
||||
// when the created is empty and matcher is also empty, let it slide...
|
||||
@@ -44,7 +44,7 @@ function checkPedantic (created: Codec, [value]: unknown[], isPedantic = false):
|
||||
|
||||
// Initializes a type with a value. This also checks for fallbacks and in the cases
|
||||
// where isPedantic is specified (storage decoding), also check the format/structure
|
||||
function initType<T extends Codec> (registry: CodecRegistry, Type: CodecClass, params: unknown[] = [], { blockHash, isOptional, isPedantic }: CreateOptions = {}): T {
|
||||
function initType<T extends Codec> (registry: Registry, Type: CodecClass, params: unknown[] = [], { blockHash, isOptional, isPedantic }: CreateOptions = {}): T {
|
||||
const created = new (isOptional ? Option.with(Type) : Type)(registry, ...params);
|
||||
|
||||
checkPedantic(created, params, isPedantic);
|
||||
@@ -59,7 +59,7 @@ function initType<T extends Codec> (registry: CodecRegistry, Type: CodecClass, p
|
||||
// An unsafe version of the `createType` below. It's unsafe because the `type`
|
||||
// argument here can be any string, which, when it cannot parse, will yield a
|
||||
// runtime error.
|
||||
export function createTypeUnsafe<T extends Codec = Codec, K extends string = string> (registry: CodecRegistry, type: K, params: unknown[] = [], options: CreateOptions = {}): T {
|
||||
export function createTypeUnsafe<T extends Codec = Codec, K extends string = string> (registry: Registry, type: K, params: unknown[] = [], options: CreateOptions = {}): T {
|
||||
let Clazz: CodecClass | null = null;
|
||||
let firstError: Error | null = null;
|
||||
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
// Copyright 2017-2022 @polkadot/types-create authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { Codec, CodecClass, ICompact, INumber } from '@polkadot/types-codec/types';
|
||||
import type { ILookup, TypeDef } from '@polkadot/types-create/types';
|
||||
|
||||
declare module '@polkadot/types-codec/types/registry' {
|
||||
export interface Registry {
|
||||
readonly lookup: ILookup;
|
||||
|
||||
createLookupType (lookupId: ICompact<INumber> | number): string;
|
||||
setLookup (lookup: ILookup): void;
|
||||
|
||||
getUnsafe <T extends Codec = Codec, K extends string = string> (name: K, withUnknown?: boolean, knownTypeDef?: TypeDef): CodecClass<T> | undefined;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
// Copyright 2017-2022 @polkadot/types-create authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import './augmentRegistry';
|
||||
|
||||
export type { CodecCreateOptions as CreateOptions } from '@polkadot/types-codec/types';
|
||||
|
||||
export * from './lookup';
|
||||
export * from './types';
|
||||
@@ -0,0 +1,14 @@
|
||||
// Copyright 2017-2022 @polkadot/types-create authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { ICompact, INumber } from '@polkadot/types-codec/types';
|
||||
import type { TypeDef } from './types';
|
||||
|
||||
export interface ILookup {
|
||||
getSiType (lookupId: ICompact<INumber> | string | number): {
|
||||
def: {
|
||||
asTuple: ICompact<INumber>[]
|
||||
}
|
||||
},
|
||||
getTypeDef (lookupId: ICompact<INumber> | string | number): TypeDef;
|
||||
}
|
||||
@@ -1,10 +1,6 @@
|
||||
// Copyright 2017-2022 @polkadot/types-create authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { Codec, CodecClass, CodecRegistry, ICompact, INumber } from '@polkadot/types-codec/types';
|
||||
|
||||
export type { CodecCreateOptions as CreateOptions } from '@polkadot/types-codec/types';
|
||||
|
||||
export enum TypeDefInfo {
|
||||
BTreeMap,
|
||||
BTreeSet,
|
||||
@@ -48,21 +44,3 @@ export interface TypeDef {
|
||||
type: string;
|
||||
typeName?: string;
|
||||
}
|
||||
|
||||
export interface ILookup {
|
||||
getSiType (lookupId: ICompact<INumber> | string | number): {
|
||||
def: {
|
||||
asTuple: ICompact<INumber>[]
|
||||
}
|
||||
},
|
||||
getTypeDef (lookupId: ICompact<INumber> | string | number): TypeDef;
|
||||
}
|
||||
|
||||
export interface CreateRegistry extends CodecRegistry {
|
||||
lookup: ILookup;
|
||||
|
||||
createLookupType (lookupId: ICompact<INumber> | number): string;
|
||||
setLookup (lookup: ILookup): void;
|
||||
|
||||
getUnsafe <T extends Codec = Codec, K extends string = string> (name: K, withUnknown?: boolean, knownTypeDef?: TypeDef): CodecClass<T> | undefined;
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
// Copyright 2017-2022 @polkadot/types-create authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { CodecRegistry } from '@polkadot/types-codec/types';
|
||||
import type { Registry } from '@polkadot/types-codec/types';
|
||||
import type { TypeDef } from '@polkadot/types-create/types';
|
||||
|
||||
import { assert, isNumber, isUndefined, objectSpread, stringify } from '@polkadot/util';
|
||||
@@ -20,7 +20,7 @@ export function paramsNotation <T> (outer: string, inner?: T | T[], transform: (
|
||||
}`;
|
||||
}
|
||||
|
||||
function encodeWithParams (registry: CodecRegistry, typeDef: TypeDef, outer: string): string {
|
||||
function encodeWithParams (registry: Registry, typeDef: TypeDef, outer: string): string {
|
||||
const { info, sub } = typeDef;
|
||||
|
||||
switch (info) {
|
||||
@@ -41,7 +41,7 @@ function encodeWithParams (registry: CodecRegistry, typeDef: TypeDef, outer: str
|
||||
throw new Error(`Unable to encode ${stringify(typeDef)} with params`);
|
||||
}
|
||||
|
||||
function encodeSubTypes (registry: CodecRegistry, sub: TypeDef[], asEnum?: boolean, extra?: Record<string, unknown>): string {
|
||||
function encodeSubTypes (registry: Registry, sub: TypeDef[], asEnum?: boolean, extra?: Record<string, unknown>): string {
|
||||
const names = sub.map(({ name }) => name);
|
||||
|
||||
assert(names.every((n) => !!n), () => `Subtypes does not have consistent names, ${names.join(', ')}`);
|
||||
@@ -63,16 +63,16 @@ function encodeSubTypes (registry: CodecRegistry, sub: TypeDef[], asEnum?: boole
|
||||
|
||||
// We setup a record here to ensure we have comprehensive coverage (any item not covered will result
|
||||
// in a compile-time error with the missing index)
|
||||
const encoders: Record<TypeDefInfo, (registry: CodecRegistry, typeDef: TypeDef) => string> = {
|
||||
[TypeDefInfo.BTreeMap]: (registry: CodecRegistry, typeDef: TypeDef) =>
|
||||
const encoders: Record<TypeDefInfo, (registry: Registry, typeDef: TypeDef) => string> = {
|
||||
[TypeDefInfo.BTreeMap]: (registry: Registry, typeDef: TypeDef) =>
|
||||
encodeWithParams(registry, typeDef, 'BTreeMap'),
|
||||
[TypeDefInfo.BTreeSet]: (registry: CodecRegistry, typeDef: TypeDef) =>
|
||||
[TypeDefInfo.BTreeSet]: (registry: Registry, typeDef: TypeDef) =>
|
||||
encodeWithParams(registry, typeDef, 'BTreeSet'),
|
||||
[TypeDefInfo.Compact]: (registry: CodecRegistry, typeDef: TypeDef) =>
|
||||
[TypeDefInfo.Compact]: (registry: Registry, typeDef: TypeDef) =>
|
||||
encodeWithParams(registry, typeDef, 'Compact'),
|
||||
[TypeDefInfo.DoNotConstruct]: (registry: CodecRegistry, { displayName, lookupIndex, lookupName }: TypeDef) =>
|
||||
[TypeDefInfo.DoNotConstruct]: (registry: Registry, { displayName, lookupIndex, lookupName }: TypeDef) =>
|
||||
`DoNotConstruct<${lookupName || displayName || (isUndefined(lookupIndex) ? 'Unknown' : registry.createLookupType(lookupIndex))}>`,
|
||||
[TypeDefInfo.Enum]: (registry: CodecRegistry, { sub }: TypeDef): string => {
|
||||
[TypeDefInfo.Enum]: (registry: Registry, { sub }: TypeDef): string => {
|
||||
assert(sub && Array.isArray(sub), 'Unable to encode Enum type');
|
||||
|
||||
// c-like enums have all Null entries
|
||||
@@ -81,26 +81,26 @@ const encoders: Record<TypeDefInfo, (registry: CodecRegistry, typeDef: TypeDef)
|
||||
? stringify({ _enum: sub.map(({ name }, index) => `${name || `Empty${index}`}`) })
|
||||
: encodeSubTypes(registry, sub, true);
|
||||
},
|
||||
[TypeDefInfo.HashMap]: (registry: CodecRegistry, typeDef: TypeDef) =>
|
||||
[TypeDefInfo.HashMap]: (registry: Registry, typeDef: TypeDef) =>
|
||||
encodeWithParams(registry, typeDef, 'HashMap'),
|
||||
[TypeDefInfo.Int]: (registry: CodecRegistry, { length = 32 }: TypeDef) =>
|
||||
[TypeDefInfo.Int]: (registry: Registry, { length = 32 }: TypeDef) =>
|
||||
`Int<${length}>`,
|
||||
[TypeDefInfo.Linkage]: (registry: CodecRegistry, typeDef: TypeDef) =>
|
||||
[TypeDefInfo.Linkage]: (registry: Registry, typeDef: TypeDef) =>
|
||||
encodeWithParams(registry, typeDef, 'Linkage'),
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
[TypeDefInfo.Null]: (registry: CodecRegistry, typeDef: TypeDef) =>
|
||||
[TypeDefInfo.Null]: (registry: Registry, typeDef: TypeDef) =>
|
||||
'Null',
|
||||
[TypeDefInfo.Option]: (registry: CodecRegistry, typeDef: TypeDef) =>
|
||||
[TypeDefInfo.Option]: (registry: Registry, typeDef: TypeDef) =>
|
||||
encodeWithParams(registry, typeDef, 'Option'),
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
[TypeDefInfo.Plain]: (registry: CodecRegistry, { displayName, type }: TypeDef) =>
|
||||
[TypeDefInfo.Plain]: (registry: Registry, { displayName, type }: TypeDef) =>
|
||||
displayName || type,
|
||||
[TypeDefInfo.Range]: (registry: CodecRegistry, typeDef: TypeDef) =>
|
||||
[TypeDefInfo.Range]: (registry: Registry, typeDef: TypeDef) =>
|
||||
encodeWithParams(registry, typeDef, typeDef.type.includes('RangeInclusive') ? 'RangeInclusive' : 'Range'),
|
||||
[TypeDefInfo.Result]: (registry: CodecRegistry, typeDef: TypeDef) =>
|
||||
[TypeDefInfo.Result]: (registry: Registry, typeDef: TypeDef) =>
|
||||
encodeWithParams(registry, typeDef, 'Result'),
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
[TypeDefInfo.Set]: (registry: CodecRegistry, { length = 8, sub }: TypeDef): string => {
|
||||
[TypeDefInfo.Set]: (registry: Registry, { length = 8, sub }: TypeDef): string => {
|
||||
assert(sub && Array.isArray(sub), 'Unable to encode Set type');
|
||||
|
||||
return stringify({
|
||||
@@ -110,9 +110,9 @@ const encoders: Record<TypeDefInfo, (registry: CodecRegistry, typeDef: TypeDef)
|
||||
});
|
||||
},
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
[TypeDefInfo.Si]: (registry: CodecRegistry, { lookupName, type }: TypeDef) =>
|
||||
[TypeDefInfo.Si]: (registry: Registry, { lookupName, type }: TypeDef) =>
|
||||
lookupName || type,
|
||||
[TypeDefInfo.Struct]: (registry: CodecRegistry, { alias, sub }: TypeDef): string => {
|
||||
[TypeDefInfo.Struct]: (registry: Registry, { alias, sub }: TypeDef): string => {
|
||||
assert(sub && Array.isArray(sub), 'Unable to encode Struct type');
|
||||
|
||||
return encodeSubTypes(registry, sub, false,
|
||||
@@ -125,33 +125,33 @@ const encoders: Record<TypeDefInfo, (registry: CodecRegistry, typeDef: TypeDef)
|
||||
: {}
|
||||
);
|
||||
},
|
||||
[TypeDefInfo.Tuple]: (registry: CodecRegistry, { sub }: TypeDef): string => {
|
||||
[TypeDefInfo.Tuple]: (registry: Registry, { sub }: TypeDef): string => {
|
||||
assert(sub && Array.isArray(sub), 'Unable to encode Tuple type');
|
||||
|
||||
return `(${sub.map((type) => encodeTypeDef(registry, type)).join(',')})`;
|
||||
},
|
||||
[TypeDefInfo.UInt]: (registry: CodecRegistry, { length = 32 }: TypeDef) =>
|
||||
[TypeDefInfo.UInt]: (registry: Registry, { length = 32 }: TypeDef) =>
|
||||
`UInt<${length}>`,
|
||||
[TypeDefInfo.Vec]: (registry: CodecRegistry, typeDef: TypeDef) =>
|
||||
[TypeDefInfo.Vec]: (registry: Registry, typeDef: TypeDef) =>
|
||||
encodeWithParams(registry, typeDef, 'Vec'),
|
||||
[TypeDefInfo.VecFixed]: (registry: CodecRegistry, { length, sub }: TypeDef): string => {
|
||||
[TypeDefInfo.VecFixed]: (registry: Registry, { length, sub }: TypeDef): string => {
|
||||
assert(isNumber(length) && !isUndefined(sub) && !Array.isArray(sub), 'Unable to encode VecFixed type');
|
||||
|
||||
return `[${sub.type};${length}]`;
|
||||
},
|
||||
[TypeDefInfo.WrapperKeepOpaque]: (registry: CodecRegistry, typeDef: TypeDef) =>
|
||||
[TypeDefInfo.WrapperKeepOpaque]: (registry: Registry, typeDef: TypeDef) =>
|
||||
encodeWithParams(registry, typeDef, 'WrapperKeepOpaque'),
|
||||
[TypeDefInfo.WrapperOpaque]: (registry: CodecRegistry, typeDef: TypeDef) =>
|
||||
[TypeDefInfo.WrapperOpaque]: (registry: Registry, typeDef: TypeDef) =>
|
||||
encodeWithParams(registry, typeDef, 'WrapperOpaque')
|
||||
};
|
||||
|
||||
function encodeType (registry: CodecRegistry, typeDef: TypeDef, withLookup = true): string {
|
||||
function encodeType (registry: Registry, typeDef: TypeDef, withLookup = true): string {
|
||||
return withLookup && typeDef.lookupName
|
||||
? typeDef.lookupName
|
||||
: encoders[typeDef.info](registry, typeDef);
|
||||
}
|
||||
|
||||
export function encodeTypeDef (registry: CodecRegistry, typeDef: TypeDef): string {
|
||||
export function encodeTypeDef (registry: Registry, typeDef: TypeDef): string {
|
||||
// In the case of contracts we do have the unfortunate situation where the displayName would
|
||||
// refer to "Option" when it is an option. For these, string it out, only using when actually
|
||||
// not a top-level element to be used
|
||||
@@ -160,7 +160,7 @@ export function encodeTypeDef (registry: CodecRegistry, typeDef: TypeDef): strin
|
||||
: encodeType(registry, typeDef);
|
||||
}
|
||||
|
||||
export function withTypeString (registry: CodecRegistry, typeDef: Omit<TypeDef, 'type'> & { type?: string }): TypeDef {
|
||||
export function withTypeString (registry: Registry, typeDef: Omit<TypeDef, 'type'> & { type?: string }): TypeDef {
|
||||
return objectSpread({}, typeDef, {
|
||||
type: encodeType(registry, typeDef as TypeDef, false)
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user