Allow enum definitions via JSON (#681)
* Allow enum definitions via JSON * Return only type of type & value via toString() * Remove displaced console.error * Fix toString tests
This commit is contained in:
@@ -23,7 +23,7 @@ describe('Struct', () => {
|
||||
{ Text, U32 },
|
||||
new Uint8Array([1, 2 << 2, 49, 50])
|
||||
).toString()
|
||||
).toEqual('U32');
|
||||
).toEqual('{"U32":3289352}');
|
||||
});
|
||||
|
||||
it('decodes from a JSON input (lowercase)', () => {
|
||||
@@ -79,6 +79,16 @@ describe('Struct', () => {
|
||||
expect(new Test().toJSON()).toEqual({ a: null });
|
||||
});
|
||||
|
||||
it('creates via with', () => {
|
||||
class A extends Null { }
|
||||
class B extends U32 { }
|
||||
class C extends Null { }
|
||||
const Test = EnumType.with({ A, B, C });
|
||||
|
||||
expect(new Test().toJSON()).toEqual({ A: null });
|
||||
expect(new Test(1234, 1).toJSON()).toEqual({ B: 1234 });
|
||||
});
|
||||
|
||||
// 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
|
||||
|
||||
@@ -2,11 +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 { Codec, Constructor, ConstructorDef } from '../types';
|
||||
|
||||
import { assert, hexToU8a, isHex, isNumber, isObject, isString, isU8a, u8aConcat, u8aToHex } from '@polkadot/util';
|
||||
|
||||
import Base from './Base';
|
||||
import Null from '../Null';
|
||||
import { Codec, Constructor } from '../types';
|
||||
|
||||
type EnumConstructor<T = Codec> = { new(value?: any, index?: number): T };
|
||||
|
||||
type TypesDef = {
|
||||
[name: string]: Constructor
|
||||
@@ -95,6 +98,16 @@ export default class EnumType<T> extends Base<Codec> implements Codec {
|
||||
};
|
||||
}
|
||||
|
||||
static with<
|
||||
S extends ConstructorDef
|
||||
> (Types: S): EnumConstructor<EnumType<S>> {
|
||||
return class extends EnumType<S> {
|
||||
constructor (value?: any, index?: number) {
|
||||
super(Types, value, index);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The length of the value when encoded as a Uint8Array
|
||||
*/
|
||||
@@ -177,7 +190,9 @@ export default class EnumType<T> extends Base<Codec> implements Codec {
|
||||
* @description Returns the string representation of the value
|
||||
*/
|
||||
toString (): string {
|
||||
return this.type;
|
||||
return this.isNull
|
||||
? this.type
|
||||
: JSON.stringify(this.toJSON());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -212,4 +212,16 @@ describe('createType', () => {
|
||||
index: 16
|
||||
});
|
||||
});
|
||||
|
||||
it('allows creation of a Enum (simple)', () => {
|
||||
expect(
|
||||
createType('{"_enum": ["A", "B", "C"]}', 1).toJSON()
|
||||
).toEqual({ B: null });
|
||||
});
|
||||
|
||||
it('allows creation of a Enum (parametrised)', () => {
|
||||
expect(
|
||||
createType('{"_enum": {"A": null, "B": "u32", "C": null} }', 1).toJSON()
|
||||
).toEqual({ B: 0 });
|
||||
});
|
||||
});
|
||||
|
||||
@@ -6,6 +6,7 @@ import { assert } from '@polkadot/util';
|
||||
|
||||
import { Codec, Constructor } from '../types';
|
||||
import Text from '../Text';
|
||||
import EnumType from './EnumType';
|
||||
import Compact from './Compact';
|
||||
import Option from './Option';
|
||||
import Struct from './Struct';
|
||||
@@ -16,6 +17,7 @@ import registry from './typeRegistry';
|
||||
|
||||
export enum TypeDefInfo {
|
||||
Compact,
|
||||
Enum,
|
||||
Option,
|
||||
Plain,
|
||||
Struct,
|
||||
@@ -105,9 +107,20 @@ export function getTypeDef (_type: Text | string, name?: string): TypeDef {
|
||||
value.sub = typeSplit(subType).map((inner) => getTypeDef(inner));
|
||||
} else if (startingWith(type, '{', '}')) {
|
||||
const parsed = JSON.parse(type);
|
||||
const keys = Object.keys(parsed);
|
||||
|
||||
value.info = TypeDefInfo.Struct;
|
||||
value.sub = Object.keys(parsed).map((name) => getTypeDef(parsed[name], name));
|
||||
if (keys.length === 1 && keys[0] === '_enum') {
|
||||
const details = parsed[keys[0]];
|
||||
|
||||
// not as pretty, but remain compatible with oo7 for both struct and Array types
|
||||
value.sub = Array.isArray(details)
|
||||
? details.map((name) => ({ info: TypeDefInfo.Plain, name, type: 'Null' }))
|
||||
: Object.keys(details).map((name) => ({ info: TypeDefInfo.Plain, name, type: details[name] || 'Null' }));
|
||||
value.info = TypeDefInfo.Enum;
|
||||
} else {
|
||||
value.info = TypeDefInfo.Struct;
|
||||
value.sub = keys.map((name) => getTypeDef(parsed[name], name));
|
||||
}
|
||||
} else if (startingWith(type, 'Compact<', '>')) {
|
||||
value.info = TypeDefInfo.Compact;
|
||||
value.sub = getTypeDef(subType);
|
||||
@@ -130,6 +143,16 @@ export function getTypeClass (value: TypeDef): Constructor {
|
||||
return Compact.with(
|
||||
getTypeClass(value.sub as TypeDef) as Constructor<UInt>
|
||||
);
|
||||
} else if (value.info === TypeDefInfo.Enum) {
|
||||
assert(value.sub && Array.isArray(value.sub), 'Expected subtype for Enum');
|
||||
|
||||
return EnumType.with(
|
||||
(value.sub as Array<TypeDef>).reduce((result, sub, index) => {
|
||||
result[sub.name as string] = getTypeClass(sub);
|
||||
|
||||
return result;
|
||||
}, {} as { [index: string]: Constructor })
|
||||
);
|
||||
} else if (value.info === TypeDefInfo.Option) {
|
||||
assert(value.sub && !Array.isArray(value.sub), 'Expected subtype for Option');
|
||||
|
||||
|
||||
Reference in New Issue
Block a user