feat: adding double map support

Merged in double-map (pull request #1)

Approved-by: Ian He <ian-he@outlook.com>
This commit is contained in:
Alex Wang
2019-04-16 02:53:30 +00:00
committed by Ian He
parent c14a071b74
commit 04f6d352d9
5 changed files with 93 additions and 108 deletions
+39
View File
@@ -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;
+22 -2
View File
@@ -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<PlainType | MapType> {
export class MetadataStorageType extends EnumType<PlainType | MapType | DoubleMapType> {
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<PlainType | MapType> {
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<PlainType | MapType> {
* @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();
+22 -2
View File
@@ -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<PlainType | MapType> {
export class MetadataStorageType extends EnumType<PlainType | MapType | DoubleMapType> {
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<PlainType | MapType> {
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<PlainType | MapType> {
* @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();
+9 -5
View File
@@ -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()];
}
})
);
}
+1 -99
View File
@@ -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<PlainType | MapType | DoubleMapType> {
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