Update Set toU8a, Enum accepts string in constructor (#460)

* Update Set toU8a, Enum accepts string in constructor

* Standalone else

* Handle undefined case in check

* Revert previous... :(

* Flatten else usage
This commit is contained in:
Jaco Greeff
2018-12-11 14:42:01 +01:00
committed by GitHub
parent 2bfbaac09f
commit c0a9924c64
7 changed files with 59 additions and 34 deletions
+2 -2
View File
@@ -57,9 +57,9 @@ export default class ExtrinsicSignature extends Struct {
if ((version & BIT_SIGNED) === BIT_SIGNED) {
return value.subarray(1);
} else {
return {};
}
return {};
}
return value as any;
+1 -1
View File
@@ -17,11 +17,11 @@ const testEncode = (to, expected) =>
});
describe('Enum', () => {
testDecode('Enum', undefined, 'foo');
testDecode('Enum', new Enum([], 1), 'bar');
testDecode('number', 0, 'foo');
testDecode('number', 1, 'bar');
testDecode('string', 'bar', 'bar');
testDecode('Uint8Array', Uint8Array.from([0]), 'foo');
testDecode('Uint8Array', Uint8Array.from([1]), 'bar');
+27 -11
View File
@@ -2,13 +2,17 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { isU8a, u8aToHex } from '@polkadot/util';
import { isString, isU8a, u8aToHex, assert } from '@polkadot/util';
import { Codec } from '../types';
import Base from './Base';
type EnumDef = {
type EnumMap = {
[index: number]: string
};
type EnumDef = {
[index: string]: number
} | Array<string>;
/**
@@ -22,24 +26,36 @@ type EnumDef = {
// TODO:
// - It would be great if this could actually wrap actual TS enums
export default class Enum extends Base<number> implements Codec {
private _enum: EnumDef;
private _enum: EnumMap | Array<string>;
constructor (def: EnumDef, value: Enum | Uint8Array | number = 0) {
super(
Enum.decodeEnum(value)
);
constructor (def: EnumDef, value: Enum | Uint8Array | string | number = 0) {
const decoded = Enum.decodeEnum(def, value);
this._enum = def;
assert(decoded !== -1, `Unable to initialise Enum with value ${value}`);
super(decoded);
this._enum = Array.isArray(def)
? def
: Object.keys(def).reduce((result, key) => {
result[def[key]] = key;
return result;
}, {} as EnumMap);
}
static decodeEnum (value: Enum | Uint8Array | number = 0): number {
static decodeEnum (def: EnumDef, value: Enum | Uint8Array | string | number): number | undefined {
if (value instanceof Enum) {
return value.raw;
} else if (isU8a(value)) {
return value[0];
} else {
return value;
} else if (isString(value)) {
return Array.isArray(def)
? def.indexOf(value)
: def[value] || -1;
}
return value;
}
/**
+22 -13
View File
@@ -4,14 +4,23 @@
import Set from './Set';
const SET_FIELDS = {
header: 0b00000001,
body: 0b00000010,
receipt: 0b00000100,
messageQueue: 0b00001000,
justification: 0b00010000
};
const SET_ROLES = {
none: 0b00000000,
full: 0b00000001,
light: 0b00000010,
authority: 0b00000100
};
describe('Set', () => {
it('constructs via an Array<string>', () => {
const set = new Set({
none: 0b00000000,
full: 0b00000001,
light: 0b00000010,
authority: 0b00000100
}, ['full', 'authority', 'invalid']);
const set = new Set(SET_ROLES, ['full', 'authority', 'invalid']);
expect(set.isEmpty).toEqual(false);
expect(set.toString()).toEqual(
@@ -20,17 +29,17 @@ describe('Set', () => {
});
it('constructs via Uint8Array', () => {
const set = new Set({
header: 0b00000001,
body: 0b00000010,
receipt: 0b00000100,
messageQueue: 0b00001000,
justification: 0b00010000
}, new Uint8Array([0b00000001 | 0b00000010 | 0b00010000 | 0b10000000]));
const set = new Set(SET_FIELDS, new Uint8Array([0b00000001 | 0b00000010 | 0b00010000 | 0b10000000]));
expect(set.encodedLength).toEqual(1);
expect(set.toJSON()).toEqual([
'header', 'body', 'justification'
]);
});
it('hash a valid encoding', () => {
const set = new Set(SET_FIELDS, ['header', 'body', 'justification']);
expect(set.toU8a()).toEqual(new Uint8Array([19]));
});
});
+1 -1
View File
@@ -120,6 +120,6 @@ export default class Set extends Base<Array<string>> implements Codec {
* @param isBare true when the value has none of the type-specific prefixes (internal)
*/
toU8a (isBare?: boolean): Uint8Array {
return new Uint8Array(this.valueEncoded);
return new Uint8Array([this.valueEncoded]);
}
}
+4 -4
View File
@@ -43,11 +43,11 @@ export default class Tuple<
});
} else if (isHex(value)) {
return Tuple.decodeTuple(Types, hexToU8a(value));
} else {
return Types.map((Type, index) => {
return new Type(value && value[index]);
});
}
return Types.map((Type, index) => {
return new Type(value && value[index]);
});
}
static with<
+2 -2
View File
@@ -25,9 +25,9 @@ export default class U8a extends Uint8Array implements Codec {
private static decodeU8a (value: any): Uint8Array {
if (isU8a(value)) {
return value;
} else {
return u8aToU8a(value);
}
return u8aToU8a(value);
}
/**