Skip to content

Commit

Permalink
Alias vec<u8> -> Bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
jacogr committed Sep 25, 2018
1 parent e9b0090 commit 2e2a1bf
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 24 deletions.
Expand Up @@ -4,8 +4,8 @@

import u8aConcat from '@polkadot/util/u8a/concat';

import Length from './Length';
import U8a from './U8a';
import Length from './codec/Length';
import U8a from './codec/U8a';

// A Bytes. The significant difference between this and a normal Uint8Array is that
// this version allows for length-encoding. (i.e. it is a variable-item codec, the same
Expand Down
3 changes: 1 addition & 2 deletions packages/api-codec/src/Extrinsic.ts
Expand Up @@ -4,8 +4,7 @@

import blake2Asu8a from '@polkadot/util-crypto/blake2/asU8a';

import Bytes from './codec/Bytes';

import Bytes from './Bytes';
import Hash from './Hash';

// Representation of an Extrinsic in the system.
Expand Down
2 changes: 1 addition & 1 deletion packages/api-codec/src/Header.ts
Expand Up @@ -5,12 +5,12 @@
import BN from 'bn.js';
import blake2Asu8a from '@polkadot/util-crypto/blake2/asU8a';

import Bytes from './codec/Bytes';
import UInt from './codec/UInt';
import Struct from './codec/Struct';
import Vector from './codec/Vector';

import BlockNumber from './BlockNumber';
import Bytes from './Bytes';
import Hash from './Hash';

type DigestStruct = {
Expand Down
3 changes: 2 additions & 1 deletion packages/api-codec/src/KeyValue.ts
Expand Up @@ -2,9 +2,10 @@
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.

import Bytes from './codec/Bytes';
import Struct from './codec/Struct';

import Bytes from './Bytes';

type KeyValueStruct = {
key?: Uint8Array | string,
value?: Uint8Array | string
Expand Down
11 changes: 5 additions & 6 deletions packages/api-codec/src/Type.spec.js
Expand Up @@ -18,12 +18,11 @@ describe('Type', () => {
).toEqual('Vec<AccountId>');
});

// currently no aliasses, code left as a comment, as here
// it('handles aliasses, multiples per line', () => {
// expect(
// new Type().fromJSON('(PropIndex, AccountId, PropIndex)').toString()
// ).toEqual('(ProposalIndex, AccountId, ProposalIndex)');
// });
it('handles aliasses, multiples per line', () => {
expect(
new Type().fromJSON('(Vec<u8>, AccountId, Vec<u8>)').toString()
).toEqual('(Bytes, AccountId, Bytes)');
});

it('does not allow toU8a', () => {
expect(
Expand Down
26 changes: 14 additions & 12 deletions packages/api-codec/src/Type.ts
Expand Up @@ -43,7 +43,11 @@ export default class Type extends Text {
// remove boxing, `Box<Proposal>` -> `Proposal`
this._unwrap('Box<'),
// remove generics, `MisbehaviorReport<Hash, BlockNumber>` -> `MisbehaviorReport`
this._ungeneric()
this._ungeneric(),
// convert `RawAddress` -> `Address`
// convert `PropIndex` -> `ProposalIndex`
// alias Vec<u8> -> Bytes
this._unalias('Vec<u8>', 'Bytes')
];

this.raw = mappings.reduce((result, fn) => {
Expand Down Expand Up @@ -72,17 +76,15 @@ export default class Type extends Text {
throw new Error(`Unable to find closing matching <> on '${value}' (start ${start})`);
}

// convert `RawAddress` -> `Address`
// convert `PropIndex` -> `ProposalIndex`
// private _unalias (src: string, dest: string): Mapper {
// return (value: string): string => {
// while (value.indexOf(src) !== -1) {
// value = value.replace(src, dest);
// }

// return value;
// };
// }
private _unalias (src: string, dest: string): Mapper {
return (value: string): string => {
while (value.indexOf(src) !== -1) {
value = value.replace(src, dest);
}

return value;
};
}

private _ungeneric (): Mapper {
return (value: string): string => {
Expand Down

0 comments on commit 2e2a1bf

Please sign in to comment.