Factorize some u8a decoding logic (#483)
* Simplify decoding u8a * Move to another folder * Refactor isU8a in struct * Remove ts ignore * Whitespaces * Don't modify input args
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
import { hexToU8a, isHex, isObject, isU8a, u8aConcat, u8aToHex } from '@polkadot/util';
|
||||
|
||||
import { Codec, Constructor, ConstructorDef } from '../types';
|
||||
import decodeU8a from './utils/decodeU8a';
|
||||
|
||||
/**
|
||||
* @name Struct
|
||||
@@ -65,15 +66,24 @@ export default class Struct<
|
||||
S extends ConstructorDef,
|
||||
_,
|
||||
T extends { [K in keyof S]: Codec }
|
||||
> (Types: S, value: any, jsonMap: Map<keyof S, string>): T {
|
||||
> (Types: S, value: any, jsonMap: Map<keyof S, string>): T {
|
||||
// l.debug(() => ['Struct.decode', { Types, value }]);
|
||||
|
||||
if (isHex(value)) {
|
||||
return Struct.decodeStruct(Types, hexToU8a(value as string), jsonMap);
|
||||
} else if (isU8a(value)) {
|
||||
const values = decodeU8a(value, Object.values(Types));
|
||||
|
||||
// Transform array of values to {key: value} mapping
|
||||
return Object.keys(Types).reduce((raw: T, key: keyof S, index) => {
|
||||
raw[key] = values[index];
|
||||
return raw;
|
||||
}, {} as T);
|
||||
} else if (!value) {
|
||||
return {} as T;
|
||||
}
|
||||
|
||||
// We assume from here that value is a JS object (Array, Map, Object)
|
||||
return Struct.decodeStructFromObject(Types, value, jsonMap);
|
||||
}
|
||||
|
||||
@@ -81,22 +91,13 @@ export default class Struct<
|
||||
S extends ConstructorDef,
|
||||
_,
|
||||
T extends { [K in keyof S]: Codec }
|
||||
> (Types: S, value: any, jsonMap: Map<keyof S, string>): T {
|
||||
// `currentIndex` is only used when we have a UintArray/U8a as value. It's
|
||||
// used to track at which index we are currently parsing in that array.
|
||||
let currentIndex = 0;
|
||||
|
||||
> (Types: S, value: any, jsonMap: Map<keyof S, string>): T {
|
||||
return Object.keys(Types).reduce((raw: T, key: keyof S, index) => {
|
||||
// The key in the JSON can be snake_case (or other cases), but in our
|
||||
// Types, result or any other maps, it's camelCase
|
||||
const jsonKey = (jsonMap.get(key as any) && !value[key]) ? jsonMap.get(key as any) : key;
|
||||
|
||||
if (isU8a(value)) {
|
||||
raw[key] = new Types[key](value.subarray(currentIndex));
|
||||
|
||||
// Move the currentIndex forward
|
||||
currentIndex += raw[key].encodedLength;
|
||||
} else if (Array.isArray(value)) {
|
||||
if (Array.isArray(value)) {
|
||||
raw[key] = value[index] instanceof Types[key]
|
||||
? value[index]
|
||||
: new Types[key](value[index]);
|
||||
@@ -120,7 +121,7 @@ export default class Struct<
|
||||
|
||||
static with<
|
||||
S extends ConstructorDef
|
||||
> (Types: S): Constructor<Struct<S>> {
|
||||
> (Types: S): Constructor<Struct<S>> {
|
||||
return class extends Struct<S> {
|
||||
constructor (value?: any, jsonMap?: Map<keyof S, string>) {
|
||||
super(Types, value, jsonMap);
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
import { isU8a, u8aConcat, u8aToHex, isHex, hexToU8a } from '@polkadot/util';
|
||||
|
||||
import { AnyNumber, AnyU8a, AnyString, Codec, Constructor } from '../types';
|
||||
import decodeU8a from './utils/decodeU8a';
|
||||
|
||||
/**
|
||||
* @name Tuple
|
||||
@@ -27,20 +28,9 @@ export default class Tuple<
|
||||
|
||||
private static decodeTuple<
|
||||
S extends Array<Constructor>
|
||||
> (Types: S, value: AnyU8a | string | Array<AnyU8a | AnyNumber | AnyString | undefined | null>): Array<Codec> {
|
||||
> (Types: S, value: AnyU8a | string | Array<AnyU8a | AnyNumber | AnyString | undefined | null>): Array<Codec> {
|
||||
if (isU8a(value)) {
|
||||
// `currentIndex` is only used when we have a UintArray/U8a as value. It's
|
||||
// used to track at which index we are currently parsing in the U8a.
|
||||
let currentIndex = 0;
|
||||
|
||||
return Types
|
||||
.map((Type) => {
|
||||
const raw = new Type(value.subarray(currentIndex));
|
||||
// Move the currentIndex forward
|
||||
currentIndex += raw.encodedLength;
|
||||
|
||||
return raw;
|
||||
});
|
||||
return decodeU8a(value, Types);
|
||||
} else if (isHex(value)) {
|
||||
return Tuple.decodeTuple(Types, hexToU8a(value));
|
||||
}
|
||||
@@ -52,7 +42,7 @@ export default class Tuple<
|
||||
|
||||
static with<
|
||||
S extends Array<Constructor>
|
||||
> (Types: S): Constructor<Tuple<S>> {
|
||||
> (Types: S): Constructor<Tuple<S>> {
|
||||
return class extends Tuple<S> {
|
||||
constructor (value?: any) {
|
||||
super(Types, value);
|
||||
|
||||
@@ -6,6 +6,7 @@ import { u8aConcat, u8aToU8a, u8aToHex } from '@polkadot/util';
|
||||
|
||||
import Compact from './Compact';
|
||||
import { Codec, Constructor } from '../types';
|
||||
import decodeU8a from './utils/decodeU8a';
|
||||
|
||||
/**
|
||||
* @name Vector
|
||||
@@ -42,16 +43,7 @@ export default class Vector<
|
||||
let [offset, _length] = Compact.decodeU8a(u8a);
|
||||
const length = _length.toNumber();
|
||||
|
||||
const result = [];
|
||||
|
||||
for (let index = 0; index < length; index++) {
|
||||
const decoded = new Type(u8a.subarray(offset));
|
||||
|
||||
result.push(decoded);
|
||||
offset += decoded.encodedLength;
|
||||
}
|
||||
|
||||
return result;
|
||||
return decodeU8a(u8a.subarray(offset), new Array(length).fill(Type)) as T[];
|
||||
}
|
||||
|
||||
static with<O extends Codec> (Type: Constructor<O>): Constructor<Vector<O>> {
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
// Copyright 2017-2018 @polkadot/types authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { Codec, Constructor } from '../../types';
|
||||
|
||||
/**
|
||||
* Given an u8a, and an array of Type constructors, decode the u8a against the
|
||||
* types, and return an array of decoded values.
|
||||
*
|
||||
* @param u8a - The u8a to decode.
|
||||
* @param types - The array of Constructor to decode the U8a against.
|
||||
*/
|
||||
export default function decodeU8a (u8a: Uint8Array, types: Constructor[]): Codec[] {
|
||||
if (!types.length) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const Type = types[0];
|
||||
const value = new Type(u8a);
|
||||
|
||||
return [value].concat(decodeU8a(u8a.subarray(value.encodedLength), types.slice(1)));
|
||||
}
|
||||
Reference in New Issue
Block a user