Exhaustive TypeDefInfo handling in extractType (#4506)

This commit is contained in:
Jaco
2022-01-26 18:06:18 +02:00
committed by GitHub
parent 33c4d82293
commit 697e6d8b8b
5 changed files with 107 additions and 96 deletions
+1 -1
View File
@@ -33,7 +33,7 @@
"@polkadot/x-global": "^8.3.3",
"@polkadot/x-ws": "^8.3.3",
"eventemitter3": "^4.0.7",
"mock-socket": "^9.1.0",
"mock-socket": "^9.1.2",
"nock": "^13.2.2"
}
}
@@ -7,43 +7,53 @@ import { getTypeDef, TypeDefInfo } from '@polkadot/types-create';
type Extracted = string | Extracted[];
function extractSubSingle ({ lookupName, type }: TypeDef): Extracted[] {
function extractSubSingle (_: string, { sub }: TypeDef): Extracted[] {
const { lookupName, type } = sub as TypeDef;
return extractTypes([lookupName || type]);
}
function extractSubArray (types: TypeDef[]): Extracted[] {
return extractTypes(types.map(({ lookupName, type }) => lookupName || type));
function extractSubArray (_: string, { sub }: TypeDef): Extracted[] {
return extractTypes((sub as TypeDef[]).map(({ lookupName, type }) => lookupName || type));
}
function unhandled (type: string, { info }: TypeDef): never {
throw new Error(`Unhandled: Unable to create and validate type from ${type} (info=${TypeDefInfo[info]})`);
}
// we only handle the types with params here
const mapping: Record<TypeDefInfo, (type: string, typeDef: TypeDef) => Extracted> = {
[TypeDefInfo.BTreeMap]: extractSubArray,
[TypeDefInfo.BTreeSet]: extractSubSingle,
[TypeDefInfo.Compact]: extractSubSingle,
[TypeDefInfo.DoNotConstruct]: unhandled,
[TypeDefInfo.Enum]: extractSubArray,
[TypeDefInfo.HashMap]: extractSubArray,
[TypeDefInfo.Int]: unhandled,
[TypeDefInfo.Linkage]: unhandled,
[TypeDefInfo.Null]: unhandled,
[TypeDefInfo.Option]: extractSubSingle,
[TypeDefInfo.Plain]: (_: string, typeDef: TypeDef) => typeDef.lookupName || typeDef.type,
[TypeDefInfo.Range]: extractSubSingle,
[TypeDefInfo.RangeInclusive]: extractSubSingle,
[TypeDefInfo.Result]: extractSubArray,
[TypeDefInfo.Set]: extractSubArray,
[TypeDefInfo.Si]: unhandled,
[TypeDefInfo.Struct]: extractSubArray,
[TypeDefInfo.Tuple]: extractSubArray,
[TypeDefInfo.UInt]: unhandled,
[TypeDefInfo.Vec]: extractSubSingle,
[TypeDefInfo.VecFixed]: extractSubSingle,
[TypeDefInfo.WrapperKeepOpaque]: extractSubSingle,
[TypeDefInfo.WrapperOpaque]: extractSubSingle
};
/** @internal */
export function extractTypes (types: string[]): Extracted[] {
return types.map((type): Extracted => {
const decoded = getTypeDef(type);
const typeDef = getTypeDef(type);
switch (decoded.info) {
case TypeDefInfo.Plain:
return decoded.lookupName || decoded.type;
case TypeDefInfo.BTreeSet:
case TypeDefInfo.Compact:
case TypeDefInfo.Option:
case TypeDefInfo.Vec:
case TypeDefInfo.VecFixed:
case TypeDefInfo.WrapperKeepOpaque:
case TypeDefInfo.WrapperOpaque:
return extractSubSingle(decoded.sub as TypeDef);
case TypeDefInfo.BTreeMap:
case TypeDefInfo.Enum:
case TypeDefInfo.HashMap:
case TypeDefInfo.Result:
case TypeDefInfo.Set:
case TypeDefInfo.Struct:
case TypeDefInfo.Tuple:
return extractSubArray(decoded.sub as TypeDef[]);
default:
throw new Error(`Unhandled: Unable to create and validate type from ${type} (info=${TypeDefInfo[decoded.info]})`);
}
return mapping[typeDef.info](type, typeDef);
});
}