Handle null/undefined param values properly (#127)

This commit is contained in:
Jaco Greeff
2018-08-13 11:17:23 +02:00
committed by GitHub
parent 92f5569414
commit ef1c87872e
18 changed files with 60 additions and 48 deletions
+4 -4
View File
@@ -11,7 +11,7 @@ import isUndefined from '@polkadot/util/is/undefined';
import decodeValue from './value';
function decodeTuple (type: Param$Type$Array, input: Uint8Array | null, version: EncodingVersions, isStorage: boolean): Param$Decoded {
function decodeTuple (type: Param$Type$Array, input: Uint8Array | null | undefined, version: EncodingVersions, isStorage: boolean): Param$Decoded {
if (!input) {
return {
length: 0,
@@ -36,7 +36,7 @@ function decodeTuple (type: Param$Type$Array, input: Uint8Array | null, version:
};
}
function decodeArray ([ type ]: Param$Type$Array, input: Uint8Array | null, version: EncodingVersions, isStorage: boolean): Param$Decoded {
function decodeArray ([ type ]: Param$Type$Array, input: Uint8Array | null | undefined, version: EncodingVersions, isStorage: boolean): Param$Decoded {
if (!input) {
return {
length: 0,
@@ -63,8 +63,8 @@ function decodeArray ([ type ]: Param$Type$Array, input: Uint8Array | null, vers
}
export default function decode (type: Param$Types, _input: Uint8Array | string | null | undefined, version: EncodingVersions, isStorage: boolean = false): Param$Decoded {
const input: Uint8Array | null = isUndefined(_input) || isNull(_input)
? null
const input: Uint8Array | null | undefined = isUndefined(_input) || isNull(_input)
? _input
: toU8a(_input);
if (Array.isArray(type)) {
@@ -8,10 +8,11 @@ import encodeAddress from '@polkadot/util-keyring/address/encode';
import u8a from './u8a';
export default function accountId (input: Uint8Array | null, version: EncodingVersions, isStorage: boolean): Param$Decoded {
if (input === null) {
export default function accountId (input: Uint8Array | null | undefined, version: EncodingVersions, isStorage: boolean): Param$Decoded {
if (!input) {
return {
length: 0
length: 0,
value: input
} as Param$Decoded;
}
+2 -2
View File
@@ -7,8 +7,8 @@ import { Param$Decoded } from '../../types';
import BN from 'bn.js';
import u8aToBn from '@polkadot/util/u8a/toBn';
export default function bn (input: Uint8Array | null, bitLength: 32 | 64 | 128): Param$Decoded {
if (input === null) {
export default function bn (input: Uint8Array | null | undefined, bitLength: 32 | 64 | 128): Param$Decoded {
if (!input) {
return {
length: 0,
value: new BN(0)
@@ -4,8 +4,8 @@
import { Param$Decoded } from '../../types';
export default function bool (input: Uint8Array | null): Param$Decoded {
if (input === null) {
export default function bool (input: Uint8Array | null | undefined): Param$Decoded {
if (!input) {
return {
length: 0,
value: false
@@ -4,8 +4,8 @@
import { Param$Decoded } from '../../types';
export default function byte (input: Uint8Array | null): Param$Decoded {
if (input === null) {
export default function byte (input: Uint8Array | null | undefined): Param$Decoded {
if (!input) {
return {
length: 0,
value: 0
@@ -6,10 +6,11 @@ import { Param$Decoded } from '../../types';
import u8aToBn from '@polkadot/util/u8a/toBn';
export default function bytes (input: Uint8Array | null): Param$Decoded {
if (input === null) {
export default function bytes (input: Uint8Array | null | undefined): Param$Decoded {
if (!input) {
return {
length: 0
length: 0,
value: input
} as Param$Decoded;
}
@@ -6,10 +6,11 @@ import { Param$Decoded } from '../../types';
import u8aToBn from '@polkadot/util/u8a/toBn';
export default function code (input: Uint8Array | null): Param$Decoded {
if (input === null) {
export default function code (input: Uint8Array | null | undefined): Param$Decoded {
if (!input) {
return {
length: 0
length: 0,
value: input
} as Param$Decoded;
}
@@ -9,10 +9,11 @@ import u8aToBn from '@polkadot/util/u8a/toBn';
import bytes from './bytes';
export default function digest (input: Uint8Array | null): Param$Decoded {
if (input === null) {
export default function digest (input: Uint8Array | null | undefined): Param$Decoded {
if (!input) {
return {
length: 0
length: 0,
value: input
} as Param$Decoded;
}
@@ -6,10 +6,11 @@ import { Param$Decoded } from '../../types';
import decodeHeader from '@polkadot/primitives/codec/header/decode';
export default function header (input: Uint8Array | null): Param$Decoded {
if (input === null) {
export default function header (input: Uint8Array | null | undefined): Param$Decoded {
if (!input) {
return {
length: 0
length: 0,
value: input
} as Param$Decoded;
}
@@ -27,8 +27,8 @@ import time from './time';
import u8a from './u8a';
export default function decodeValue (decode: Decoder, type: Param$Type, _input: Uint8Array | string | null | undefined, version: EncodingVersions, isStorage: boolean): Param$Decoded {
const input: Uint8Array | null = isUndefined(_input) || isNull(_input)
? null
const input: Uint8Array | null | undefined = isUndefined(_input) || isNull(_input)
? _input
: toU8a(_input);
try {
@@ -6,10 +6,11 @@ import { Param$Decoded } from '../../types';
import u8aToBn from '@polkadot/util/u8a/toBn';
export default function bytes (input: Uint8Array | null): Param$Decoded {
export default function bytes (input: Uint8Array | null | undefined): Param$Decoded {
if (!input) {
return {
length: 0
length: 0,
value: input
} as Param$Decoded;
}
@@ -16,10 +16,11 @@ type Result = {
value: ExtrinsicDecoded
};
export default function call (decode: Decoder, input: Uint8Array | null, isPublic: boolean, version: EncodingVersions, isStorage: boolean): Param$Decoded {
if (input === null) {
export default function call (decode: Decoder, input: Uint8Array | null | undefined, isPublic: boolean, version: EncodingVersions, isStorage: boolean): Param$Decoded {
if (!input) {
return {
length: 0
length: 0,
value: input
} as Param$Decoded;
}
@@ -18,8 +18,8 @@ const B_SIG = B_HASH + 32;
const length = B_SIG + 64;
export default function misbehavior (input: Uint8Array | null): Param$Decoded {
if (input === null) {
export default function misbehavior (input: Uint8Array | null | undefined): Param$Decoded {
if (!input) {
return {
length: 0
} as Param$Decoded;
@@ -4,10 +4,11 @@
import { Param$Decoded } from '../../types';
export default function passThrough (input: Uint8Array | null): Param$Decoded {
if (input === null) {
export default function passThrough (input: Uint8Array | null | undefined): Param$Decoded {
if (!input) {
return {
length: 0
length: 0,
value: input
} as Param$Decoded;
}
@@ -6,11 +6,14 @@ import { Param$Decoded } from '../../types';
import u8aToBn from '@polkadot/util/u8a/toBn';
import u8aToUtf8 from '@polkadot/util/u8a/toUtf8';
import isNull from '@polkadot/util/is/null';
import isUndefined from '@polkadot/util/is/undefined';
export default function string (input: Uint8Array | null): Param$Decoded {
if (input === null) {
export default function string (input: Uint8Array | null | undefined): Param$Decoded {
if (isUndefined(input) || isNull(input)) {
return {
length: 0
length: 0,
value: input
} as Param$Decoded;
}
@@ -6,12 +6,12 @@ import { Param$Decoded } from '../../types';
import u8aToBn from '@polkadot/util/u8a/toBn';
export default function time (input: Uint8Array | null): Param$Decoded {
if (input === null) {
export default function time (input: Uint8Array | null | undefined): Param$Decoded {
if (!input) {
return {
length: 0,
value: new Date(0)
};
value: input
} as Param$Decoded;
}
return {
+4 -3
View File
@@ -4,10 +4,11 @@
import { Param$Decoded } from '../../types';
export default function u8a (input: Uint8Array | null, bitLength: 256 | 512, offset: number): Param$Decoded {
if (input === null) {
export default function u8a (input: Uint8Array | null | undefined, bitLength: 256 | 512, offset: number): Param$Decoded {
if (!input) {
return {
length: 0
length: 0,
value: input
} as Param$Decoded;
}
+1 -1
View File
@@ -30,7 +30,7 @@ export type KeyValue = {
value: Uint8Array
}
export type Param$Value = Digest | Header | KeyValue | MisbehaviorReport | ExtrinsicDecoded | BN | Date | Uint8Array | boolean | number | string;
export type Param$Value = Digest | Header | KeyValue | MisbehaviorReport | ExtrinsicDecoded | BN | Date | Uint8Array | boolean | number | string | null;
export type Param$Value$Array = Array<Param$Value | Array<Param$Value | Array<Param$Value>>>;