assert type on all asXxx getters (#879)

* assert type on all asXxx getters

* Convert via built-in asVx

* Implicit index

* Re-add V0 (not dropped here, but dropped somewhere)

* MetadataDeprectated -> MetadataDeprecated
This commit is contained in:
Jaco Greeff
2019-05-27 15:46:31 +12:00
committed by ian.he
parent 6c0d683235
commit 13ae360c7a
11 changed files with 237 additions and 19 deletions
@@ -20,13 +20,16 @@ import v2ToV1 from './v2/toV1';
import v3ToV2 from './v3/toV2';
import v4ToV3 from './v4/toV3';
class MetadataEnum extends EnumType<Null | MetadataV1 | MetadataV2 | MetadataV3 | MetadataV4> {
class MetadataDeprecated extends Null {
}
class MetadataEnum extends EnumType<MetadataDeprecated | MetadataV0 | MetadataV1 | MetadataV2 | MetadataV3 | MetadataV4> {
constructor (value?: any) {
super({
MetadataV0, // once rolled-out, can replace this with Null
MetadataV1, // once rolled-out, can replace this with Null
MetadataV2, // once rolled-out, can replace this with Null
MetadataV3, // once rolled-out, can replace this with Null
MetadataV0, // once rolled-out, can replace this with MetadataDeprecated
MetadataV1, // once rolled-out, can replace this with MetadataDeprecated
MetadataV2, // once rolled-out, can replace this with MetadataDeprecated
MetadataV3, // once rolled-out, can replace this with MetadataDeprecated
MetadataV4
}, value);
}
@@ -35,6 +38,8 @@ class MetadataEnum extends EnumType<Null | MetadataV1 | MetadataV2 | MetadataV3
* @description Returns the wrapped values as a V0 object
*/
get asV0 (): MetadataV0 {
assert(this.isV0, `Cannot convert '${this.type}' via asV0`);
return this.value as MetadataV0;
}
@@ -42,6 +47,8 @@ class MetadataEnum extends EnumType<Null | MetadataV1 | MetadataV2 | MetadataV3
* @description Returns the wrapped values as a V1 object
*/
get asV1 (): MetadataV1 {
assert(this.isV1, `Cannot convert '${this.type}' via asV1`);
return this.value as MetadataV1;
}
@@ -49,6 +56,8 @@ class MetadataEnum extends EnumType<Null | MetadataV1 | MetadataV2 | MetadataV3
* @description Returns the wrapped values as a V2 object
*/
get asV2 (): MetadataV2 {
assert(this.isV2, `Cannot convert '${this.type}' via asV2`);
return this.value as MetadataV2;
}
@@ -56,6 +65,8 @@ class MetadataEnum extends EnumType<Null | MetadataV1 | MetadataV2 | MetadataV3
* @description Returns the wrapped values as a V3 object
*/
get asV3 (): MetadataV3 {
assert(this.isV3, `Cannot convert '${this.type}' via asV3`);
return this.value as MetadataV3;
}
@@ -63,9 +74,53 @@ class MetadataEnum extends EnumType<Null | MetadataV1 | MetadataV2 | MetadataV3
* @description Returns the wrapped values as a V4 object
*/
get asV4 (): MetadataV4 {
assert(this.isV4, `Cannot convert '${this.type}' via asV4`);
return this.value as MetadataV4;
}
/**
* @description `true` if Deprecated
*/
get isDeprecated (): boolean {
return this.type === 'MetadataDeprectated';
}
/**
* @description `true` if V0
*/
get isV0 (): boolean {
return this.type === 'MetadataV0';
}
/**
* @description `true` if V1
*/
get isV1 (): boolean {
return this.type === 'MetadataV1';
}
/**
* @description `true` if V2
*/
get isV2 (): boolean {
return this.type === 'MetadataV2';
}
/**
* @description `true` if V3
*/
get isV3 (): boolean {
return this.type === 'MetadataV3';
}
/**
* @description `true` if V4
*/
get isV4 (): boolean {
return this.type === 'MetadataV4';
}
/**
* @description The version this metadata represents
*/
@@ -139,7 +194,7 @@ export default class MetadataVersioned extends Struct implements MetadataInterfa
assert([2, 3, 4].includes(this.metadata.version), `Cannot convert metadata from v${this.metadata.version} to v1`);
if (isUndefined(this._convertedV1)) {
this._convertedV1 = v2ToV1(this.metadata.asV2);
this._convertedV1 = v2ToV1(this.asV2);
}
return this._convertedV1;
@@ -156,7 +211,7 @@ export default class MetadataVersioned extends Struct implements MetadataInterfa
assert([3, 4].includes(this.metadata.version), `Cannot convert metadata from v${this.metadata.version} to v2`);
if (isUndefined(this._convertedV2)) {
this._convertedV2 = v3ToV2(this.metadata.asV3);
this._convertedV2 = v3ToV2(this.asV3);
}
return this._convertedV2;
@@ -173,7 +228,7 @@ export default class MetadataVersioned extends Struct implements MetadataInterfa
assert([4].includes(this.metadata.version), `Cannot convert metadata from v${this.metadata.version} to v2`);
if (isUndefined(this._convertedV3)) {
this._convertedV3 = v4ToV3(this.metadata.asV4);
this._convertedV3 = v4ToV3(this.asV4);
}
return this.metadata.asV3;
+20 -7
View File
@@ -4,6 +4,8 @@
import { AnyNumber } from '../../types';
import { assert } from '@plugnet/util';
import Enum from '../../codec/Enum';
import EnumType from '../../codec/EnumType';
import Struct from '../../codec/Struct';
@@ -86,13 +88,6 @@ export class StorageFunctionType extends EnumType<PlainType | MapType | DoubleMa
return this.toNumber() === 2;
}
/**
* @description `true` if the storage entry is a map
*/
get isMap (): boolean {
return this.toNumber() === 1;
}
/**
* @description The value as a double mapped value
*/
@@ -104,6 +99,8 @@ export class StorageFunctionType extends EnumType<PlainType | MapType | DoubleMa
* @description The value as a mapped value
*/
get asMap (): MapType {
assert(this.isMap, `Cannot convert '${this.type}' via asMap`);
return this.value as MapType;
}
@@ -111,9 +108,25 @@ export class StorageFunctionType extends EnumType<PlainType | MapType | DoubleMa
* @description The value as a [[Type]] value
*/
get asType (): PlainType {
assert(this.isPlainType, `Cannot convert '${this.type}' via asType`);
return this.value as PlainType;
}
/**
* @description `true` if the storage entry is a map
*/
get isMap (): boolean {
return this.toNumber() === 1;
}
/**
* @description `true` if the storage entry is a plain type
*/
get isPlainType (): boolean {
return this.toNumber() === 0;
}
/**
* @description Returns the string representation of the value
*/
+21 -2
View File
@@ -2,10 +2,10 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { DoubleMapType } from './../v4/Storage';
import { AnyNumber } from '../../types';
import { assert } from '@plugnet/util';
import EnumType from '../../codec/EnumType';
import Struct from '../../codec/Struct';
import Vector from '../../codec/Vector';
@@ -14,6 +14,7 @@ import Bytes from '../../primitive/Bytes';
import Text from '../../primitive/Text';
import Type from '../../primitive/Type';
import { PlainType, StorageFunctionModifier } from '../v1/Storage';
import { DoubleMapType } from './../v4/Storage';
// Re-export classes that haven't changed between V1 and V2
export {
@@ -86,6 +87,8 @@ export class StorageFunctionType extends EnumType<PlainType | MapType | DoubleMa
* @description The value as a mapped value
*/
get asMap (): MapType {
assert(this.isMap, `Cannot convert '${this.type}' via asMap`);
return this.value as MapType;
}
@@ -93,9 +96,25 @@ export class StorageFunctionType extends EnumType<PlainType | MapType | DoubleMa
* @description The value as a [[Type]] value
*/
get asType (): PlainType {
assert(this.isPlainType, `Cannot convert '${this.type}' via asType`);
return this.value as PlainType;
}
/**
* @description `true` if the storage entry is a map
*/
get isMap (): boolean {
return this.toNumber() === 1;
}
/**
* @description `true` if the storage entry is a plain type
*/
get isPlainType (): boolean {
return this.toNumber() === 0;
}
/**
* @description Returns the string representation of the value
*/
+15
View File
@@ -4,6 +4,8 @@
import { AnyNumber } from '../../types';
import { assert } from '@polkadot/util';
import EnumType from '../../codec/EnumType';
import Struct from '../../codec/Struct';
import Vector from '../../codec/Vector';
@@ -74,6 +76,8 @@ export class StorageFunctionType extends EnumType<PlainType | MapType | DoubleMa
* @description The value as a mapped value
*/
get asDoubleMap (): DoubleMapType {
assert(this.isDoubleMap, `Cannot convert '${this.type}' via asDoubleMap`);
return this.value as DoubleMapType;
}
@@ -81,6 +85,8 @@ export class StorageFunctionType extends EnumType<PlainType | MapType | DoubleMa
* @description The value as a mapped value
*/
get asMap (): MapType {
assert(this.isMap, `Cannot convert '${this.type}' via asMap`);
return this.value as MapType;
}
@@ -88,6 +94,8 @@ export class StorageFunctionType extends EnumType<PlainType | MapType | DoubleMa
* @description The value as a [[Type]] value
*/
get asType (): PlainType {
assert(this.isPlainType, `Cannot convert '${this.type}' via asType`);
return this.value as PlainType;
}
@@ -105,6 +113,13 @@ export class StorageFunctionType extends EnumType<PlainType | MapType | DoubleMa
return this.toNumber() === 1;
}
/**
* @description `true` if the storage entry is a plain type
*/
get isPlainType (): boolean {
return this.toNumber() === 0;
}
/**
* @description Returns the string representation of the value
*/
+15
View File
@@ -4,6 +4,8 @@
import { AnyNumber } from '../../types';
import { assert } from '@polkadot/util';
import Enum from '../../codec/Enum';
import EnumType from '../../codec/EnumType';
import Struct from '../../codec/Struct';
@@ -168,6 +170,8 @@ export class StorageFunctionType extends EnumType<PlainType | MapType | DoubleMa
* @description The value as a mapped value
*/
get asDoubleMap (): DoubleMapType {
assert(this.isDoubleMap, `Cannot convert '${this.type}' via asDoubleMap`);
return this.value as DoubleMapType;
}
@@ -175,6 +179,8 @@ export class StorageFunctionType extends EnumType<PlainType | MapType | DoubleMa
* @description The value as a mapped value
*/
get asMap (): MapType {
assert(this.isMap, `Cannot convert '${this.type}' via asMap`);
return this.value as MapType;
}
@@ -182,6 +188,8 @@ export class StorageFunctionType extends EnumType<PlainType | MapType | DoubleMa
* @description The value as a [[Type]] value
*/
get asType (): PlainType {
assert(this.isPlainType, `Cannot convert '${this.type}' via asType`);
return this.value as PlainType;
}
@@ -199,6 +207,13 @@ export class StorageFunctionType extends EnumType<PlainType | MapType | DoubleMa
return this.toNumber() === 1;
}
/**
* @description `true` if the storage entry is a plain type
*/
get isPlainType (): boolean {
return this.toNumber() === 0;
}
/**
* @description Returns the string representation of the value
*/
+16
View File
@@ -2,6 +2,8 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { assert } from '@polkadot/util';
import EnumType from '../codec/EnumType';
import Vector from '../codec/Vector';
import Null from '../primitive/Null';
@@ -88,6 +90,8 @@ export default class ExtrinsicStatus extends EnumType<Future | Ready | Finalized
* @description Returns the item as a [[Broadcast]]
*/
get asBroadcast (): Broadcast {
assert(this.isBroadcast, `Cannot convert '${this.type}' via asBroadcast`);
return this.value as Broadcast;
}
@@ -95,6 +99,8 @@ export default class ExtrinsicStatus extends EnumType<Future | Ready | Finalized
* @description Returns the item as a [[Dropped]]
*/
get asDropped (): Dropped {
assert(this.isDropped, `Cannot convert '${this.type}' via asDropped`);
return this.value as Dropped;
}
@@ -102,6 +108,8 @@ export default class ExtrinsicStatus extends EnumType<Future | Ready | Finalized
* @description Returns the item as a [[Finalized]]
*/
get asFinalized (): Finalized {
assert(this.isFinalized, `Cannot convert '${this.type}' via asFinalized`);
return this.value as Finalized;
}
@@ -109,6 +117,8 @@ export default class ExtrinsicStatus extends EnumType<Future | Ready | Finalized
* @description Returns the item as a [[Future]]
*/
get asFuture (): Future {
assert(this.isFuture, `Cannot convert '${this.type}' via asFuture`);
return this.value as Future;
}
@@ -116,6 +126,8 @@ export default class ExtrinsicStatus extends EnumType<Future | Ready | Finalized
* @description Returns the item as a [[Invalid]]
*/
get asInvalid (): Invalid {
assert(this.isInvalid, `Cannot convert '${this.type}' via asInvalid`);
return this.value as Invalid;
}
@@ -123,6 +135,8 @@ export default class ExtrinsicStatus extends EnumType<Future | Ready | Finalized
* @description Returns the item as a [[Ready]]
*/
get asReady (): Ready {
assert(this.isReady, `Cannot convert '${this.type}' via asReady`);
return this.value as Ready;
}
@@ -130,6 +144,8 @@ export default class ExtrinsicStatus extends EnumType<Future | Ready | Finalized
* @description Returns the item as a [[Usurped]]
*/
get asUsurped (): Usurped {
assert(this.isUsurped, `Cannot convert '${this.type}' via asUsurped`);
return this.value as Usurped;
}
@@ -2,6 +2,8 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { assert } from '@polkadot/util';
import EnumType from '../codec/EnumType';
import Struct from '../codec/Struct';
import Tuple from '../codec/Tuple';
@@ -66,6 +68,8 @@ export class ValidityAttestation extends EnumType<Null | ImplicitCandidateSignat
* @description Returns the item as a [[ExplicitCandidateSignature]]
*/
get asExplicitCandidateSignature (): ExplicitCandidateSignature {
assert(this.toNumber() === 2, `Cannot convert '${this.type}' via asExplicitCandidateSignature`);
return this.value as ExplicitCandidateSignature;
}
@@ -73,6 +77,8 @@ export class ValidityAttestation extends EnumType<Null | ImplicitCandidateSignat
* @description Returns the item as a [[ImplicitCandidateSignature]]
*/
get asImplicitCandidateSignature (): ImplicitCandidateSignature {
assert(this.toNumber() === 1, `Cannot convert '${this.type}' via asImplicitCandidateSignature`);
return this.value as ImplicitCandidateSignature;
}
}
+2 -2
View File
@@ -74,7 +74,7 @@ export default class ContractInfo extends EnumType<Alive | Tombstone> {
* @description Returns the item as an [[Alive]]
*/
get asAlive (): Alive {
assert(this.isAlive, `Cannot convert index ${this.toNumber()} to 'Alive'`);
assert(this.isAlive, `Cannot convert '${this.type}' via asAlive`);
return this.value as Alive;
}
@@ -83,7 +83,7 @@ export default class ContractInfo extends EnumType<Alive | Tombstone> {
* @description Returns the item as an [[Tombstone]]
*/
get asTombstone (): Tombstone {
assert(this.isTombstone, `Cannot convert index ${this.toNumber()} to 'Tombstone'`);
assert(this.isTombstone, `Cannot convert '${this.type}' via asTombstone`);
return this.value as Tombstone;
}
+35
View File
@@ -2,6 +2,8 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { assert } from '@polkadot/util';
import EnumType from '../codec/EnumType';
import Struct from '../codec/Struct';
import Tuple from '../codec/Tuple';
@@ -103,6 +105,8 @@ export class Consensus extends Tuple {
* @description The slot and signature extracted from the raw data (assuming Aura)
*/
get asAura (): [U64, Signature] {
assert(this.isAura, 'Invalid engine for asAura conversion');
const raw = this.data.toU8a(true);
return [
@@ -133,6 +137,8 @@ export class DigestItem extends EnumType<AuthoritiesChange | ChangesTrieRoot | O
* @description Returns the item as a [[AuthoritiesChange]]
*/
get asAuthoritiesChange (): AuthoritiesChange {
assert(this.isAuthoritiesChange, `Cannot convert '${this.type}' via asAuthoritiesChange`);
return this.value as AuthoritiesChange;
}
@@ -140,6 +146,8 @@ export class DigestItem extends EnumType<AuthoritiesChange | ChangesTrieRoot | O
* @description Returns the item as a [[ChangesTrieRoot]]
*/
get asChangesTrieRoot (): ChangesTrieRoot {
assert(this.isChangesTrieRoot, `Cannot convert '${this.type}' via asChangesTrieRoot`);
return this.value as ChangesTrieRoot;
}
@@ -147,6 +155,8 @@ export class DigestItem extends EnumType<AuthoritiesChange | ChangesTrieRoot | O
* @desciption Retuns the item as a [[Consensus]]
*/
get asConsensus (): Consensus {
assert(this.isConsensus, `Cannot convert '${this.type}' via asConsensus`);
return this.value as Consensus;
}
@@ -154,6 +164,8 @@ export class DigestItem extends EnumType<AuthoritiesChange | ChangesTrieRoot | O
* @description Returns the item as a [[Other]]
*/
get asOther (): Other {
assert(this.isOther, `Cannot convert '${this.type}' via asOther`);
return this.value as Other;
}
@@ -161,9 +173,25 @@ export class DigestItem extends EnumType<AuthoritiesChange | ChangesTrieRoot | O
* @description Returns the item as a [[Seal]]
*/
get asSeal (): Seal {
assert(this.isSeal, `Cannot convert '${this.type}' via asSeal`);
return this.value as Seal;
}
/**
* @description Returns true on [[AuthoritiesChange]]
*/
get isAuthoritiesChange (): boolean {
return this.type === 'AuthoritiesChange';
}
/**
* @description Returns true on [[ChangesTrieRoot]]
*/
get isChangesTrieRoot (): boolean {
return this.type === 'ChangesTrieRoot';
}
/**
* @description Returns true on [[Consensus]]
*/
@@ -171,6 +199,13 @@ export class DigestItem extends EnumType<AuthoritiesChange | ChangesTrieRoot | O
return this.type === 'Consensus';
}
/**
* @description Returns true on [[Other]]
*/
get isOther (): boolean {
return this.type === 'Other';
}
/**
* @description Returns true on [[Seal]]
*/
+6
View File
@@ -2,6 +2,8 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { assert } from '@polkadot/util';
import EnumType from '../codec/EnumType';
import Struct from '../codec/Struct';
import Null from '../primitive/Null';
@@ -41,6 +43,8 @@ export class Phase extends EnumType<ApplyExtrinsic | Finalization> {
* @description Returns the item as a [[ApplyExtrinsic]]
*/
get asApplyExtrinsic (): ApplyExtrinsic {
assert(this.isApplyExtrinsic, `Cannot convert '${this.type}' via asApplyExtrinsic`);
return this.value as ApplyExtrinsic;
}
@@ -48,6 +52,8 @@ export class Phase extends EnumType<ApplyExtrinsic | Finalization> {
* @description Returns the item as a [[Finalization]]
*/
get asFinalization (): Finalization {
assert(this.isFinalization, `Cannot convert '${this.type}' via asFinalization`);
return this.value as Finalization;
}
@@ -4,6 +4,8 @@
import { AnyNumber } from '../types';
import { assert } from '@polkadot/util';
import EnumType from '../codec/EnumType';
import Struct from '../codec/Struct';
import U32 from '../primitive/U32';
@@ -132,6 +134,8 @@ export class MisbehaviorKind extends EnumType<BftProposeOutOfTurn | BftDoublePro
* @description Returns the item as a [[BftDoubleCommit]]
*/
get asBftDoubleCommit (): BftDoubleCommit {
assert(this.isBftDoubleCommit, `Cannot convert '${this.type}' via asBftDoubleCommit`);
return this.value as BftDoubleCommit;
}
@@ -139,6 +143,8 @@ export class MisbehaviorKind extends EnumType<BftProposeOutOfTurn | BftDoublePro
* @description Returns the item as a [[BftDoublePrepare]]
*/
get asBftDoublePrepare (): BftDoublePrepare {
assert(this.isBftDoublePrepare, `Cannot convert '${this.type}' via asBftDoublePrepare`);
return this.value as BftDoublePrepare;
}
@@ -146,6 +152,8 @@ export class MisbehaviorKind extends EnumType<BftProposeOutOfTurn | BftDoublePro
* @description Returns the item as a [[BftDoublePropose]]
*/
get asBftDoublePropose (): BftDoublePropose {
assert(this.isBftDoublePropose, `Cannot convert '${this.type}' via asBftDoublePropose`);
return this.value as BftDoublePropose;
}
@@ -153,8 +161,38 @@ export class MisbehaviorKind extends EnumType<BftProposeOutOfTurn | BftDoublePro
* @description Returns the item as a [[BftProposeOutOfTurn]]
*/
get asBftProposeOutOfTurn (): BftProposeOutOfTurn {
assert(this.isBftProposeOutOfTurn, `Cannot convert '${this.type}' via asBftProposeOutOfTurn`);
return this.value as BftProposeOutOfTurn;
}
/**
* @description true when this is a BftDoubleCommit
*/
get isBftDoubleCommit (): boolean {
return this.type === 'BftDoubleCommit';
}
/**
* @description true when this is a BftDoublePrepare
*/
get isBftDoublePrepare (): boolean {
return this.type === 'BftDoublePrepare';
}
/**
* @description true when this is a BftDoublePropose
*/
get isBftDoublePropose (): boolean {
return this.type === 'BftDoublePropose';
}
/**
* @description true when this is a BftProposeOutOfTurn
*/
get isBftProposeOutOfTurn (): boolean {
return this.type === 'BftProposeOutOfTurn';
}
}
type MisbehaviorReportValue = {