derive.{balances, contracts, staking, session} returns Codec (#953)

* derive.{balances, contracts, staking, session} returns Codec

* Json -> StructAny (as per VectorAny)

* multi returns VectorAny

* VectorAny casts

* Bump common
This commit is contained in:
Jaco Greeff
2019-05-27 12:49:19 +02:00
committed by GitHub
parent 2fe6451447
commit acaca67385
36 changed files with 276 additions and 363 deletions
@@ -3,7 +3,10 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import { Codec } from '../types';
import { compareMap } from '../codec/utils';
import { isUndefined } from '@polkadot/util';
import { compareMap } from './utils';
/**
* @name Json
@@ -13,9 +16,24 @@ import { compareMap } from '../codec/utils';
* i.e. no hex decoding. Unlike a struct, this waps a JSON object with unknown keys
* @noInheritDoc
*/
export default class Json extends Map<string, any> implements Codec {
export default class StructAny extends Map<string, any> implements Codec {
constructor (value?: { [index: string]: any } | null) {
super(Json.decodeJson(value));
const decoded = StructAny.decodeJson(value);
super(decoded);
// like we are doing with structs, add the keys as getters
decoded.forEach(([key]) => {
// do not clobber existing properties on the object
if (!isUndefined((this as any)[key])) {
return;
}
Object.defineProperty(this, key, {
enumerable: true,
get: () => this.get(key)
});
});
}
private static decodeJson (value?: { [index: string]: any } | null): Array<[string, any]> {
+2
View File
@@ -14,8 +14,10 @@ export { default as EnumType } from './EnumType';
export { default as Option } from './Option';
export { default as Set } from './Set';
export { default as Struct } from './Struct';
export { default as StructAny } from './StructAny';
export { default as Tuple } from './Tuple';
export { default as Vector } from './Vector';
export { default as VectorAny } from './VectorAny';
// Convenience base classes, used as "anything of this type" bases
export { default as U8a } from './U8a';
+2 -2
View File
@@ -2,14 +2,14 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import Json from './Json';
import StructAny from '../codec/StructAny';
/**
* @name ChainProperties
* @description
* Wraps the properties retrieved from the chain via the `system.properties` RPC call.
*/
export default class ChainProperties extends Json {
export default class ChainProperties extends StructAny {
/**
* @description The token decimals, if defined (de-facto standard only)
*/
+2 -2
View File
@@ -2,12 +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 Json from './Json';
import StructAny from '../codec/StructAny';
/**
* @name NetworkState
* @description
* Wraps the properties retrieved from the chain via the `system.network_state` RPC call.
*/
export default class NetworkState extends Json {
export default class NetworkState extends StructAny {
}
-1
View File
@@ -14,7 +14,6 @@ export { default as Digest, DigestItem } from './Digest';
export { default as ExtrinsicStatus } from './ExtrinsicStatus';
export { default as Header } from './Header';
export { default as Health } from './Health';
export { default as Json } from './Json';
export { default as NetworkState } from './NetworkState';
export { default as PeerInfo } from './PeerInfo';
export { default as PendingExtrinsics } from './PendingExtrinsics';