Cleanup isPedantic mismatches (#1220)

* Don't assert, warn on isPedantic mismatches

* Re-apply assert
This commit is contained in:
Jaco Greeff
2019-07-28 09:23:22 +02:00
committed by GitHub
parent 67b66ee34c
commit 38e8ad659a
3 changed files with 23 additions and 15 deletions
+7
View File
@@ -33,6 +33,13 @@ export default class Linkage<T extends Codec> extends Struct {
return this.get('next') as Option<T>;
}
/**
* @description Returns the base runtime type name for this instance
*/
public toRawType (): string {
return `Linkage<${this.next.toRawType(true)}>`;
}
/**
* @description Custom toU8a which with bare mode does not return the linkage if empty
*/
+6 -2
View File
@@ -115,8 +115,12 @@ export default class Option<T extends Codec> extends Base<T> {
/**
* @description Returns the base runtime type name for this instance
*/
public toRawType (): string {
return `Option<${new this._Type().toRawType()}>`;
public toRawType (isBare?: boolean): string {
const wrapped = new this._Type().toRawType();
return isBare
? wrapped
: `Option<${wrapped}>`;
}
/**
+10 -13
View File
@@ -313,22 +313,19 @@ function initType<T extends Codec = Codec, K extends string = string> (Type: Con
// 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 && value.toU8a) {
if (isPedantic && value && value.toHex && value.toU8a && !value.isEmpty) {
const inHex = value.toHex(true);
const crHex = created.toHex(true);
assert(
inHex === crHex || // check that the hex matches, if matching, all-ok
(
(value instanceof ClassOf('StorageData')) && // input is from storage
(created instanceof Uint8Array) // we are a variable-length structure
// strip the input length
? (value.toU8a(true).toString() === created.toU8a().toString())
// compare raw
: (value.toU8a(true).toString() === created.toU8a(true).toString()) // check raw
),
`Input doesn't match output, received ${inHex}, created ${crHex}`
const hasMatch = inHex === crHex || (
(value instanceof ClassOf('StorageData')) && // input is from storage
(created instanceof Uint8Array) // we are a variable-length structure
// strip the input length
? (value.toU8a(true).toString() === created.toU8a().toString())
// compare raw
: (value.toU8a(true).toString() === created.toU8a(true).toString())
);
assert(hasMatch, `Input doesn't match output, received ${inHex}, created ${crHex}`);
}
return created;