Support recursive type registration (#1396)
* Testcase for recursive types * hasClass & hasDef * Drop .only * Option<Recursive> (simplified test) * Expand cross-ref tests, plain getOrThrow
This commit is contained in:
@@ -61,8 +61,6 @@ export default class Struct<
|
||||
* @param jsonMap
|
||||
*/
|
||||
private static decodeStruct <T> (Types: ConstructorDef, value: any, jsonMap: Map<any, string>): T {
|
||||
// l.debug(() => ['Struct.decode', { Types, value }]);
|
||||
|
||||
if (isHex(value)) {
|
||||
return Struct.decodeStruct(Types, hexToU8a(value as string), jsonMap);
|
||||
} else if (isU8a(value)) {
|
||||
|
||||
@@ -69,9 +69,8 @@ const infoMapping: Record<TypeDefInfo, (value: TypeDef) => Constructor> = {
|
||||
|
||||
[TypeDefInfo.Option]: (value: TypeDef): Constructor => Option.with(getSubType(value)),
|
||||
|
||||
[TypeDefInfo.Plain]: (value: TypeDef): Constructor => {
|
||||
throw new Error(`Unable to find plain type for ${JSON.stringify(value)}`);
|
||||
},
|
||||
[TypeDefInfo.Plain]: (value: TypeDef): Constructor =>
|
||||
getTypeRegistry().getOrThrow(value.type, `Unable to find plain type for ${JSON.stringify(value)}`),
|
||||
|
||||
[TypeDefInfo.Set]: (value: TypeDef): Constructor => {
|
||||
const result: Record<string, number> = {};
|
||||
|
||||
@@ -6,7 +6,7 @@ import '../../injector';
|
||||
|
||||
import { Constructor } from '../../types';
|
||||
|
||||
import { TypeRegistry } from './registry';
|
||||
import { TypeRegistry, getTypeRegistry } from './registry';
|
||||
import Struct from '../Struct';
|
||||
import Text from '../../primitive/Text';
|
||||
import U32 from '../../primitive/U32';
|
||||
@@ -23,7 +23,7 @@ describe('TypeRegistry', (): void => {
|
||||
let registry: TypeRegistry;
|
||||
|
||||
beforeEach((): void => {
|
||||
registry = new TypeRegistry();
|
||||
registry = getTypeRegistry();
|
||||
});
|
||||
|
||||
it('handles non exist type', (): void => {
|
||||
@@ -50,6 +50,51 @@ describe('TypeRegistry', (): void => {
|
||||
expect(isChildClass(U32, registry.get('U32Renamed'))).toBe(true);
|
||||
});
|
||||
|
||||
it('can register recursive types', (): void => {
|
||||
registry.register({
|
||||
Recursive: {
|
||||
next: 'Option<Recursive>'
|
||||
}
|
||||
});
|
||||
|
||||
expect(registry.hasDef('Recursive')).toBe(true);
|
||||
expect(registry.hasClass('Recursive')).toBe(false);
|
||||
|
||||
const Recursive = registry.getOrThrow('Recursive');
|
||||
|
||||
expect(registry.hasClass('Recursive')).toBe(true);
|
||||
|
||||
const last = new Recursive({ next: null });
|
||||
const first = new Recursive({ next: last });
|
||||
|
||||
expect((first as any).next.isSome).toBe(true);
|
||||
expect((first as any).next.unwrap().next.isSome).toBe(false);
|
||||
});
|
||||
|
||||
it('can register cross-referencing types ()', (): void => {
|
||||
registry.register({
|
||||
A: {
|
||||
next: 'B'
|
||||
},
|
||||
B: {
|
||||
_enum: {
|
||||
End: null,
|
||||
Other: 'A'
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
const A = registry.getOrThrow('A');
|
||||
const B = registry.getOrThrow('B');
|
||||
|
||||
expect(registry.hasClass('Recursive')).toBe(true);
|
||||
|
||||
const last = new B({ End: null });
|
||||
const first = new B({ Other: new A({ next: last }) });
|
||||
|
||||
expect((first as any).isOther).toBe(true);
|
||||
});
|
||||
|
||||
it('can create types from string', (): void => {
|
||||
registry.register({
|
||||
U32Renamed: 'u32'
|
||||
|
||||
@@ -93,8 +93,16 @@ export class TypeRegistry {
|
||||
return type;
|
||||
}
|
||||
|
||||
public hasClass (name: string): boolean {
|
||||
return this._classes.has(name);
|
||||
}
|
||||
|
||||
public hasDef (name: string): boolean {
|
||||
return this._definitions.has(name);
|
||||
}
|
||||
|
||||
public hasType (name: string): boolean {
|
||||
return this._classes.has(name) || this._definitions.has(name);
|
||||
return this.hasClass(name) || this.hasDef(name);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -129,7 +129,7 @@ export interface Constructor<T = Codec> {
|
||||
|
||||
export type ConstructorDef<T = Codec> = Record<string, Constructor<T>>;
|
||||
|
||||
export type RegistryTypes = Record<string, Constructor | string | Record<string, string> | { _enum: string[] | Record<string, string> } | { _set: Record<string, number> }>;
|
||||
export type RegistryTypes = Record<string, Constructor | string | Record<string, string> | { _enum: string[] | Record<string, string | null> } | { _set: Record<string, number> }>;
|
||||
|
||||
export interface RuntimeVersionInterface {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
|
||||
Reference in New Issue
Block a user