Codec Fallback on createType (along with pedantic) (#929)

* Codec Fallback on createType (along with pedantic)

* Updates for numbers (fix failing tests)

* Add isPedantic to linked
This commit is contained in:
Jaco Greeff
2019-05-17 14:07:51 +02:00
committed by GitHub
parent e326f27ac6
commit 03234a0cc9
11 changed files with 75 additions and 75 deletions
+2 -2
View File
@@ -108,8 +108,8 @@ export default class Compact extends Base<UInt | Moment> implements Codec {
/**
* @description Returns a hex string representation of the value
*/
toHex (): any {
return this.raw.toHex();
toHex (isLe?: boolean): any {
return this.raw.toHex(isLe);
}
/**
+2 -2
View File
@@ -33,10 +33,10 @@ export default class Int extends AbstractInt {
/**
* @description Returns a hex string representation of the value
*/
toHex (): string {
toHex (isLe: boolean = false): string {
return bnToHex(this, {
bitLength: this._bitLength,
isLe: false,
isLe,
isNegative: true
});
}
+2 -2
View File
@@ -23,12 +23,12 @@ export default class Option<T extends Codec> extends Base<T> implements Codec {
);
}
static decodeOption<O> (Type: Constructor, value?: any): Codec {
static decodeOption (Type: Constructor, value?: any): Codec {
if (isNull(value) || isUndefined(value) || value instanceof Null) {
return new Null();
} else if (value instanceof Option) {
return Option.decodeOption(Type, value.value);
} else if (value instanceof Type) {
} else if (value instanceof Type || (Type.Fallback && value instanceof Type.Fallback)) {
// don't re-create, use as it (which also caters for derived types)
return value;
} else if (isU8a(value)) {
+3 -2
View File
@@ -33,10 +33,11 @@ export default class UInt extends AbstractInt {
/**
* @description Returns a hex string representation of the value
*/
toHex (): string {
toHex (isLe: boolean = false): string {
// For display/JSON, this is BE, for compare, use isLe
return bnToHex(this, {
bitLength: this._bitLength,
isLe: false,
isLe,
isNegative: false
});
}
+4
View File
@@ -57,6 +57,10 @@ export default class Vector<T extends Codec> extends AbstractArray<T> {
constructor (value?: Array<any>) {
super(Type, value);
}
static Fallback = Type.Fallback
? Vector.with(Type.Fallback)
: undefined;
};
}
+25 -4
View File
@@ -226,10 +226,31 @@ export function createClass (type: Text | string): Constructor {
);
}
export default function createType (type: Text | string, value?: any): Codec {
function initType (Type: Constructor, value?: any, isPedantic?: boolean): Codec {
try {
const created = new Type(value);
// in pedantic mode, actually check that the encoding matches that supplied - this
// is much slower, but ensures that we have a 100% grasp on the actual provided value
if (isPedantic && value && value.toHex) {
const inHex = value.toHex(true);
const crHex = created.toHex(true);
assert(crHex === inHex, `Encoding for input doesn't match output, created ${crHex} from ${inHex}`);
}
return created;
} catch (error) {
if (Type.Fallback) {
return initType(Type.Fallback, value, isPedantic);
}
throw error;
}
}
export default function createType (type: Text | string, value?: any, isPedantic?: boolean): Codec {
// l.debug(() => ['createType', { type, value }]);
const Type = createClass(type);
return new Type(value);
return initType(createClass(type), value, isPedantic);
}
@@ -125,6 +125,8 @@ export default class EventRecord extends Struct {
}, value);
}
static Fallback = EventRecord77;
/**
* @description The [[Event]] this record refers to
*/
+4 -2
View File
@@ -51,9 +51,9 @@ export interface Codec {
eq (other?: any): boolean;
/**
* @description Returns a hex string representation of the value
* @description Returns a hex string representation of the value. isLe returns a LE (number-only) repersentation
*/
toHex (): string;
toHex (isLe?: boolean): string;
/**
* @description Converts the Object to JSON, typically used for RPC transfers
@@ -80,6 +80,8 @@ export interface Codec {
export type CodecTo = 'toHex' | 'toJSON' | 'toString' | 'toU8a';
export interface Constructor<T = Codec> {
Fallback?: Constructor<T>;
new(...value: Array<any>): T;
}