Add dynamic Vec ABI support (#1499)

This commit is contained in:
kwingram25
2019-10-20 11:57:19 +02:00
committed by Jaco Greeff
parent b5edcb8529
commit b911bdbd2d
2 changed files with 45 additions and 27 deletions
+32 -19
View File
@@ -2,7 +2,7 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { MetaRegistryItem, MetaRegistryJson, MetaTypeDefClikeEnum, MetaType, MetaTypeDefEnum, MetaTypeDefEnumVariant, MetaTypeDefEnumVariantStruct, MetaTypeDefEnumVariantTupleStruct, MetaTypeDefEnumVariantUnit, MetaTypeDefStruct, MetaTypeDefStructField, MetaTypeDefTupleStruct, MetaTypeDefUnion, MetaTypeIdArray, MetaTypeIdCustom, MetaTypeInfo, StringIndex, TypeDef, TypeDefInfo, TypeIndex } from '@polkadot/types/types';
import { MetaRegistryItem, MetaRegistryJson, MetaTypeDefClikeEnum, MetaType, MetaTypeDefEnum, MetaTypeDefEnumVariant, MetaTypeDefEnumVariantStruct, MetaTypeDefEnumVariantTupleStruct, MetaTypeDefEnumVariantUnit, MetaTypeDefStruct, MetaTypeDefStructField, MetaTypeDefTupleStruct, MetaTypeDefUnion, MetaTypeIdCustom, MetaTypeIdVec, MetaTypeIdVecFixed, MetaTypeInfo, StringIndex, TypeDef, TypeDefInfo, TypeIndex } from '@polkadot/types/types';
import { assert } from '@polkadot/util';
import { displayType, withTypeString } from '@polkadot/types';
@@ -10,7 +10,8 @@ import { displayType, withTypeString } from '@polkadot/types';
const {
BuiltinPlain,
BuiltinTuple,
BuiltinArray,
BuiltinVec,
BuiltinVecFixed,
ClikeEnum,
Enum,
Struct,
@@ -36,8 +37,11 @@ export default class MetaRegistry {
if (Array.isArray(id)) {
metaTypeInfo = MetaTypeInfo.BuiltinTuple;
}
if ((id as MetaTypeIdArray)['array.type']) {
metaTypeInfo = MetaTypeInfo.BuiltinArray;
if ((id as MetaTypeIdVec)['slice.type']) {
metaTypeInfo = MetaTypeInfo.BuiltinVec;
}
if ((id as MetaTypeIdVecFixed)['array.type']) {
metaTypeInfo = MetaTypeInfo.BuiltinVecFixed;
}
}
@@ -164,8 +168,11 @@ export default class MetaRegistry {
case BuiltinTuple:
typeDef = this.typeDefForBuiltinTuple(metaType.id as TypeIndex[]);
break;
case BuiltinArray:
typeDef = this.typeDefForBuiltinArray(metaType.id as MetaTypeIdArray, typeIndex);
case BuiltinVec:
typeDef = this.typeDefForBuiltinVec(metaType.id as MetaTypeIdVec, typeIndex);
break;
case BuiltinVecFixed:
typeDef = this.typeDefForBuiltinVecFixed(metaType.id as MetaTypeIdVecFixed, typeIndex);
break;
case Enum:
typeDef = this.typeDefForEnum(metaType.def as MetaTypeDefEnum, metaType.id as MetaTypeIdCustom, typeIndex);
@@ -251,7 +258,22 @@ export default class MetaRegistry {
};
}
private typeDefForBuiltinArray (id: MetaTypeIdArray, typeIndex?: TypeIndex): Pick<TypeDef, never> {
private typeDefForBuiltinVec (id: MetaTypeIdVec, typeIndex?: TypeIndex): Pick<TypeDef, never> {
const { 'slice.type': vecTypeIndex } = id;
assert(!typeIndex || vecTypeIndex !== typeIndex, `MetaRegistry: self-referencing registry type at index ${typeIndex}`);
const type = displayType(this.typeDefFromMetaTypeAt(vecTypeIndex));
assert(type && type.length > 0, `MetaRegistry: Invalid builtin Vec type found at index ${typeIndex}`);
return {
info: TypeDefInfo.Vec,
type: `Vec<${type}>`,
sub: this.typeDefFromMetaTypeAt(vecTypeIndex)
};
}
private typeDefForBuiltinVecFixed (id: MetaTypeIdVecFixed, typeIndex?: TypeIndex): Pick<TypeDef, never> {
const { 'array.type': vecTypeIndex, 'array.len': vecLength } = id;
assert(!vecLength || vecLength <= 256, 'MetaRegistry: Only support for [Type; <length>], where length <= 256');
@@ -261,18 +283,9 @@ export default class MetaRegistry {
assert(type && type.length > 0, `MetaRegistry: Invalid vector type found at index ${typeIndex}`);
return {
...(
vecLength
? {
info: TypeDefInfo.VecFixed,
ext: { length: vecLength, type },
type: `[${type};${vecLength}]`
}
: {
info: TypeDefInfo.Vec,
type: `Vec<${type}>`
}
),
info: TypeDefInfo.VecFixed,
ext: { length: vecLength, type },
type: `[${type};${vecLength}]`,
sub: this.typeDefFromMetaTypeAt(vecTypeIndex)
};
}
+13 -8
View File
@@ -57,30 +57,35 @@ export type StringIndex = number;
export enum MetaTypeInfo {
BuiltinPlain,
BuiltinTuple,
BuiltinArray,
BuiltinVec,
BuiltinVecFixed,
Enum,
ClikeEnum,
Struct,
TupleStruct,
Null
Null,
}
export type MetaTypeIdPrimitive = string;
export type MetaTypeIdTuple = TypeIndex[];
export interface MetaTypeIdArray {
'array.len': number;
'array.type': TypeIndex;
}
export interface MetaTypeIdCustom {
'custom.name': StringIndex;
'custom.namespace'?: StringIndex[];
'custom.params'?: TypeIndex[];
}
export type MetaTypeId = MetaTypeIdPrimitive | MetaTypeIdTuple | MetaTypeIdArray | MetaTypeIdCustom;
export interface MetaTypeIdVec {
'slice.type': TypeIndex;
}
export interface MetaTypeIdVecFixed {
'array.len': number;
'array.type': TypeIndex;
}
export type MetaTypeId = MetaTypeIdPrimitive | MetaTypeIdTuple | MetaTypeIdVec | MetaTypeIdVecFixed | MetaTypeIdCustom;
// MetaTypeIdPrimitive | MetaTypeIdTuple | MetaTypeIdArray | MetaTypeIdCustom;
// export type MetaTypeId = Record<string, any>;