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:
@@ -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
@@ -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
|
||||
|
||||
@@ -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()
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user