Cleanup, simplify, remove Fallback (type injection) (#1294)
This commit is contained in:
@@ -34,7 +34,7 @@ export default class Option<T extends Codec> extends Base<T> {
|
||||
return new Null();
|
||||
} else if (value instanceof Option) {
|
||||
return Option.decodeOption(Type, value.value);
|
||||
} else if (value instanceof Type || (Type.Fallback && value instanceof Type.Fallback)) {
|
||||
} else if (value instanceof Type) {
|
||||
// don't re-create, use as it (which also caters for derived types)
|
||||
return value;
|
||||
} else if (isU8a(value)) {
|
||||
|
||||
@@ -58,12 +58,6 @@ export default class Vec<T extends Codec> extends AbstractArray<T> {
|
||||
public constructor (value?: any[]) {
|
||||
super(Type, value);
|
||||
}
|
||||
|
||||
// @ts-ignore
|
||||
public static Fallback = Type.Fallback
|
||||
// @ts-ignore
|
||||
? Vec.with(Type.Fallback)
|
||||
: undefined;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -47,10 +47,6 @@ export default class VecFixed<T extends Codec> extends AbstractArray<T> {
|
||||
public constructor (value?: any[]) {
|
||||
super(Type, length, value);
|
||||
}
|
||||
|
||||
public static Fallback = Type.Fallback
|
||||
? VecFixed.with(Type.Fallback, length)
|
||||
: undefined;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -209,7 +209,7 @@ export function getTypeClassArray (defs: TypeDef[]): (InterfaceTypes)[] {
|
||||
}
|
||||
|
||||
// Returns the type Class for construction
|
||||
export function getTypeClass<T extends Codec = Codec> (value: TypeDef, Fallback?: Constructor<T>): Constructor<T> {
|
||||
export function getTypeClass<T extends Codec = Codec> (value: TypeDef): Constructor<T> {
|
||||
const Type = getTypeRegistry().get<T>(value.type);
|
||||
|
||||
if (Type) {
|
||||
@@ -293,50 +293,34 @@ export function getTypeClass<T extends Codec = Codec> (value: TypeDef, Fallback?
|
||||
return ClassOf('Null') as unknown as Constructor<T>;
|
||||
}
|
||||
|
||||
if (Fallback) {
|
||||
return Fallback;
|
||||
}
|
||||
|
||||
throw new Error(`Unable to determine type from ${JSON.stringify(value)}`);
|
||||
}
|
||||
|
||||
// Initializes a type with a value. This also checks for fallbacks and in the cases
|
||||
// where isPedantic is specified (storage decoding), also check the format/structure
|
||||
function initType<T extends Codec = Codec, K extends string = string> (Type: Constructor<FromReg<T, K>>, params: any[] = [], isPedantic?: boolean): FromReg<T, K> {
|
||||
try {
|
||||
const created = new Type(...params);
|
||||
const [value] = params;
|
||||
const created = new Type(...params);
|
||||
const [value] = params;
|
||||
|
||||
// With isPedantic, actually check that the encoding matches that supplied. This
|
||||
// is much slower, but verifies that we have the correct types defined
|
||||
if (isPedantic && value && value.toU8a && !value.isEmpty) {
|
||||
const inHex = value.toHex(true);
|
||||
const crHex = created.toHex(true);
|
||||
const hasMatch = inHex === crHex || (
|
||||
created instanceof Uint8Array
|
||||
// strip the input length
|
||||
? (value.toU8a(true).toString() === created.toU8a().toString())
|
||||
// compare raw. without additions
|
||||
: (value.toU8a(true).toString() === created.toU8a(true).toString())
|
||||
);
|
||||
// With isPedantic, actually check that the encoding matches that supplied. This
|
||||
// is much slower, but verifies that we have the correct types defined
|
||||
if (isPedantic && value && value.toU8a && !value.isEmpty) {
|
||||
const inHex = value.toHex(true);
|
||||
const crHex = created.toHex(true);
|
||||
const hasMatch = inHex === crHex || (
|
||||
created instanceof Uint8Array
|
||||
// strip the input length
|
||||
? (value.toU8a(true).toString() === created.toU8a().toString())
|
||||
// compare raw. without additions
|
||||
: (value.toU8a(true).toString() === created.toU8a(true).toString())
|
||||
);
|
||||
|
||||
if (!hasMatch) {
|
||||
if (Type.Fallback) {
|
||||
return initType(Type.Fallback as Constructor<FromReg<T, K>>, params, isPedantic);
|
||||
}
|
||||
|
||||
console.warn(`${created.toRawType()}:: Input doesn't match output, received ${inHex}, created ${crHex}`);
|
||||
}
|
||||
if (!hasMatch) {
|
||||
console.warn(`${created.toRawType()}:: Input doesn't match output, received ${inHex}, created ${crHex}`);
|
||||
}
|
||||
|
||||
return created;
|
||||
} catch (error) {
|
||||
if (Type.Fallback) {
|
||||
return initType(Type.Fallback as Constructor<FromReg<T, K>>, params, isPedantic);
|
||||
}
|
||||
|
||||
throw error;
|
||||
}
|
||||
|
||||
return created;
|
||||
}
|
||||
|
||||
// An unsafe version of the `createType` below. It's unsafe because the `type`
|
||||
|
||||
@@ -71,10 +71,7 @@ export class TypeRegistry {
|
||||
|
||||
// NOTE If we didn't extend here, we would have strange artifacts. An example is
|
||||
// Balance, with this, new Balance() instanceof u128 is true, but Balance !== u128
|
||||
Type = class extends BaseType {
|
||||
// ensure we carry through any fallbacks identified - since they are now lower
|
||||
public static Fallback = BaseType.Fallback;
|
||||
};
|
||||
Type = class extends BaseType {};
|
||||
|
||||
this._classes.set(name, Type);
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { ClassOf } from './codec/createType';
|
||||
import getTypeRegistry from './codec/typeRegistry';
|
||||
import * as definitions from './interfaces/definitions';
|
||||
import * as baseTypes from './index.types';
|
||||
@@ -20,11 +19,6 @@ export function injectTypes (): void {
|
||||
Object.values(definitions).forEach(({ types }): void =>
|
||||
registry.register(types)
|
||||
);
|
||||
|
||||
// FIXME Register the fallbacks. The issue with this atm is that it forcibly
|
||||
// creates the actual classes here, which is something we don't really want,
|
||||
// we want it do be done on-demand, not up-front.
|
||||
ClassOf('Vec<EventRecord>').Fallback = ClassOf('Vec<EventRecord0to76>');
|
||||
}
|
||||
|
||||
injectTypes();
|
||||
|
||||
@@ -14,7 +14,6 @@ import Tuple from '../../codec/Tuple';
|
||||
import Metadata from '../../Metadata';
|
||||
import { EventMetadata as EventMetadataV7 } from '../../Metadata/v7/Events';
|
||||
import Null from '../Null';
|
||||
import Unconstructable from '../Unconstructable';
|
||||
|
||||
const EventTypes: Record<string, Constructor<EventData>> = {};
|
||||
|
||||
@@ -121,7 +120,7 @@ export default class Event extends Struct {
|
||||
const methodName = meta.name.toString();
|
||||
const eventIndex = new Uint8Array([sectionIndex, methodIndex]);
|
||||
const typeDef = meta.args.map((arg): TypeDef => getTypeDef(arg.toString()));
|
||||
const Types = typeDef.map((typeDef): Constructor<Codec> => getTypeClass(typeDef, Unconstructable.with(typeDef)));
|
||||
const Types = typeDef.map((typeDef): Constructor<Codec> => getTypeClass(typeDef));
|
||||
|
||||
EventTypes[eventIndex.toString()] = class extends EventData {
|
||||
public constructor (value: Uint8Array) {
|
||||
|
||||
@@ -123,8 +123,6 @@ export interface IHash extends U8a { }
|
||||
export type CodecTo = 'toHex' | 'toJSON' | 'toString' | 'toU8a';
|
||||
|
||||
export interface Constructor<T = Codec> {
|
||||
Fallback?: Constructor<Codec>;
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
new(...value: any[]): T;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user