EnumType toJSON to include type & value (#645)

* EnumType toJSON to include type & value

* Update metadata for new EnumType formatting

* PlainType & MapType
This commit is contained in:
Jaco Greeff
2019-01-28 18:01:25 +01:00
committed by GitHub
parent c4fe565fbb
commit ebcb7954cc
5 changed files with 3100 additions and 2632 deletions
+11 -8
View File
@@ -128,7 +128,7 @@ export class StorageFunctionModifier extends Enum {
}
}
export class StorageFunctionType$Map extends Struct {
export class MapType extends Struct {
constructor (value?: any) {
super({
key: Type,
@@ -151,11 +151,14 @@ export class StorageFunctionType$Map extends Struct {
}
}
export class StorageFunctionType extends EnumType<Type | StorageFunctionType$Map> {
class PlainType extends Type {
}
export class StorageFunctionType extends EnumType<PlainType | MapType> {
constructor (value?: any, index?: number) {
super({
Type,
StorageFunctionType$Map
PlainType,
MapType
}, value, index);
}
@@ -169,15 +172,15 @@ export class StorageFunctionType extends EnumType<Type | StorageFunctionType$Map
/**
* @description The value as a mapped value
*/
get asMap (): StorageFunctionType$Map {
return this.value as StorageFunctionType$Map;
get asMap (): MapType {
return this.value as MapType;
}
/**
* @description The value as a [[Type]] value
*/
get asType (): Type {
return this.value as Type;
get asType (): PlainType {
return this.value as PlainType;
}
/**
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
+17
View File
@@ -62,6 +62,23 @@ describe('Struct', () => {
).toEqual('Null');
});
it('stringifies with custom types', () => {
class A extends Null { }
class B extends Null { }
class C extends Null { }
class Test extends EnumType<A | B | C> {
constructor (value?: string, index?: number) {
super({
a: A,
b: B,
c: C
}, value, index);
}
}
expect(new Test().toJSON()).toEqual({ a: null });
});
// We are currently not using this approach, none of the types in Substrate currently
// have any overrides. Insteda of trying to support it (just-in-case), rather have it
// removed to simplyfy the code - it can be pulled-back if needed
+3 -1
View File
@@ -161,7 +161,9 @@ export default class EnumType<T> extends Base<Codec> implements Codec {
* @description Converts the Object to JSON, typically used for RPC transfers
*/
toJSON (): any {
return this.raw;
return {
[this.type]: this.raw.toJSON()
};
}
/**