createType allows multiple params (#1207)

This commit is contained in:
Jaco Greeff
2019-07-27 08:05:11 +02:00
committed by GitHub
parent d56905d1b5
commit 99368a6d4d
19 changed files with 71 additions and 70 deletions
+3 -3
View File
@@ -27,12 +27,12 @@
"homepage": "https://github.com/polkadot-js/api/tree/master/packages/types#readme",
"dependencies": {
"@babel/runtime": "^7.5.5",
"@polkadot/util": "^0.94.1",
"@polkadot/util-crypto": "^0.94.1",
"@polkadot/util": "^0.95.0-beta.1",
"@polkadot/util-crypto": "^0.95.0-beta.1",
"@types/memoizee": "^0.4.2",
"memoizee": "^0.4.14"
},
"devDependencies": {
"@polkadot/keyring": "^0.94.1"
"@polkadot/keyring": "^0.95.0-beta.1"
}
}
+1 -1
View File
@@ -62,7 +62,7 @@ export function defaultValues (rpcData: string): void {
mod.storage.unwrap().items.forEach(({ fallback, name, type }): void => {
it(`creates default types for ${mod.name}.${name}, type ${type}`, (): void => {
expect(
(): Codec => createTypeUnsafe(type.toString(), fallback)
(): Codec => createTypeUnsafe(type.toString(), [fallback])
).not.toThrow();
});
});
+2 -2
View File
@@ -53,9 +53,9 @@ describe('Vec', (): void => {
});
it.skip('decodes a complex type via construction', (): void => {
const test = createTypeUnsafe('Vec<(PropIndex, Proposal, AccountId)>', new Uint8Array([
const test = createTypeUnsafe('Vec<(PropIndex, Proposal, AccountId)>', [new Uint8Array([
4, 10, 0, 0, 0, 0, 3, 80, 123, 10, 9, 34, 48, 120, 52, 50, 34, 58, 32, 34, 48, 120, 52, 51, 34, 10, 125, 10, 209, 114, 167, 76, 218, 76, 134, 89, 18, 195, 43, 160, 168, 10, 87, 174, 105, 171, 174, 65, 14, 92, 203, 89, 222, 232, 78, 47, 68, 50, 219, 79
]));
])]);
const first = (test as Vec<Codec>)[0] as Tuple;
expect((first[0] as PropIndex).toNumber()).toEqual(10);
+11 -11
View File
@@ -225,10 +225,10 @@ describe('createClass', (): void => {
describe('createType', (): void => {
it('allows creation of a Struct', (): void => {
expect(
createTypeUnsafe('{"balance":"Balance","index":"u32"}', {
createTypeUnsafe('{"balance":"Balance","index":"u32"}', [{
balance: 1234,
index: '0x10'
}).toJSON()
}]).toJSON()
).toEqual({
balance: 1234,
index: 16
@@ -237,31 +237,31 @@ describe('createType', (): void => {
it('allows creation of a Enum (simple)', (): void => {
expect(
createTypeUnsafe('{"_enum": ["A", "B", "C"]}', 1).toJSON()
createTypeUnsafe('{"_enum": ["A", "B", "C"]}', [1]).toJSON()
).toEqual({ B: null });
});
it('allows creation of a Enum (parametrised)', (): void => {
expect(
createTypeUnsafe('{"_enum": {"A": null, "B": "u32", "C": null} }', 1).toJSON()
createTypeUnsafe('{"_enum": {"A": null, "B": "u32", "C": null} }', [1]).toJSON()
).toEqual({ B: 0 });
});
it('allows creation of a Set', (): void => {
expect(
createTypeUnsafe<CodecSet>('{"_set": { "A": 1, "B": 2, "C": 4, "D": 8, "E": 16, "G": 32, "H": 64, "I": 128 } }', 1 + 4 + 16 + 64).strings
createTypeUnsafe<CodecSet>('{"_set": { "A": 1, "B": 2, "C": 4, "D": 8, "E": 16, "G": 32, "H": 64, "I": 128 } }', [1 + 4 + 16 + 64]).strings
).toEqual(['A', 'C', 'E', 'H']);
});
it('allows creation of a [u8; 8]', (): void => {
expect(
createTypeUnsafe('[u8; 8]', [0x12, 0x00, 0x23, 0x00, 0x45, 0x00, 0x67, 0x00]).toHex()
createTypeUnsafe('[u8; 8]', [[0x12, 0x00, 0x23, 0x00, 0x45, 0x00, 0x67, 0x00]]).toHex()
).toEqual('0x1200230045006700');
});
it('allows creation of a [u16; 4]', (): void => {
expect(
createTypeUnsafe('[u16; 4]', [0x1200, 0x2300, 0x4500, 0x6700]).toU8a()
createTypeUnsafe('[u16; 4]', [[0x1200, 0x2300, 0x4500, 0x6700]]).toU8a()
).toEqual(new Uint8Array([0x00, 0x12, 0x00, 0x23, 0x00, 0x45, 0x00, 0x67]));
});
@@ -269,7 +269,7 @@ describe('createType', (): void => {
const base = createType('StorageData', null);
expect(
(): Codec => createTypeUnsafe('DoubleMap<Vec<(BlockNumber,EventIndex)>>', base, true)
(): Codec => createTypeUnsafe('DoubleMap<Vec<(BlockNumber,EventIndex)>>', [base], true)
).toThrow(/ Input doesn't match output, received 0x, created 0x00/);
});
@@ -277,7 +277,7 @@ describe('createType', (): void => {
const base = createType('StorageData', null);
expect(
(): Codec => createTypeUnsafe('Vec<(BlockNumber,EventIndex)>', base, true)
(): Codec => createTypeUnsafe('Vec<(BlockNumber,EventIndex)>', [base], true)
).toThrow(/Input doesn't match output, received 0x, created 0x00/);
});
@@ -298,12 +298,12 @@ describe('createType', (): void => {
it('instanceof should work (complex type)', (): void => {
const complexType = '{"balance":"Balance","account_id":"AccountId","log":"(u64, u32)","fromSrml":"Gas"}';
const value = createTypeUnsafe(complexType, {
const value = createTypeUnsafe(complexType, [{
balance: 123,
accountId: '',
log: [456, 789],
fromSrml: 0
});
}]);
const ComplexType = createClass(complexType);
expect(value instanceof ComplexType).toBe(true);
+8 -8
View File
@@ -328,9 +328,10 @@ export function getTypeClass<T extends Codec = Codec> (value: TypeDef, Fallback?
throw new Error(`Unable to determine type from ${JSON.stringify(value)}`);
}
function initType<T extends Codec = Codec, K extends string = string> (Type: Constructor<FromReg<T, K>>, value?: any, isPedantic?: boolean): FromReg<T, K> {
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(value);
const created = new Type(...params);
const [value] = params;
// 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
@@ -352,7 +353,7 @@ function initType<T extends Codec = Codec, K extends string = string> (Type: Con
return created;
} catch (error) {
if (Type.Fallback) {
return initType(Type.Fallback as Constructor<FromReg<T, K>>, value, isPedantic);
return initType(Type.Fallback as Constructor<FromReg<T, K>>, params, isPedantic);
}
throw error;
@@ -364,11 +365,11 @@ function initType<T extends Codec = Codec, K extends string = string> (Type: Con
// runtime error.
export function createTypeUnsafe<T extends Codec = Codec, K extends string = string> (
type: K,
value?: any,
params: any[] = [],
isPedantic?: boolean
): FromReg<T, K> {
try {
return initType(createClass<T, K>(type), value, isPedantic);
return initType(createClass<T, K>(type), params, isPedantic);
} catch (error) {
throw new Error(`createType(${type}):: ${error.message}`);
}
@@ -385,8 +386,7 @@ export function createTypeUnsafe<T extends Codec = Codec, K extends string = str
*/
export default function createType<K extends keyof InterfaceRegistry> (
type: K,
value?: any,
isPedantic?: boolean
...params: any[]
): InterfaceRegistry[K] {
return createTypeUnsafe<Codec, K>(type, value, isPedantic) as InterfaceRegistry[K];
return createTypeUnsafe<Codec, K>(type, params) as InterfaceRegistry[K];
}