Allow construction of HashMap (#1955)
* Allow construction of HashMap * WIP... add Map to codec, extend BTreeMap & HashMap from it * Rework various map tests * registry.createClass * createClass in tests
This commit is contained in:
@@ -38,6 +38,7 @@ function tsExport (definitions: object, def: TypeDef, imports: TypeImports): str
|
||||
const tsBTreeMap = tsExport;
|
||||
const tsBTreeSet = tsExport;
|
||||
const tsCompact = tsExport;
|
||||
const tsHashMap = tsExport;
|
||||
const tsOption = tsExport;
|
||||
const tsPlain = tsExport;
|
||||
const tsTuple = tsExport;
|
||||
@@ -156,6 +157,7 @@ function generateInterfaces (definitions: object, { types }: { types: Record<str
|
||||
[TypeDefInfo.BTreeSet]: tsBTreeSet,
|
||||
[TypeDefInfo.Compact]: tsCompact,
|
||||
[TypeDefInfo.Enum]: tsEnum,
|
||||
[TypeDefInfo.HashMap]: tsHashMap,
|
||||
[TypeDefInfo.Int]: tsInt,
|
||||
[TypeDefInfo.Linkage]: errorUnhandled,
|
||||
[TypeDefInfo.Null]: errorUnhandled,
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
import { TypeDef, TypeDefInfo } from '@polkadot/types/create/types';
|
||||
import { Constructor, Registry } from '@polkadot/types/types';
|
||||
|
||||
import { ClassOf, ClassOfUnsafe, getTypeDef } from '@polkadot/types/create';
|
||||
import { ClassOfUnsafe, getTypeDef } from '@polkadot/types/create';
|
||||
import AbstractInt from '@polkadot/types/codec/AbstractInt';
|
||||
import Compact from '@polkadot/types/codec/Compact';
|
||||
import Enum from '@polkadot/types/codec/Enum';
|
||||
@@ -118,7 +118,7 @@ export function getSimilarTypes (definitions: object, registry: Registry, _type:
|
||||
possibleTypes.push('Address', 'AccountId', 'AccountIndex', 'string', 'Uint8Array');
|
||||
} else if (isChildClass(GenericAccountId, Clazz)) {
|
||||
possibleTypes.push('string', 'Uint8Array');
|
||||
} else if (isChildClass(ClassOf(registry, 'bool'), Clazz)) {
|
||||
} else if (isChildClass(registry.createClass('bool'), Clazz)) {
|
||||
possibleTypes.push('boolean', 'Uint8Array');
|
||||
} else if (isChildClass(Null, Clazz)) {
|
||||
possibleTypes.push('null');
|
||||
|
||||
@@ -97,6 +97,14 @@ function formatCompact (inner: string): string {
|
||||
return paramsNotation('Compact', inner);
|
||||
}
|
||||
|
||||
/**
|
||||
* Given the inner `K` & `V`, return a `BTreeMap<K, V>` string
|
||||
*/
|
||||
/** @internal */
|
||||
function formatHashMap (key: string, val: string): string {
|
||||
return `HashMap<${key}, ${val}>`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Given the inner `O` & `E`, return a `Result<O, E>` string
|
||||
*/
|
||||
@@ -150,13 +158,16 @@ export function formatType (definitions: object, type: string | TypeDef, imports
|
||||
|
||||
setImports(definitions, imports, [typeDef.type]);
|
||||
|
||||
// FIXME Swap to Record<TypeDefInfo, fn> to check all types
|
||||
switch (typeDef.info) {
|
||||
case TypeDefInfo.Compact: {
|
||||
setImports(definitions, imports, ['Compact']);
|
||||
|
||||
return formatCompact(formatType(definitions, (typeDef.sub as TypeDef).type, imports));
|
||||
}
|
||||
case TypeDefInfo.Option: {
|
||||
setImports(definitions, imports, ['Option']);
|
||||
|
||||
return formatOption(formatType(definitions, (typeDef.sub as TypeDef).type, imports));
|
||||
}
|
||||
case TypeDefInfo.Plain: {
|
||||
@@ -164,6 +175,7 @@ export function formatType (definitions: object, type: string | TypeDef, imports
|
||||
}
|
||||
case TypeDefInfo.Vec: {
|
||||
setImports(definitions, imports, ['Vec']);
|
||||
|
||||
return formatVec(formatType(definitions, (typeDef.sub as TypeDef).type, imports));
|
||||
}
|
||||
case TypeDefInfo.Tuple: {
|
||||
@@ -190,21 +202,34 @@ export function formatType (definitions: object, type: string | TypeDef, imports
|
||||
}
|
||||
case TypeDefInfo.BTreeMap: {
|
||||
setImports(definitions, imports, ['BTreeMap']);
|
||||
|
||||
const [keyDef, valDef] = (typeDef.sub as TypeDef[]);
|
||||
|
||||
return formatBTreeMap(formatType(definitions, keyDef.type, imports), formatType(definitions, valDef.type, imports));
|
||||
}
|
||||
case TypeDefInfo.BTreeSet: {
|
||||
setImports(definitions, imports, ['BTreeSet']);
|
||||
|
||||
const valDef = typeDef.sub as TypeDef;
|
||||
|
||||
return formatBTreeSet(formatType(definitions, valDef.type, imports));
|
||||
}
|
||||
case TypeDefInfo.HashMap: {
|
||||
setImports(definitions, imports, ['HashMap']);
|
||||
|
||||
const [keyDef, valDef] = (typeDef.sub as TypeDef[]);
|
||||
|
||||
return formatHashMap(formatType(definitions, keyDef.type, imports), formatType(definitions, valDef.type, imports));
|
||||
}
|
||||
case TypeDefInfo.Result: {
|
||||
setImports(definitions, imports, ['Result']);
|
||||
|
||||
const [okDef, errorDef] = (typeDef.sub as TypeDef[]);
|
||||
|
||||
return formatResult(formatType(definitions, okDef.type, imports), formatType(definitions, errorDef.type, imports));
|
||||
}
|
||||
default: {
|
||||
throw new Error(`Cannot format ${type}.`);
|
||||
throw new Error(`Cannot format ${JSON.stringify(type)}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user