Add TypeDefInfo handling for BTreeMap (#1481)
* Add TypeDefInfo handling for BTreeMap * Update getTypeClass.ts * Fixup BTreeMap result (will be active once class support is merged) * Merge master, update with BTreeMap classes
This commit is contained in:
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -20,6 +20,12 @@ describe('createType', (): void => {
|
||||
});
|
||||
});
|
||||
|
||||
it('allows creation of a BTreeMap', (): void => {
|
||||
expect(
|
||||
createTypeUnsafe('BTreeMap<Text,u32>', ['0x041c62617a7a696e6745000000']).toString()
|
||||
).toEqual('{"bazzing":69}');
|
||||
});
|
||||
|
||||
it('allows creation of a Result', (): void => {
|
||||
expect(
|
||||
createTypeUnsafe('Result<u32,Text>', ['0x011064656667']).toJSON()
|
||||
|
||||
@@ -7,6 +7,7 @@ import { TypeDef, TypeDefExtVecFixed, TypeDefInfo } from './types';
|
||||
|
||||
import { assert } from '@polkadot/util';
|
||||
|
||||
import BTreeMap from '../BTreeMap';
|
||||
import Compact from '../Compact';
|
||||
import Enum from '../Enum';
|
||||
import Linkage from '../Linkage';
|
||||
@@ -56,6 +57,13 @@ function getTypeClassArray (value: TypeDef): (InterfaceTypes)[] {
|
||||
}
|
||||
|
||||
const infoMapping: Record<TypeDefInfo, (value: TypeDef) => Constructor> = {
|
||||
[TypeDefInfo.BTreeMap]: (value: TypeDef): Constructor => {
|
||||
const [keyDef, valueDef] = getSubDefArray(value);
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
||||
return BTreeMap.with(getTypeClass(keyDef), getTypeClass(valueDef));
|
||||
},
|
||||
|
||||
[TypeDefInfo.Compact]: (value: TypeDef): Constructor => Compact.with(getSubType(value)),
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
||||
|
||||
@@ -30,6 +30,25 @@ describe('getTypeDef', (): void => {
|
||||
});
|
||||
});
|
||||
|
||||
it('properly decodes a BTreeMap<u32, Text>', (): void => {
|
||||
expect(
|
||||
getTypeDef('BTreeMap<u32, Text>')
|
||||
).toEqual({
|
||||
info: TypeDefInfo.BTreeMap,
|
||||
type: 'BTreeMap<u32,Text>',
|
||||
sub: [
|
||||
{
|
||||
info: TypeDefInfo.Plain,
|
||||
type: 'u32'
|
||||
},
|
||||
{
|
||||
info: TypeDefInfo.Plain,
|
||||
type: 'Text'
|
||||
}
|
||||
]
|
||||
});
|
||||
});
|
||||
|
||||
it('properly decodes a Result<u32, Text>', (): void => {
|
||||
expect(
|
||||
getTypeDef('Result<u32, Text>')
|
||||
|
||||
@@ -102,7 +102,8 @@ const nestedExtraction: [string, string, TypeDefInfo, (value: TypeDef, type: str
|
||||
['[', ']', TypeDefInfo.VecFixed, _decodeFixedVec],
|
||||
['{', '}', TypeDefInfo.Struct, _decodeStruct],
|
||||
['(', ')', TypeDefInfo.Tuple, _decodeTuple],
|
||||
// the inner for result is the same as tuple
|
||||
// the inner for these are the same as tuple, multiple values
|
||||
['BTreeMap<', '>', TypeDefInfo.BTreeMap, _decodeTuple],
|
||||
['Result<', '>', TypeDefInfo.Result, _decodeTuple]
|
||||
];
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
|
||||
type Mapper = (value: string) => string;
|
||||
|
||||
const ALLOWED_BOXES = ['Compact', 'DoubleMap', 'Linkage', 'Result', 'Option', 'Vec'];
|
||||
const ALLOWED_BOXES = ['BTreeMap', 'Compact', 'DoubleMap', 'Linkage', 'Result', 'Option', 'Vec'];
|
||||
|
||||
const mappings: Mapper[] = [
|
||||
// alias <T::InherentOfflineReport as InherentOfflineReport>::Inherent -> InherentOfflineReport
|
||||
|
||||
@@ -12,6 +12,7 @@ export type FromReg<T extends Codec, K extends string> = K extends InterfaceType
|
||||
: T;
|
||||
|
||||
export enum TypeDefInfo {
|
||||
BTreeMap,
|
||||
Compact,
|
||||
DoubleMap,
|
||||
Enum,
|
||||
|
||||
@@ -9,7 +9,7 @@ import { isString, stringCamelCase, stringUpperFirst } from '@polkadot/util';
|
||||
|
||||
import { getTypeDef } from '../../codec/create';
|
||||
import * as definitions from '../../interfaces/definitions';
|
||||
import { createImportCode, createImports, exportInterface, exportType, formatCompact, formatOption, formatResult, formatTuple, formatVec, FOOTER, HEADER, setImports, TypeImports } from '../util';
|
||||
import { createImportCode, createImports, exportInterface, exportType, formatBTreeMap, formatCompact, formatOption, formatResult, formatTuple, formatVec, FOOTER, HEADER, setImports, TypeImports } from '../util';
|
||||
|
||||
interface Imports extends TypeImports {
|
||||
interfaces: [string, string][];
|
||||
@@ -26,6 +26,14 @@ function errorUnhandled (def: TypeDef, imports: TypeImports): string {
|
||||
throw new Error(`Generate: ${name}: Unhandled type ${TypeDefInfo[def.info]}`);
|
||||
}
|
||||
|
||||
function tsBTreeMap ({ name: resultName, sub, type }: TypeDef, imports: TypeImports): string {
|
||||
const [keyDef, valDef] = (sub as TypeDef[]);
|
||||
|
||||
setImports(imports, [type]);
|
||||
|
||||
return exportInterface(resultName, formatBTreeMap(keyDef.type, valDef.type));
|
||||
}
|
||||
|
||||
function tsCompact ({ name: compactName, sub }: TypeDef, imports: TypeImports): string {
|
||||
const def = (sub as TypeDef);
|
||||
|
||||
@@ -218,6 +226,7 @@ function generateInterfaces ({ types }: { types: Record<string, any> }, imports:
|
||||
// `generators[typedef.info](...)` TS will show any unhandled types. Rather
|
||||
// we are being explicit in having no handlers where we do not support (yet)
|
||||
const generators = {
|
||||
[TypeDefInfo.BTreeMap]: tsBTreeMap,
|
||||
[TypeDefInfo.Compact]: tsCompact,
|
||||
[TypeDefInfo.DoubleMap]: errorUnhandled,
|
||||
[TypeDefInfo.Enum]: tsEnum,
|
||||
|
||||
@@ -34,6 +34,13 @@ export function exportType (name = '', base: string): string {
|
||||
return exportInterface(name, base);
|
||||
}
|
||||
|
||||
/**
|
||||
* Given the inner `K` & `V`, return a `BTreeMap<K, V>` string
|
||||
*/
|
||||
export function formatBTreeMap (key: string, val: string): string {
|
||||
return `BTreeMap<${key}, ${val}>`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Given the inner `T`, return a `Compact<T>` string
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user