From 04f6d352d94e6614483390568bd400d697deeef1 Mon Sep 17 00:00:00 2001 From: Alex Wang Date: Tue, 16 Apr 2019 02:53:30 +0000 Subject: [PATCH] feat: adding double map support Merged in double-map (pull request #1) Approved-by: Ian He --- packages/types/src/Metadata/v0/Modules.ts | 39 ++++++++ packages/types/src/Metadata/v1/Storage.ts | 24 ++++- packages/types/src/Metadata/v2/Storage.ts | 24 ++++- packages/types/src/Metadata/v3/Metadata.ts | 14 +-- packages/types/src/Metadata/v3/Storage.ts | 100 +-------------------- 5 files changed, 93 insertions(+), 108 deletions(-) diff --git a/packages/types/src/Metadata/v0/Modules.ts b/packages/types/src/Metadata/v0/Modules.ts index eb72da7a67..c1b95c0402 100644 --- a/packages/types/src/Metadata/v0/Modules.ts +++ b/packages/types/src/Metadata/v0/Modules.ts @@ -135,6 +135,45 @@ export class StorageFunctionModifier extends Enum { } } +export class DoubleMapType extends Struct { + constructor (value?: any) { + super({ + key1: Text, + key2: Text, + value: Text, + keyHasher: Text + }, value); + } + + /** + * @description The mapped key as [[Text]] + */ + get key1 (): Text { + return this.get('key1') as Text; + } + + /** + * @description The mapped key as [[Text]] + */ + get key2 (): Text { + return this.get('key2') as Text; + } + + /** + * @description The mapped key as [[Text]] + */ + get keyHasher (): Text { + return this.get('keyHasher') as Text; + } + + /** + * @description The mapped key as [[Text]] + */ + get value (): Text { + return this.get('value') as Text; + } +} + export class MapType extends Struct { private _isLinked = false; diff --git a/packages/types/src/Metadata/v1/Storage.ts b/packages/types/src/Metadata/v1/Storage.ts index 99958b3891..73071f8347 100644 --- a/packages/types/src/Metadata/v1/Storage.ts +++ b/packages/types/src/Metadata/v1/Storage.ts @@ -9,6 +9,7 @@ import Bytes from '../../primitive/Bytes'; import Null from '../../primitive/Null'; import Text from '../../primitive/Text'; import Type from '../../primitive/Type'; +import { DoubleMapType } from '../v0/Modules'; export class Default extends Null {} @@ -73,14 +74,22 @@ export class MapType extends Struct { export class PlainType extends Type { } -export class MetadataStorageType extends EnumType { +export class MetadataStorageType extends EnumType { constructor (value?: any, index?: number) { super({ PlainType, - MapType + MapType, + DoubleMapType }, value, index); } + /** + * @description `true` if the storage entry is a doublemap + */ + get isDoubleMap (): boolean { + return this.toNumber() === 2; + } + /** * @description `true` if the storage entry is a map */ @@ -88,6 +97,13 @@ export class MetadataStorageType extends EnumType { return this.toNumber() === 1; } + /** + * @description The value as a mapped value + */ + get asDoubleMap (): DoubleMapType { + return this.value as DoubleMapType; + } + /** * @description The value as a mapped value */ @@ -106,6 +122,10 @@ export class MetadataStorageType extends EnumType { * @description Returns the string representation of the value */ toString (): string { + if (this.isDoubleMap) { + return this.asDoubleMap.value.toString(); + } + return this.isMap ? this.asMap.value.toString() : this.asType.toString(); diff --git a/packages/types/src/Metadata/v2/Storage.ts b/packages/types/src/Metadata/v2/Storage.ts index e8f486e711..d1db771db3 100644 --- a/packages/types/src/Metadata/v2/Storage.ts +++ b/packages/types/src/Metadata/v2/Storage.ts @@ -11,6 +11,7 @@ import Null from '../../primitive/Null'; import Text from '../../primitive/Text'; import Type from '../../primitive/Type'; import { MetadataStorageModifier } from '../v1/Storage'; +import { DoubleMapType } from '../v0/Modules'; export class Default extends Null {} @@ -50,14 +51,22 @@ export class MapType extends Struct { export class PlainType extends Type { } -export class MetadataStorageType extends EnumType { +export class MetadataStorageType extends EnumType { constructor (value?: any, index?: number) { super({ PlainType, - MapType + MapType, + DoubleMapType }, value, index); } + /** + * @description `true` if the storage entry is a doublemap + */ + get isDoubleMap (): boolean { + return this.toNumber() === 2; + } + /** * @description `true` if the storage entry is a map */ @@ -65,6 +74,13 @@ export class MetadataStorageType extends EnumType { return this.toNumber() === 1; } + /** + * @description The value as a mapped value + */ + get asDoubleMap (): DoubleMapType { + return this.value as DoubleMapType; + } + /** * @description The value as a mapped value */ @@ -83,6 +99,10 @@ export class MetadataStorageType extends EnumType { * @description Returns the string representation of the value */ toString (): string { + if (this.isDoubleMap) { + return this.asDoubleMap.value.toString(); + } + return this.isMap ? this.asMap.value.toString() : this.asType.toString(); diff --git a/packages/types/src/Metadata/v3/Metadata.ts b/packages/types/src/Metadata/v3/Metadata.ts index 1c53cb3c23..ec65474759 100644 --- a/packages/types/src/Metadata/v3/Metadata.ts +++ b/packages/types/src/Metadata/v3/Metadata.ts @@ -109,11 +109,15 @@ export default class MetadataV3 extends Struct implements MetadataInterface { return this.modules.map((mod) => mod.storage.isNone ? [] - : mod.storage.unwrap().map((fn) => - fn.type.isMap - ? [fn.type.asMap.key.toString(), fn.type.asMap.value.toString()] - : [fn.type.asType.toString()] - ) + : mod.storage.unwrap().map((fn) => { + if (fn.type.isMap) { + return [fn.type.asMap.key.toString(), fn.type.asMap.value.toString()]; + } else if (fn.type.isDoubleMap) { + return [fn.type.asDoubleMap.key1.toString(), fn.type.asDoubleMap.key2.toString(), fn.type.asDoubleMap.value.toString()]; + } else { + return [fn.type.asType.toString()]; + } + }) ); } diff --git a/packages/types/src/Metadata/v3/Storage.ts b/packages/types/src/Metadata/v3/Storage.ts index 072287bdea..926ef94628 100644 --- a/packages/types/src/Metadata/v3/Storage.ts +++ b/packages/types/src/Metadata/v3/Storage.ts @@ -2,110 +2,12 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import EnumType from '../../codec/EnumType'; import Struct from '../../codec/Struct'; import Vector from '../../codec/Vector'; import Bytes from '../../primitive/Bytes'; import Text from '../../primitive/Text'; import { MetadataStorageModifier } from '../v1/Storage'; -import { MapType, PlainType } from '../v2/Storage'; - -export class DoubleMapType extends Struct { - constructor (value?: any) { - super({ - key1: Text, - key2: Text, - value: Text, - keyHasher: Text - }, value); - } - - /** - * @description The mapped key as [[Text]] - */ - get key1 (): Text { - return this.get('key1') as Text; - } - - /** - * @description The mapped key as [[Text]] - */ - get key2 (): Text { - return this.get('key2') as Text; - } - - /** - * @description The mapped key as [[Text]] - */ - get keyHasher (): Text { - return this.get('keyHasher') as Text; - } - - /** - * @description The mapped key as [[Text]] - */ - get value (): Text { - return this.get('value') as Text; - } -} - -export class MetadataStorageType extends EnumType { - constructor (value?: any, index?: number) { - super({ - PlainType, - MapType, - DoubleMapType - }, value, index); - } - - /** - * @description The value as a mapped value - */ - get asDoubleMap (): DoubleMapType { - return this.value as DoubleMapType; - } - - /** - * @description The value as a mapped value - */ - get asMap (): MapType { - return this.value as MapType; - } - - /** - * @description The value as a [[Type]] value - */ - get asType (): PlainType { - return this.value as PlainType; - } - - /** - * @description `true` if the storage entry is a doublemap - */ - get isDoubleMap (): boolean { - return this.toNumber() === 2; - } - - /** - * @description `true` if the storage entry is a map - */ - get isMap (): boolean { - return this.toNumber() === 1; - } - - /** - * @description Returns the string representation of the value - */ - toString (): string { - if (this.isDoubleMap) { - return this.asDoubleMap.toString(); - } - - return this.isMap - ? this.asMap.value.toString() - : this.asType.toString(); - } -} +import { MetadataStorageType } from '../v2/Storage'; /** * @name MetadataModule