Remove custom toString() from metadata storage (#1597)

* Convert asType -> asPlain, isPlainType -> isPlain

* Make tests pass

* Move unwrapStorageType to StorageKey
This commit is contained in:
Jaco Greeff
2019-11-22 10:47:21 +01:00
committed by GitHub
parent ea2c320f33
commit 3b758a0d64
27 changed files with 1142 additions and 3585 deletions
@@ -70,7 +70,7 @@ export default {
},
StorageFunctionTypeV0: {
_enum: {
Type: 'PlainTypeV0',
Plain: 'PlainTypeV0',
Map: 'MapTypeV0'
}
},
@@ -417,10 +417,10 @@ export interface StorageFunctionModifierV5 extends StorageFunctionModifierV4 {}
/** Enum */
export interface StorageFunctionTypeV0 extends Enum {
/** 0:: Type(PlainTypeV0) */
readonly isType: boolean;
/** 0:: Plain(PlainTypeV0) */
readonly isPlain: boolean;
/** PlainTypeV0 */
readonly asType: PlainTypeV0;
readonly asPlain: PlainTypeV0;
/** 1:: Map(MapTypeV0) */
readonly isMap: boolean;
/** MapTypeV0 */
+38 -3
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 { AnyU8a, Registry } from '../types';
import { AnyU8a, Codec, Registry } from '../types';
import { StorageEntryMetadata as MetaV8 } from '@polkadot/metadata/Metadata/v8/Storage';
import { assert, isFunction, isString, isU8a } from '@polkadot/util';
@@ -30,6 +30,41 @@ interface StorageKeyExtra {
section: string;
}
// eslint-disable-next-line @typescript-eslint/ban-types
type Text = String & Codec;
interface StorageType {
asDoubleMap: {
value: Text;
};
asMap: {
linked: { isTrue: boolean };
key: Text;
value: Text;
};
asPlain: Text;
isDoubleMap: boolean;
isMap: boolean;
isPlain: boolean;
}
// we unwrap the type here, turning into an output usable for createType
export function unwrapStorageType (type: StorageType): string {
if (type.isDoubleMap) {
return `DoubleMap<${type.asDoubleMap.value.toString()}>`;
}
if (type.isMap) {
if (type.asMap.linked.isTrue) {
return `(${type.asMap.value.toString()}, Linkage<${type.asMap.key.toString()}>)`;
}
return type.asMap.value.toString();
}
return type.asPlain.toString();
}
/**
* @name StorageKey
* @description
@@ -105,11 +140,11 @@ export default class StorageKey extends Bytes {
if (value instanceof StorageKey) {
return value.outputType;
} else if (isFunction(value)) {
return value.meta.type.toString();
return unwrapStorageType(value.meta.type);
} else if (Array.isArray(value)) {
const [fn] = value;
return fn.meta.type.toString();
return unwrapStorageType(fn.meta.type);
}
return undefined;
@@ -16,10 +16,10 @@ import { TypeRegistry } from '@polkadot/types/codec';
// From a storage entry metadata, we return [args, returnType]
function entrySignature (registry: Registry, storageEntry: StorageEntryMetadata, imports: TypeImports): [string, string] {
if (storageEntry.type.isPlainType) {
setImports(imports, [storageEntry.type.asType.toString()]);
if (storageEntry.type.isPlain) {
setImports(imports, [storageEntry.type.asPlain.toString()]);
return ['', formatType(storageEntry.type.asType.toString(), imports)];
return ['', formatType(storageEntry.type.asPlain.toString(), imports)];
} else if (storageEntry.type.isMap) {
// Find similar types of the `key` type
const similarTypes = getSimilarTypes(registry, storageEntry.type.asMap.key.toString(), imports);