Allow registry to create via object/string definitions (#503)
* Allow registry to create via object/string definitions * typo
This commit is contained in:
@@ -13,26 +13,54 @@ describe('TypeRegistry', () => {
|
||||
registry = new TypeRegistry();
|
||||
});
|
||||
|
||||
it('Handles non exist type', () => {
|
||||
it('handles non exist type', () => {
|
||||
expect(registry.get('non-exist')).toBeUndefined();
|
||||
});
|
||||
|
||||
it('Able to register type', () => {
|
||||
it('can register single type', () => {
|
||||
registry.register(Text);
|
||||
expect(registry.get('Text')).toBe(Text);
|
||||
});
|
||||
|
||||
it('Able to register type with a different name', () => {
|
||||
it('can register type with a different name', () => {
|
||||
registry.register('TextRenamed', Text);
|
||||
expect(registry.get('TextRenamed')).toBe(Text);
|
||||
});
|
||||
|
||||
it('Able to register multiple types', () => {
|
||||
registry.register({
|
||||
Text,
|
||||
'U32Renamed': U32
|
||||
describe('object registration', () => {
|
||||
it('can register multiple types', () => {
|
||||
registry.register({
|
||||
Text,
|
||||
'U32Renamed': U32
|
||||
});
|
||||
expect(registry.get('Text')).toBe(Text);
|
||||
expect(registry.get('U32Renamed')).toBe(U32);
|
||||
});
|
||||
|
||||
it('can create types from string', () => {
|
||||
registry.register({
|
||||
'U32Renamed': 'u32'
|
||||
});
|
||||
expect(registry.get('U32Renamed')).toBe(U32);
|
||||
});
|
||||
|
||||
it('can create structs via definition', () => {
|
||||
registry.register({
|
||||
'SomeStruct': {
|
||||
'foo': 'u32',
|
||||
'bar': 'Text'
|
||||
}
|
||||
});
|
||||
|
||||
const SomeStruct: any = registry.get('SomeStruct');
|
||||
const struct: any = new SomeStruct({
|
||||
foo: 42,
|
||||
bar: 'testing'
|
||||
});
|
||||
|
||||
expect(SomeStruct.name).toBe('Struct');
|
||||
expect(struct.get('foo').toNumber()).toEqual(42);
|
||||
expect(struct.get('bar').toString()).toEqual('testing');
|
||||
});
|
||||
expect(registry.get('Text')).toBe(Text);
|
||||
expect(registry.get('U32Renamed')).toBe(U32);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -169,12 +169,16 @@ export function getTypeClass (value: TypeDef): Constructor {
|
||||
return Type;
|
||||
}
|
||||
|
||||
export function createClass (type: Text | string, value?: any): Constructor {
|
||||
return getTypeClass(
|
||||
getTypeDef(type)
|
||||
);
|
||||
}
|
||||
|
||||
export default function createType (type: Text | string, value?: any): Codec {
|
||||
// l.debug(() => ['createType', { type, value }]);
|
||||
|
||||
const Type = getTypeClass(
|
||||
getTypeDef(type)
|
||||
);
|
||||
const Type = createClass(type);
|
||||
|
||||
return new Type(value);
|
||||
}
|
||||
|
||||
@@ -2,18 +2,24 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { Constructor } from '../types';
|
||||
|
||||
import { isFunction, isString } from '@polkadot/util';
|
||||
|
||||
import { Constructor } from '../types';
|
||||
import { createClass } from './createType';
|
||||
|
||||
type ConstructorObj = {
|
||||
[name: string]: Constructor | string | { [name: string]: string }
|
||||
};
|
||||
|
||||
export class TypeRegistry {
|
||||
static readonly defaultRegistry: TypeRegistry = new TypeRegistry();
|
||||
|
||||
private _registry: Map<string, Constructor> = new Map();
|
||||
|
||||
register (type: Constructor | {[name: string]: Constructor}): void;
|
||||
register (type: Constructor | ConstructorObj): void;
|
||||
register (name: string, type: Constructor): void;
|
||||
register (arg1: string | Constructor | {[name: string]: Constructor}, arg2?: Constructor): void {
|
||||
register (arg1: string | Constructor | ConstructorObj, arg2?: Constructor): void {
|
||||
if (isString(arg1)) {
|
||||
const name = arg1;
|
||||
const type = arg2!;
|
||||
@@ -25,12 +31,23 @@ export class TypeRegistry {
|
||||
|
||||
this._registry.set(name, type);
|
||||
} else {
|
||||
Object.entries(arg1).forEach(([name, type]) => {
|
||||
this._registry.set(name, type);
|
||||
});
|
||||
this.registerObject(arg1);
|
||||
}
|
||||
}
|
||||
|
||||
private registerObject (obj: ConstructorObj) {
|
||||
Object.entries(obj).forEach(([name, type]) => {
|
||||
if (isString(type)) {
|
||||
this._registry.set(name, createClass(type));
|
||||
} else if (isFunction(type)) {
|
||||
// This _looks_ a bit funny, but `typeof Clazz === 'function'
|
||||
this._registry.set(name, type);
|
||||
} else {
|
||||
this._registry.set(name, createClass(JSON.stringify(type)));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
get (name: string): Constructor | undefined {
|
||||
return this._registry.get(name);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user