Remove PairOf, convert to simple Tuple (#360)

This commit is contained in:
Jaco Greeff
2018-10-29 11:29:22 +01:00
committed by GitHub
parent 1b87784403
commit 78f179c09e
5 changed files with 46 additions and 78 deletions
+18
View File
@@ -18,12 +18,30 @@ describe('Type', () => {
).toEqual('Vec<AccountId>');
});
it('handles nested types (embedded)', () => {
expect(
new Type('(u32, Box<Vec<AccountId>>)').toString()
).toEqual('(u32, Vec<AccountId>)');
});
it('handles aliasses, multiples per line', () => {
expect(
new Type('(Vec<u8>, AccountId, Vec<u8>)').toString()
).toEqual('(Bytes, AccountId, Bytes)');
});
it('changes PairOf<T> -> (T, T)', () => {
expect(
new Type('PairOf<T::Balance>').toString()
).toEqual('(Balance, Balance)');
});
it('changes PairOf<T> (embedded) -> (T, T)', () => {
expect(
new Type('(Vec<u8>, PairOf<T::Balance>, Vec<AccountId>)').toString()
).toEqual('(Bytes, (Balance, Balance), Vec<AccountId>)');
});
it('does not allow toU8a', () => {
expect(
() => new Type().toU8a()
+22 -3
View File
@@ -6,7 +6,7 @@ import Text from './Text';
type Mapper = (value: string) => string;
const ALLOWED_BOXES = ['Option', 'PairOf', 'Vec'];
const ALLOWED_BOXES = ['Option', 'Vec'];
// This is a extended version of String, specifically to handle types. Here we rely full on
// what string provides us, however we also "tweak" the types received from the runtime, i.e.
@@ -39,6 +39,8 @@ export default class Type extends Text {
const mappings: Array<Mapper> = [
// Remove all the trait prefixes
this._removeTraits(),
// remove PairOf<T> -> (T, T)
this._removePairOf(),
// remove boxing, `Box<Proposal>` -> `Proposal`
this._removeWrap('Box'),
// remove generics, `MisbehaviorReport<Hash, BlockNumber>` -> `MisbehaviorReport`
@@ -56,7 +58,7 @@ export default class Type extends Text {
this._originalLength = this.raw.length;
this.raw = mappings.reduce((result, fn) => {
return fn(result);
}, this.raw);
}, this.raw).trim();
return this;
}
@@ -112,6 +114,23 @@ export default class Type extends Text {
};
}
// remove the PairOf wrappers
private _removePairOf (): Mapper {
return (value: string): string => {
for (let index = 0; index < value.length; index++) {
if (value.substr(index, 7) === 'PairOf<') {
const start = index + 7;
const end = this._findClosing(value, start);
const type = value.substr(start, end - start);
value = `${value.substr(0, index)}(${type}, ${type})${value.substr(end + 1)}`;
}
}
return value;
};
}
// remove the type traits
private _removeTraits (): Mapper {
return (value: string): string => {
@@ -139,7 +158,7 @@ export default class Type extends Text {
const start = index + check.length;
const end = this._findClosing(value, start);
value = `${value.substr(start, end - start)}`;
value = `${value.substr(0, index)}${value.substr(start, end - start)}${value.substr(end + 1)}`;
}
}
-36
View File
@@ -1,36 +0,0 @@
// Copyright 2017-2018 @polkadot/types authors & contributors
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
import { Constructor, ConstructorDef } from '../types';
import Base from './Base';
import Tuple from './Tuple';
// PairOf defines a general structure that creates a Tuple pair of a specific type. These values
// are accessible via the first & second getters
export default class PairOf<
T extends Base
> extends Tuple<{ first: Constructor<T>, second: Constructor<T> }> {
static with<
S extends ConstructorDef = { [index: string]: Constructor<Base> }
> (Type: S): Constructor<Tuple<S>>;
static with<T extends Base> (Type: Constructor<T>): Constructor<PairOf<T>> {
return class extends PairOf<T> {
constructor (value?: any) {
super({
first: Type,
second: Type
}, value);
}
};
}
get first (): T {
return this.get('first') as T;
}
get second (): T {
return this.get('second') as T;
}
}
+4 -18
View File
@@ -13,8 +13,8 @@ describe('typeSplit', () => {
it('splits nested combinations', () => {
expect(
typeSplit('Text, (u32), Vec<u64>, PairOf<u32>')
).toEqual(['Text', '(u32)', 'Vec<u64>', 'PairOf<u32>']);
typeSplit('Text, (u32), Vec<u64>')
).toEqual(['Text', '(u32)', 'Vec<u64>']);
});
it('keeps nested tuples together', () => {
@@ -61,18 +61,12 @@ describe('getTypeValue', () => {
).toThrow(/Vec wrapped/);
});
it('does not allow invalid Pairs, end >', () => {
expect(
() => getTypeDef('PairOf<u32')
).toThrow(/PairOf wrapped/);
});
it('returns a type structure', () => {
expect(
getTypeDef('(u32, Vec<u64>, PairOf<u32>, (Text, Vec<(Bool, u128)>))')
getTypeDef('(u32, Vec<u64>, (Text, Vec<(Bool, u128)>))')
).toEqual({
info: TypeDefInfo.Tuple,
type: '(u32, Vec<u64>, PairOf<u32>, (Text, Vec<(Bool, u128)>))',
type: '(u32, Vec<u64>, (Text, Vec<(Bool, u128)>))',
sub: [
{
info: TypeDefInfo.Plain,
@@ -86,14 +80,6 @@ describe('getTypeValue', () => {
type: 'u64'
}
},
{
info: TypeDefInfo.PairOf,
type: 'PairOf<u32>',
sub: {
info: TypeDefInfo.Plain,
type: 'u32'
}
},
{
info: TypeDefInfo.Tuple,
type: '(Text, Vec<(Bool, u128)>)',
+2 -21
View File
@@ -7,12 +7,10 @@ import { assert } from '@polkadot/util';
import { Constructor } from '../types';
import Text from '../Text';
import Base from './Base';
import PairOf from './PairOf';
import Tuple from './Tuple';
import Vector from './Vector';
export enum TypeDefInfo {
PairOf,
Plain,
Tuple,
Vector
@@ -52,12 +50,12 @@ export function typeSplit (type: string): Array<string> {
break;
case '<':
// inc vec/pair depth
// inc vec depth
vDepth++;
break;
case '>':
// dec vec/pair depth
// dec vec depth
vDepth--;
break;
@@ -100,14 +98,6 @@ export function getTypeDef (_type: Text | string): TypeDef {
value.info = TypeDefInfo.Vector;
value.sub = getTypeDef(subType);
} else if (type.substr(0, 7) === 'PairOf<') {
assert(type[type.length - 1] === '>', `Expected PairOf wrapped with <>`);
// strip wrapping PairOf<>
const subType = type.substr(7, type.length - 7 - 1);
value.info = TypeDefInfo.PairOf;
value.sub = getTypeDef(subType);
}
return value;
@@ -135,15 +125,6 @@ export function getTypeClass (value: TypeDef): Constructor {
return Vector.with(
getTypeClass(value.sub)
);
} else if (value.info === TypeDefInfo.PairOf) {
if (!value.sub || Array.isArray(value.sub)) {
throw new Error(`Expected subtype for PairOf`);
}
// This is not too cool... because of the static overrides we have a small issue here
return PairOf.with<any>(
getTypeClass(value.sub)
);
}
// We are dynamically loading as to avoid circular dependencies