Type registry (#428)
* basic type registry * register all buildin types * get types from TypeRegistry * Update Api constructor options to include additionalTypes * Fix linter issue * Style improvements and rename ApiBaseOptions to ApiOptions * refactor Api classes constructor options
This commit is contained in:
committed by
Amaury Martiny
parent
d5515efa83
commit
82dcbe50d5
@@ -0,0 +1,38 @@
|
||||
// Copyright 2017-2018 @polkadot/types authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import TypeRegistry from './TypeRegistry';
|
||||
import Text from '../Text';
|
||||
import U32 from '../U32';
|
||||
|
||||
describe('TypeRegistry', () => {
|
||||
let registry: TypeRegistry;
|
||||
|
||||
beforeEach(() => {
|
||||
registry = new TypeRegistry();
|
||||
});
|
||||
|
||||
it('Handles non exist type', () => {
|
||||
expect(registry.get('non-exist')).toBeUndefined();
|
||||
});
|
||||
|
||||
it('Able to register type', () => {
|
||||
registry.register(Text);
|
||||
expect(registry.get('Text')).toBe(Text);
|
||||
});
|
||||
|
||||
it('Able to 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
|
||||
});
|
||||
expect(registry.get('Text')).toBe(Text);
|
||||
expect(registry.get('U32Renamed')).toBe(U32);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,40 @@
|
||||
// Copyright 2017-2018 @polkadot/types authors & contributors
|
||||
// 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 * as allTypes from '../index';
|
||||
|
||||
export default class TypeRegistry {
|
||||
static readonly defaultRegistry: TypeRegistry = new TypeRegistry();
|
||||
|
||||
private _registry: Map<string, Constructor> = new Map();
|
||||
|
||||
register (type: Constructor | {[name: string]: Constructor}): void;
|
||||
register (name: string, type: Constructor): void;
|
||||
register (arg1: string | Constructor | {[name: string]: Constructor}, arg2?: Constructor): void {
|
||||
if (typeof arg1 === 'string') {
|
||||
const name = arg1;
|
||||
const type = arg2!;
|
||||
|
||||
this._registry.set(name, type);
|
||||
} else if (typeof arg1 === 'function') {
|
||||
const name = arg1.name;
|
||||
const type = arg1;
|
||||
|
||||
this._registry.set(name, type);
|
||||
} else {
|
||||
const dict = arg1;
|
||||
|
||||
for (const [name, type] of Object.entries(dict)) {
|
||||
this._registry.set(name, type);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
get (name: string) {
|
||||
return this._registry.get(name);
|
||||
}
|
||||
}
|
||||
|
||||
TypeRegistry.defaultRegistry.register(allTypes as any);
|
||||
@@ -8,6 +8,7 @@ import { Codec, Constructor } from '../types';
|
||||
import Text from '../Text';
|
||||
import Compact from './Compact';
|
||||
import Tuple from './Tuple';
|
||||
import TypeRegistry from './TypeRegistry';
|
||||
import UInt from './UInt';
|
||||
import Vector from './Vector';
|
||||
|
||||
@@ -145,13 +146,12 @@ export function getTypeClass (value: TypeDef): Constructor {
|
||||
);
|
||||
}
|
||||
|
||||
// We are dynamically loading as to avoid circular dependencies
|
||||
const Types = require('../index');
|
||||
const Type = Types[value.type];
|
||||
const registry = TypeRegistry.defaultRegistry;
|
||||
const Type = registry.get(value.type);
|
||||
|
||||
assert(Type, `Unable to determine type from '${value.type}'`);
|
||||
|
||||
return Type;
|
||||
return Type!;
|
||||
}
|
||||
|
||||
export default function createType (type: Text | string, value?: any): Codec {
|
||||
|
||||
Reference in New Issue
Block a user