Codec.inspect (#4538)
* Codec.inspect
* Additional
* Adjust
* LookupSource
* Some tests
* All base tests
* Tests for BTree{Map, Set}
* Additional tests
* All extended type tests
* Additional
* Native tests
* Adjust
* Struct with names
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { AnyJson, Codec, IU8a, IVec, Registry } from '../types';
|
||||
import type { AnyJson, Codec, Inspect, IU8a, IVec, Registry } from '../types';
|
||||
|
||||
import { compactToU8a, u8aConcat, u8aToHex } from '@polkadot/util';
|
||||
|
||||
@@ -78,6 +78,26 @@ export abstract class AbstractArray<T extends Codec> extends Array<T> implements
|
||||
return compareArray(this, other);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Returns a breakdown of the hex encoding for this Codec
|
||||
*/
|
||||
inspect (): Inspect {
|
||||
return {
|
||||
inner: this.inspectInner(),
|
||||
value: compactToU8a(this.length)
|
||||
};
|
||||
}
|
||||
|
||||
inspectInner (): Inspect[] {
|
||||
const inner = new Array<Inspect>(this.length);
|
||||
|
||||
for (let i = 0; i < this.length; i++) {
|
||||
inner[i] = this[i].inspect();
|
||||
}
|
||||
|
||||
return inner;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Converts the Object to an standard JavaScript Array
|
||||
*/
|
||||
@@ -141,18 +161,21 @@ export abstract class AbstractArray<T extends Codec> extends Array<T> implements
|
||||
* @param isBare true when the value has none of the type-specific prefixes (internal)
|
||||
*/
|
||||
public toU8a (isBare?: boolean): Uint8Array {
|
||||
const encoded = this.toU8aInner();
|
||||
|
||||
return isBare
|
||||
? u8aConcat(...encoded)
|
||||
: u8aConcat(compactToU8a(this.length), ...encoded);
|
||||
}
|
||||
|
||||
public toU8aInner (isBare?: boolean): Uint8Array[] {
|
||||
const encoded = new Array<Uint8Array>(this.length);
|
||||
|
||||
for (let i = 0; i < this.length; i++) {
|
||||
encoded[i] = this[i].toU8a(isBare);
|
||||
}
|
||||
|
||||
return isBare
|
||||
? u8aConcat(...encoded)
|
||||
: u8aConcat(
|
||||
compactToU8a(this.length),
|
||||
...encoded
|
||||
);
|
||||
return encoded;
|
||||
}
|
||||
|
||||
// Below are methods that we override. When we do a `new Vec(...).map()`,
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { AnyNumber, INumber, IU8a, Registry, UIntBitLength } from '../types';
|
||||
import type { AnyNumber, Inspect, INumber, IU8a, Registry, UIntBitLength } from '../types';
|
||||
|
||||
import { assert, BN, BN_BILLION, BN_HUNDRED, BN_MILLION, BN_QUINTILL, BN_ZERO, bnToBn, bnToHex, bnToU8a, formatBalance, formatNumber, hexToBn, isBn, isHex, isNumber, isString, isU8a, u8aToBn } from '@polkadot/util';
|
||||
|
||||
@@ -122,6 +122,16 @@ export abstract class AbstractInt extends BN implements INumber {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Returns a breakdown of the hex encoding for this Codec
|
||||
*/
|
||||
inspect (): Inspect {
|
||||
return {
|
||||
inner: [],
|
||||
value: this.toU8a()
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @description True if this value is the max of the type
|
||||
*/
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { AnyJson, BareOpts, Codec, IU8a, Registry } from '../types';
|
||||
import type { AnyJson, BareOpts, Codec, Inspect, IU8a, Registry } from '../types';
|
||||
|
||||
/**
|
||||
* @name Base
|
||||
@@ -59,6 +59,13 @@ export abstract class Base<T extends Codec> implements Codec {
|
||||
return this.#raw.eq(other);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Returns a breakdown of the hex encoding for this Codec
|
||||
*/
|
||||
inspect (): Inspect {
|
||||
return this.#raw.inspect();
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Returns a hex string representation of the value. isLe returns a LE (number-only) representation
|
||||
*/
|
||||
|
||||
@@ -79,5 +79,14 @@ describe('Compact', (): void => {
|
||||
(new (Compact.with('u128'))(registry, 12345678987654321n).toBigInt() + 1n) === 12345678987654322n
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it('has a sane inspect', (): void => {
|
||||
expect(
|
||||
new (Compact.with(U32))(registry, 0xffff).inspect()
|
||||
).toEqual({
|
||||
inner: [],
|
||||
value: new Uint8Array([254, 255, 3, 0])
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
import type { BN } from '@polkadot/util';
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { AnyJson, AnyNumber, CodecClass, ICompact, INumber, IU8a, Registry } from '../types';
|
||||
import type { AnyJson, AnyNumber, CodecClass, ICompact, Inspect, INumber, IU8a, Registry } from '../types';
|
||||
|
||||
import { compactFromU8a, compactToU8a, isBigInt, isBn, isNumber, isString } from '@polkadot/util';
|
||||
|
||||
@@ -100,6 +100,16 @@ export class Compact<T extends INumber> implements ICompact<T> {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Returns a breakdown of the hex encoding for this Codec
|
||||
*/
|
||||
inspect (): Inspect {
|
||||
return {
|
||||
inner: [],
|
||||
value: this.toU8a()
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Returns a BigInt representation of the number
|
||||
*/
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { AnyJson, Codec, CodecClass, IU8a, Registry } from '../types';
|
||||
import type { AnyJson, Codec, CodecClass, Inspect, IU8a, Registry } from '../types';
|
||||
|
||||
/**
|
||||
* @name DoNotConstruct
|
||||
@@ -56,6 +56,10 @@ export class DoNotConstruct implements Codec {
|
||||
throw this.#neverError;
|
||||
}
|
||||
|
||||
inspect (): Inspect {
|
||||
throw this.#neverError;
|
||||
}
|
||||
|
||||
toHex (): HexString {
|
||||
throw this.#neverError;
|
||||
}
|
||||
|
||||
@@ -289,6 +289,19 @@ describe('Enum', (): void => {
|
||||
expect(test.isNone).toBe(true);
|
||||
expect(test.index).toEqual(1);
|
||||
});
|
||||
|
||||
it('has a sane inspect', (): void => {
|
||||
expect(
|
||||
new Enum(
|
||||
registry,
|
||||
{ Text, U32 },
|
||||
'0x0134120000'
|
||||
).inspect()
|
||||
).toEqual({
|
||||
inner: [{ inner: [], value: new Uint8Array([0x34, 0x12, 0x00, 0x00]) }],
|
||||
value: new Uint8Array([0x01])
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { AnyJson, Codec, CodecClass, IEnum, IU8a, Registry } from '../types';
|
||||
import type { AnyJson, Codec, CodecClass, IEnum, Inspect, IU8a, Registry } from '../types';
|
||||
|
||||
import { assert, isHex, isNumber, isObject, isString, isU8a, isUndefined, objectProperties, stringCamelCase, stringify, stringPascalCase, u8aConcat, u8aToHex, u8aToU8a } from '@polkadot/util';
|
||||
|
||||
@@ -320,6 +320,18 @@ export class Enum implements IEnum {
|
||||
return this.value.eq(other);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Returns a breakdown of the hex encoding for this Codec
|
||||
*/
|
||||
inspect (): Inspect {
|
||||
return {
|
||||
inner: this.isBasic
|
||||
? []
|
||||
: [this.#raw.inspect()],
|
||||
value: new Uint8Array([this.index])
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Returns a hex string representation of the value
|
||||
*/
|
||||
|
||||
@@ -52,6 +52,15 @@ describe('Int', (): void => {
|
||||
).toEqual(-123);
|
||||
});
|
||||
|
||||
it('has a sane inspect', (): void => {
|
||||
expect(
|
||||
new Int(registry, '0x12', 16).inspect()
|
||||
).toEqual({
|
||||
inner: [],
|
||||
value: new Uint8Array([0x12, 0x00])
|
||||
});
|
||||
});
|
||||
|
||||
describe('static with', (): void => {
|
||||
it('allows default toRawType', (): void => {
|
||||
expect(
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { Codec, IU8a, Registry } from '../types';
|
||||
import type { Codec, Inspect, IU8a, Registry } from '../types';
|
||||
|
||||
import { isNull } from '@polkadot/util';
|
||||
|
||||
@@ -51,6 +51,16 @@ export class Null implements Codec {
|
||||
return other instanceof Null || isNull(other);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Returns a breakdown of the hex encoding for this Codec
|
||||
*/
|
||||
inspect (): Inspect {
|
||||
return {
|
||||
inner: [],
|
||||
value: new Uint8Array()
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Returns a hex string representation of the value
|
||||
*/
|
||||
|
||||
@@ -153,5 +153,17 @@ describe('Option', (): void => {
|
||||
it('unwrapOrDefault to specified if non-empty', (): void => {
|
||||
expect(new Option(registry, U32, '1234').unwrapOrDefault().toNumber()).toEqual(1234);
|
||||
});
|
||||
|
||||
it('has a sane inspect', (): void => {
|
||||
expect(
|
||||
new Option(registry, U32, '1234').inspect()
|
||||
).toEqual({
|
||||
inner: [{
|
||||
inner: [],
|
||||
value: new Uint8Array([210, 4, 0, 0])
|
||||
}],
|
||||
value: new Uint8Array([0x01])
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { AnyJson, Codec, CodecClass, IOption, IU8a, Registry } from '../types';
|
||||
import type { AnyJson, Codec, CodecClass, Inspect, IOption, IU8a, Registry } from '../types';
|
||||
|
||||
import { assert, isCodec, isNull, isU8a, isUndefined, u8aToHex } from '@polkadot/util';
|
||||
|
||||
@@ -137,6 +137,22 @@ export class Option<T extends Codec> implements IOption<T> {
|
||||
return this.value.eq(other);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Returns a breakdown of the hex encoding for this Codec
|
||||
*/
|
||||
inspect (): Inspect {
|
||||
return {
|
||||
inner: this.isSome
|
||||
? [this.#raw.inspect()]
|
||||
: [],
|
||||
value: new Uint8Array(
|
||||
this.isSome
|
||||
? [1]
|
||||
: [0]
|
||||
)
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Returns a hex string representation of the value
|
||||
*/
|
||||
|
||||
@@ -7,6 +7,7 @@ import type { AnyTupleValue, CodecTo } from '@polkadot/types-codec/types';
|
||||
import { Metadata, TypeRegistry } from '@polkadot/types';
|
||||
import { Text, Tuple, U32, U128 } from '@polkadot/types-codec';
|
||||
import rpcMetadata from '@polkadot/types-support/metadata/static-substrate';
|
||||
import { stringToU8a } from '@polkadot/util';
|
||||
|
||||
describe('Tuple', (): void => {
|
||||
const registry = new TypeRegistry();
|
||||
@@ -119,6 +120,18 @@ describe('Tuple', (): void => {
|
||||
it('compares against inputs (mismatch)', (): void => {
|
||||
expect(tuple.eq(['bazzing', 72])).toBe(false);
|
||||
});
|
||||
|
||||
it('has a sane inspect', (): void => {
|
||||
expect(
|
||||
tuple.inspect()
|
||||
).toEqual({
|
||||
inner: [
|
||||
{ inner: [{ inner: [], value: stringToU8a('bazzing') }], value: new Uint8Array([7 << 2]) },
|
||||
{ inner: [], value: new Uint8Array([69, 0, 0, 0]) }
|
||||
],
|
||||
value: new Uint8Array()
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('toRawType', (): void => {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// Copyright 2017-2022 @polkadot/types-codec authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { AnyTupleValue, Codec, CodecClass, ITuple, Registry } from '../types';
|
||||
import type { AnyTupleValue, Codec, CodecClass, Inspect, ITuple, Registry } from '../types';
|
||||
|
||||
import { isFunction, isHex, isString, isU8a, stringify, u8aConcat, u8aToU8a } from '@polkadot/util';
|
||||
|
||||
@@ -100,6 +100,16 @@ export class Tuple extends AbstractArray<Codec> implements ITuple<Codec[]> {
|
||||
: Object.keys(this.#Types);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Returns a breakdown of the hex encoding for this Codec
|
||||
*/
|
||||
override inspect (): Inspect {
|
||||
return {
|
||||
inner: this.inspectInner(),
|
||||
value: new Uint8Array()
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Returns the base runtime type name for this instance
|
||||
*/
|
||||
@@ -128,12 +138,6 @@ export class Tuple extends AbstractArray<Codec> implements ITuple<Codec[]> {
|
||||
* @param isBare true when the value has none of the type-specific prefixes (internal)
|
||||
*/
|
||||
public override toU8a (isBare?: boolean): Uint8Array {
|
||||
const encoded = new Array<Uint8Array>(this.length);
|
||||
|
||||
for (let i = 0; i < this.length; i++) {
|
||||
encoded[i] = this[i].toU8a(isBare);
|
||||
}
|
||||
|
||||
return u8aConcat(...encoded);
|
||||
return u8aConcat(...this.toU8aInner(isBare));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -123,6 +123,15 @@ describe('UInt', (): void => {
|
||||
expect(new UInt(registry, '0x1234567890abcdef', 64).toJSON()).toEqual('0x1234567890abcdef');
|
||||
});
|
||||
|
||||
it('has a sane inspect', (): void => {
|
||||
expect(
|
||||
new UInt(registry, '0x12', 16).inspect()
|
||||
).toEqual({
|
||||
inner: [],
|
||||
value: new Uint8Array([0x12, 0x00])
|
||||
});
|
||||
});
|
||||
|
||||
describe('eq', (): void => {
|
||||
const test = new UInt(registry, 12345);
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ import type { Codec, CodecTo, ITuple } from '@polkadot/types-codec/types';
|
||||
import { createTypeUnsafe, GenericAccountId as AccountId, Metadata, TypeRegistry } from '@polkadot/types';
|
||||
import { Text, Vec } from '@polkadot/types-codec';
|
||||
import rpcMetadata from '@polkadot/types-support/metadata/static-substrate';
|
||||
import { randomAsU8a } from '@polkadot/util-crypto';
|
||||
import { decodeAddress, randomAsU8a } from '@polkadot/util-crypto';
|
||||
|
||||
const registry = new TypeRegistry();
|
||||
const metadata = new Metadata(registry, rpcMetadata);
|
||||
@@ -175,5 +175,20 @@ describe('Vec', (): void => {
|
||||
expect(vec).toHaveLength(10);
|
||||
expect(vec.slice(2, 7)).toHaveLength(5);
|
||||
});
|
||||
|
||||
it('has a sane inspect', (): void => {
|
||||
const addrs = [
|
||||
'5HGjWAeFDfFCWPsjFQdVV2Msvz2XtMktvgocEZcCj68kUMaw', '5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty', '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY'
|
||||
];
|
||||
const vec = registry.createType('Vec<AccountId>', addrs);
|
||||
|
||||
expect(vec.inspect()).toEqual({
|
||||
inner: addrs.map((a) => ({
|
||||
inner: [],
|
||||
value: decodeAddress(a)
|
||||
})),
|
||||
value: new Uint8Array([3 << 2])
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
import { TypeRegistry } from '@polkadot/types';
|
||||
import { Text, u16, VecFixed } from '@polkadot/types-codec';
|
||||
import { stringToU8a } from '@polkadot/util';
|
||||
|
||||
describe('VecFixed', (): void => {
|
||||
const registry = new TypeRegistry();
|
||||
@@ -33,7 +34,7 @@ describe('VecFixed', (): void => {
|
||||
let test: VecFixed<Text>;
|
||||
|
||||
beforeEach((): void => {
|
||||
test = new (VecFixed.with(Text, 5))(registry, ['1', '2', '3', undefined, '5']);
|
||||
test = new (VecFixed.with(Text, 5))(registry, ['1', '2', '3', undefined, '56']);
|
||||
});
|
||||
|
||||
it('has a sane string types', (): void => {
|
||||
@@ -43,7 +44,7 @@ describe('VecFixed', (): void => {
|
||||
|
||||
it('has a correct toHex', (): void => {
|
||||
// each entry length 1 << 2, char as hex (0x31 === `1`), one empty
|
||||
expect(test.toHex()).toEqual('0x043104320433000435');
|
||||
expect(test.toHex()).toEqual('0x04310432043300083536');
|
||||
});
|
||||
|
||||
it('has empty Uint8Array when length is 0', (): void => {
|
||||
@@ -59,5 +60,18 @@ describe('VecFixed', (): void => {
|
||||
expect(test.encodedLength).toEqual(1 + 5);
|
||||
expect(test.toU8a()).toEqual(new Uint8Array([20, 104, 101, 108, 108, 111]));
|
||||
});
|
||||
|
||||
it('has a sane inspect', (): void => {
|
||||
expect(test.inspect()).toEqual({
|
||||
inner: [
|
||||
{ inner: [{ inner: [], value: stringToU8a('1') }], value: new Uint8Array([1 << 2]) },
|
||||
{ inner: [{ inner: [], value: stringToU8a('2') }], value: new Uint8Array([1 << 2]) },
|
||||
{ inner: [{ inner: [], value: stringToU8a('3') }], value: new Uint8Array([1 << 2]) },
|
||||
{ inner: [{ inner: [], value: new Uint8Array() }], value: new Uint8Array([0]) },
|
||||
{ inner: [{ inner: [], value: stringToU8a('56') }], value: new Uint8Array([2 << 2]) }
|
||||
],
|
||||
value: new Uint8Array()
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { Codec, CodecClass, Registry } from '../types';
|
||||
import type { Codec, CodecClass, Inspect, Registry } from '../types';
|
||||
|
||||
import { assert, isU8a, u8aConcat } from '@polkadot/util';
|
||||
|
||||
@@ -70,14 +70,20 @@ export class VecFixed<T extends Codec> extends AbstractArray<T> {
|
||||
return total;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Returns a breakdown of the hex encoding for this Codec
|
||||
*/
|
||||
override inspect (): Inspect {
|
||||
return {
|
||||
inner: this.inspectInner(),
|
||||
value: new Uint8Array()
|
||||
};
|
||||
}
|
||||
|
||||
public override toU8a (): Uint8Array {
|
||||
// we override, we don't add the length prefix for ourselves, and at the same time we
|
||||
// ignore isBare on entries, since they should be properly encoded at all times
|
||||
const encoded = new Array<Uint8Array>(this.length);
|
||||
|
||||
for (let i = 0; i < this.length; i++) {
|
||||
encoded[i] = this[i].toU8a();
|
||||
}
|
||||
const encoded = this.toU8aInner();
|
||||
|
||||
return encoded.length
|
||||
? u8aConcat(...encoded)
|
||||
|
||||
@@ -5,6 +5,7 @@ import type { CodecClass, ITuple } from '@polkadot/types-codec/types';
|
||||
|
||||
import { TypeRegistry } from '@polkadot/types';
|
||||
import { BTreeMap, Enum, I32, Struct, Text, Tuple, U32 } from '@polkadot/types-codec';
|
||||
import { stringToU8a } from '@polkadot/util';
|
||||
|
||||
const registry = new TypeRegistry();
|
||||
|
||||
@@ -187,4 +188,42 @@ describe('BTreeMap', (): void => {
|
||||
expect(new (BTreeMap.with(Text, Struct.with({ a: U32, b: Text })))(registry).toRawType())
|
||||
.toBe('BTreeMap<Text,{"a":"u32","b":"Text"}>');
|
||||
});
|
||||
|
||||
it('has a sane inspect', (): void => {
|
||||
expect(
|
||||
new (BTreeMap.with(Text, Text))(registry, new Map([
|
||||
[new Text(registry, '1'), new Text(registry, 'foo')],
|
||||
[new Text(registry, '2'), new Text(registry, 'bar')],
|
||||
[new Text(registry, '3'), new Text(registry, 'baz')]
|
||||
])).inspect()
|
||||
).toEqual({
|
||||
inner: [
|
||||
{
|
||||
inner: [{ inner: [], value: stringToU8a('1') }],
|
||||
value: new Uint8Array([1 << 2])
|
||||
},
|
||||
{
|
||||
inner: [{ inner: [], value: stringToU8a('foo') }],
|
||||
value: new Uint8Array([3 << 2])
|
||||
},
|
||||
{
|
||||
inner: [{ inner: [], value: stringToU8a('2') }],
|
||||
value: new Uint8Array([1 << 2])
|
||||
},
|
||||
{
|
||||
inner: [{ inner: [], value: stringToU8a('bar') }],
|
||||
value: new Uint8Array([3 << 2])
|
||||
},
|
||||
{
|
||||
inner: [{ inner: [], value: stringToU8a('3') }],
|
||||
value: new Uint8Array([1 << 2])
|
||||
},
|
||||
{
|
||||
inner: [{ inner: [], value: stringToU8a('baz') }],
|
||||
value: new Uint8Array([3 << 2])
|
||||
}
|
||||
],
|
||||
value: new Uint8Array([3 << 2])
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -180,4 +180,18 @@ describe('BTreeSet', (): void => {
|
||||
expect(new (BTreeSet.with(Struct.with({ a: U32, b: Text })))(registry).toRawType())
|
||||
.toBe('BTreeSet<{"a":"u32","b":"Text"}>');
|
||||
});
|
||||
|
||||
it('has a sane inspect', (): void => {
|
||||
expect(
|
||||
new (BTreeSet.with(U32))(registry, [1, 2, 3, 4]).inspect()
|
||||
).toEqual({
|
||||
inner: [
|
||||
{ inner: [], value: new Uint8Array([1, 0, 0, 0]) },
|
||||
{ inner: [], value: new Uint8Array([2, 0, 0, 0]) },
|
||||
{ inner: [], value: new Uint8Array([3, 0, 0, 0]) },
|
||||
{ inner: [], value: new Uint8Array([4, 0, 0, 0]) }
|
||||
],
|
||||
value: new Uint8Array([4 << 2])
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { AnyJson, Codec, CodecClass, ISet, IU8a, Registry } from '../types';
|
||||
import type { AnyJson, Codec, CodecClass, Inspect, ISet, IU8a, Registry } from '../types';
|
||||
|
||||
import { compactFromU8a, compactToU8a, isHex, isU8a, logger, stringify, u8aConcat, u8aToHex, u8aToU8a } from '@polkadot/util';
|
||||
|
||||
@@ -138,6 +138,22 @@ export class BTreeSet<V extends Codec = Codec> extends Set<V> implements ISet<V>
|
||||
return compareSet(this, other);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Returns a breakdown of the hex encoding for this Codec
|
||||
*/
|
||||
inspect (): Inspect {
|
||||
const inner = new Array<Inspect>();
|
||||
|
||||
for (const v of this.values()) {
|
||||
inner.push(v.inspect());
|
||||
}
|
||||
|
||||
return {
|
||||
inner,
|
||||
value: compactToU8a(this.size)
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Returns a hex string representation of the value. isLe returns a LE (number-only) representation
|
||||
*/
|
||||
|
||||
@@ -28,4 +28,15 @@ describe('BitVec', (): void => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('has a sane inspect', (): void => {
|
||||
expect(
|
||||
new BitVec(registry, '0x0837').inspect()
|
||||
).toEqual({
|
||||
inner: [
|
||||
{ inner: [], value: new Uint8Array([0x08, 0x37]) }
|
||||
],
|
||||
value: new Uint8Array([0])
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// Copyright 2017-2022 @polkadot/types-codec authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { AnyU8a, Registry } from '../types';
|
||||
import type { AnyU8a, Inspect, Registry } from '../types';
|
||||
|
||||
import { assert, compactFromU8a, compactToU8a, isString, u8aConcat, u8aToU8a } from '@polkadot/util';
|
||||
|
||||
@@ -57,6 +57,16 @@ export class BitVec extends Raw {
|
||||
return this.length + compactToU8a(this.#decodedLength).length;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Returns a breakdown of the hex encoding for this Codec
|
||||
*/
|
||||
override inspect (): Inspect {
|
||||
return {
|
||||
inner: [{ inner: [], value: super.toU8a() }],
|
||||
value: compactToU8a(this.#decodedLength)
|
||||
};
|
||||
}
|
||||
|
||||
public override toHuman (): string {
|
||||
return `0b${[...this.toU8a(true)].map((d) => `00000000${d.toString(2)}`.slice(-8)).join('_')}`;
|
||||
}
|
||||
|
||||
@@ -62,4 +62,13 @@ describe('Bytes', (): void => {
|
||||
).toEqual(HEX);
|
||||
});
|
||||
});
|
||||
|
||||
it('has a sane inspect', (): void => {
|
||||
expect(
|
||||
new Bytes(registry, '0x12345678').inspect()
|
||||
).toEqual({
|
||||
inner: [{ inner: [], value: new Uint8Array([0x12, 0x34, 0x56, 0x78]) }],
|
||||
value: new Uint8Array([4 << 2])
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// Copyright 2017-2022 @polkadot/types-codec authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { AnyU8a, Registry } from '../types';
|
||||
import type { AnyU8a, Inspect, Registry } from '../types';
|
||||
|
||||
import { assert, compactAddLength, compactFromU8a, compactToU8a, isString, isU8a, u8aToU8a } from '@polkadot/util';
|
||||
|
||||
@@ -60,6 +60,16 @@ export class Bytes extends Raw {
|
||||
return this.length + compactToU8a(this.length).length;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Returns a breakdown of the hex encoding for this Codec
|
||||
*/
|
||||
override inspect (): Inspect {
|
||||
return {
|
||||
inner: [{ inner: [], value: super.toU8a() }],
|
||||
value: compactToU8a(this.length)
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Returns the base runtime type name for this instance
|
||||
*/
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
import { TypeRegistry } from '@polkadot/types';
|
||||
import { HashMap, Struct, Text, U32 } from '@polkadot/types-codec';
|
||||
import { stringToU8a } from '@polkadot/util';
|
||||
|
||||
const registry = new TypeRegistry();
|
||||
|
||||
@@ -13,4 +14,33 @@ describe('HashMap', (): void => {
|
||||
expect(new (HashMap.with(Text, Struct.with({ a: U32, b: Text })))(registry).toRawType())
|
||||
.toBe('HashMap<Text,{"a":"u32","b":"Text"}>');
|
||||
});
|
||||
|
||||
it('has a sane inspect', (): void => {
|
||||
expect(
|
||||
new (HashMap.with(Text, Text))(registry, new Map([
|
||||
[new Text(registry, '1'), new Text(registry, 'foo')],
|
||||
[new Text(registry, '2'), new Text(registry, 'bar')]
|
||||
])).inspect()
|
||||
).toEqual({
|
||||
inner: [
|
||||
{
|
||||
inner: [{ inner: [], value: stringToU8a('1') }],
|
||||
value: new Uint8Array([1 << 2])
|
||||
},
|
||||
{
|
||||
inner: [{ inner: [], value: stringToU8a('foo') }],
|
||||
value: new Uint8Array([3 << 2])
|
||||
},
|
||||
{
|
||||
inner: [{ inner: [], value: stringToU8a('2') }],
|
||||
value: new Uint8Array([1 << 2])
|
||||
},
|
||||
{
|
||||
inner: [{ inner: [], value: stringToU8a('bar') }],
|
||||
value: new Uint8Array([3 << 2])
|
||||
}
|
||||
],
|
||||
value: new Uint8Array([2 << 2])
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -5,6 +5,7 @@ import type { CodecTo } from '@polkadot/types-codec/types';
|
||||
|
||||
import { TypeRegistry } from '@polkadot/types';
|
||||
import { CodecMap, Text, U32 } from '@polkadot/types-codec';
|
||||
import { stringToU8a } from '@polkadot/util';
|
||||
|
||||
const registry = new TypeRegistry();
|
||||
const mockU32TextMap = new Map<Text, U32>();
|
||||
@@ -60,7 +61,7 @@ describe('CodecMap', (): void => {
|
||||
testEncode('toString', mockU32TextMapString);
|
||||
});
|
||||
|
||||
describe('encoding muple values', (): void => {
|
||||
describe('encoding multiple values', (): void => {
|
||||
const testEncode = (to: CodecTo, expected: any): void =>
|
||||
it(`can encode ${to}`, (): void => {
|
||||
const s = new CodecMap(registry, Text, U32, mockU32U32Map, 'BTreeMap');
|
||||
@@ -73,4 +74,33 @@ describe('CodecMap', (): void => {
|
||||
testEncode('toU8a', mockU32U32MapUint8Array);
|
||||
testEncode('toString', mockU32U32MapString);
|
||||
});
|
||||
|
||||
it('has a sane inspect', (): void => {
|
||||
expect(
|
||||
new CodecMap(registry, Text, Text, new Map([
|
||||
[new Text(registry, '1'), new Text(registry, 'foo')],
|
||||
[new Text(registry, '2'), new Text(registry, 'bar')]
|
||||
])).inspect()
|
||||
).toEqual({
|
||||
inner: [
|
||||
{
|
||||
inner: [{ inner: [], value: stringToU8a('1') }],
|
||||
value: new Uint8Array([1 << 2])
|
||||
},
|
||||
{
|
||||
inner: [{ inner: [], value: stringToU8a('foo') }],
|
||||
value: new Uint8Array([3 << 2])
|
||||
},
|
||||
{
|
||||
inner: [{ inner: [], value: stringToU8a('2') }],
|
||||
value: new Uint8Array([1 << 2])
|
||||
},
|
||||
{
|
||||
inner: [{ inner: [], value: stringToU8a('bar') }],
|
||||
value: new Uint8Array([3 << 2])
|
||||
}
|
||||
],
|
||||
value: new Uint8Array([2 << 2])
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { AnyJson, Codec, CodecClass, IMap, IU8a, Registry } from '../types';
|
||||
import type { AnyJson, Codec, CodecClass, IMap, Inspect, IU8a, Registry } from '../types';
|
||||
|
||||
import { compactFromU8a, compactToU8a, isHex, isObject, isU8a, logger, stringify, u8aConcat, u8aToHex, u8aToU8a } from '@polkadot/util';
|
||||
|
||||
@@ -125,7 +125,7 @@ export class CodecMap<K extends Codec = Codec, V extends Codec = Codec> extends
|
||||
let len = compactToU8a(this.size).length;
|
||||
|
||||
for (const [k, v] of this.entries()) {
|
||||
len += v.encodedLength + k.encodedLength;
|
||||
len += k.encodedLength + v.encodedLength;
|
||||
}
|
||||
|
||||
return len;
|
||||
@@ -152,6 +152,23 @@ export class CodecMap<K extends Codec = Codec, V extends Codec = Codec> extends
|
||||
return compareMap(this, other);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Returns a breakdown of the hex encoding for this Codec
|
||||
*/
|
||||
inspect (): Inspect {
|
||||
const inner = new Array<Inspect>();
|
||||
|
||||
for (const [k, v] of this.entries()) {
|
||||
inner.push(k.inspect());
|
||||
inner.push(v.inspect());
|
||||
}
|
||||
|
||||
return {
|
||||
inner,
|
||||
value: compactToU8a(this.size)
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Returns a hex string representation of the value. isLe returns a LE (number-only) representation
|
||||
*/
|
||||
|
||||
@@ -28,4 +28,14 @@ describe('Range', (): void => {
|
||||
it('has a sane toRawType representation', (): void => {
|
||||
expect(range.toRawType()).toEqual('Range<u32>');
|
||||
});
|
||||
|
||||
it('has a sane inspect', (): void => {
|
||||
expect(range.inspect()).toEqual({
|
||||
inner: [
|
||||
{ inner: [], value: new Uint8Array([1, 0, 0, 0]) },
|
||||
{ inner: [], value: new Uint8Array([2, 0, 0, 0]) }
|
||||
],
|
||||
value: new Uint8Array()
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -58,9 +58,16 @@ describe('U8aFixed', (): void => {
|
||||
).toEqual(new Uint8Array([1, 2, 3, 4]));
|
||||
});
|
||||
|
||||
it('hash a sane toRawType', (): void => {
|
||||
it('has a sane toRawType', (): void => {
|
||||
expect(u8a.toRawType()).toEqual('[u8;4]');
|
||||
});
|
||||
|
||||
it('has a sane inspect', (): void => {
|
||||
expect(u8a.inspect()).toEqual({
|
||||
inner: [],
|
||||
value: new Uint8Array([1, 2, 3, 4])
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('static with', (): void => {
|
||||
|
||||
@@ -49,4 +49,22 @@ describe('WrapperKeepOpaque', (): void => {
|
||||
|
||||
expect(u32.unwrap().toNumber()).toEqual(1234567);
|
||||
});
|
||||
|
||||
it('has a sane inspect (non-decodable)', (): void => {
|
||||
expect(
|
||||
new ClazzUSize(registry, u8au32).inspect()
|
||||
).toEqual({
|
||||
inner: [{ inner: [], value: new Uint8Array([0x87, 0xd6, 0x12, 0x00]) }],
|
||||
value: new Uint8Array([4 << 2])
|
||||
});
|
||||
});
|
||||
|
||||
it('has a sane inspect (decodable)', (): void => {
|
||||
expect(
|
||||
new ClazzU32(registry, u8au32).inspect()
|
||||
).toEqual({
|
||||
inner: [{ inner: [], value: new Uint8Array([0x87, 0xd6, 0x12, 0x00]) }],
|
||||
value: new Uint8Array([4 << 2])
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
// Copyright 2017-2022 @polkadot/types-codec authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { AnyJson, AnyU8a, Codec, CodecClass, Registry } from '../types';
|
||||
import type { AnyJson, AnyU8a, Codec, CodecClass, Inspect, Registry } from '../types';
|
||||
|
||||
import { assertReturn, compactAddLength, compactStripLength, isHex, isU8a } from '@polkadot/util';
|
||||
import { assertReturn, compactAddLength, compactStripLength, compactToU8a, isHex, isU8a } from '@polkadot/util';
|
||||
|
||||
import { Raw } from '../native/Raw';
|
||||
import { typeToConstructor } from '../utils';
|
||||
@@ -63,6 +63,18 @@ export class WrapperKeepOpaque<T extends Codec> extends Bytes {
|
||||
return !!this.#decoded;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Returns a breakdown of the hex encoding for this Codec
|
||||
*/
|
||||
override inspect (): Inspect {
|
||||
return {
|
||||
inner: this.#decoded
|
||||
? [this.#decoded.inspect()]
|
||||
: [{ inner: [], value: this.toU8a(true) }],
|
||||
value: compactToU8a(this.length)
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
|
||||
*/
|
||||
|
||||
@@ -44,4 +44,13 @@ describe('WrapperOpaque', (): void => {
|
||||
new WrapperOpaque(registry, 'u32').toRawType()
|
||||
).toEqual('WrapperOpaque<u32>');
|
||||
});
|
||||
|
||||
it('has a sane inspect', (): void => {
|
||||
expect(
|
||||
new WrapperOpaque(registry, 'u32', '0x12345678').inspect()
|
||||
).toEqual({
|
||||
inner: [{ inner: [], value: new Uint8Array([0x78, 0x56, 0x34, 0x12]) }],
|
||||
value: new Uint8Array([4 << 2])
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -62,5 +62,12 @@ describe('Bool', (): void => {
|
||||
expect(new Bool(registry, false).isEmpty).toBe(true);
|
||||
expect(new Bool(registry, true).isEmpty).toBe(false);
|
||||
});
|
||||
|
||||
it('has a sane inspect', (): void => {
|
||||
expect(new Bool(registry, true).inspect()).toEqual({
|
||||
inner: [],
|
||||
value: new Uint8Array([1])
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { Codec, IU8a, Registry } from '../types';
|
||||
import type { Codec, Inspect, IU8a, Registry } from '../types';
|
||||
|
||||
import { isU8a, u8aToHex } from '@polkadot/util';
|
||||
|
||||
@@ -81,6 +81,16 @@ export class bool extends Boolean implements Codec {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Returns a breakdown of the hex encoding for this Codec
|
||||
*/
|
||||
inspect (): Inspect {
|
||||
return {
|
||||
inner: [],
|
||||
value: this.toU8a()
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Returns a hex string representation of the value
|
||||
*/
|
||||
|
||||
@@ -73,5 +73,12 @@ describe('Date', (): void => {
|
||||
it('compares values (non-match)', (): void => {
|
||||
expect(new CodecDate(registry, 123).eq(456)).toBe(false);
|
||||
});
|
||||
|
||||
it('has a sane inspect', (): void => {
|
||||
expect(new CodecDate(registry, 3).inspect()).toEqual({
|
||||
inner: [],
|
||||
value: new Uint8Array([3, 0, 0, 0, 0, 0, 0, 0])
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { AnyNumber, INumber, IU8a, Registry, UIntBitLength } from '../types';
|
||||
import type { AnyNumber, Inspect, INumber, IU8a, Registry, UIntBitLength } from '../types';
|
||||
|
||||
import { BN, bnToBn, bnToHex, bnToU8a, isString, isU8a, u8aToBn } from '@polkadot/util';
|
||||
|
||||
@@ -64,6 +64,13 @@ export class CodecDate extends Date implements INumber {
|
||||
return this.getTime() === 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Returns the number of bits in the value
|
||||
*/
|
||||
public bitLength (): UIntBitLength {
|
||||
return BITLENGTH;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Compares the value of the input to see if there is a match
|
||||
*/
|
||||
@@ -72,10 +79,13 @@ export class CodecDate extends Date implements INumber {
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Returns the number of bits in the value
|
||||
* @description Returns a breakdown of the hex encoding for this Codec
|
||||
*/
|
||||
public bitLength (): UIntBitLength {
|
||||
return BITLENGTH;
|
||||
inspect (): Inspect {
|
||||
return {
|
||||
inner: [],
|
||||
value: this.toU8a()
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { AnyJson, Codec, IU8a, Registry } from '../types';
|
||||
import type { AnyJson, Codec, Inspect, IU8a, Registry } from '../types';
|
||||
|
||||
import { isFunction, objectProperties, stringify } from '@polkadot/util';
|
||||
|
||||
@@ -68,6 +68,13 @@ export class Json extends Map<string, any> implements Codec {
|
||||
return this.get(key) as unknown as T;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Unimplemented, will throw
|
||||
*/
|
||||
public inspect (): Inspect {
|
||||
throw new Error('Unimplemented');
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Unimplemented, will throw
|
||||
*/
|
||||
|
||||
@@ -89,5 +89,14 @@ describe('Raw', (): void => {
|
||||
() => new Raw(registry, '0x7f07b1f87709608bee603bbc79a0dfc29cd315c1351a83aa31adf7458d7d3003').toUtf8()
|
||||
).toThrow(/The character sequence is not a valid Utf8 string/);
|
||||
});
|
||||
|
||||
it('has a sane inspect', (): void => {
|
||||
expect(
|
||||
new Raw(registry, [1, 2, 3, 4, 5]).inspect()
|
||||
).toEqual({
|
||||
inner: [],
|
||||
value: new Uint8Array([1, 2, 3, 4, 5])
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { AnyJson, AnyU8a, IU8a, Registry } from '../types';
|
||||
import type { AnyJson, AnyU8a, Inspect, IU8a, Registry } from '../types';
|
||||
|
||||
import { assert, isAscii, isUndefined, isUtf8, u8aToHex, u8aToString, u8aToU8a } from '@polkadot/util';
|
||||
|
||||
@@ -91,6 +91,16 @@ export class Raw extends Uint8Array implements IU8a {
|
||||
return this.eq(u8aToU8a(other as string));
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Returns a breakdown of the hex encoding for this Codec
|
||||
*/
|
||||
inspect (): Inspect {
|
||||
return {
|
||||
inner: [],
|
||||
value: this.toU8a()
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Create a new slice from the actual buffer. (compat)
|
||||
* @param start The position to start at
|
||||
|
||||
@@ -95,6 +95,13 @@ describe('Set', (): void => {
|
||||
set.eq('full')
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
it('has a sane inspect', (): void => {
|
||||
expect(set.inspect()).toEqual({
|
||||
inner: [],
|
||||
value: new Uint8Array([SET_ROLES.full | SET_ROLES.authority])
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('has a sane toRawType representation', (): void => {
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { CodecClass, ISet, IU8a, Registry } from '../types';
|
||||
import type { CodecClass, Inspect, ISet, IU8a, Registry } from '../types';
|
||||
|
||||
import { assert, BN, bnToBn, bnToU8a, isBn, isNumber, isString, isU8a, isUndefined, objectProperties, stringify, stringPascalCase, u8aToBn, u8aToHex, u8aToU8a } from '@polkadot/util';
|
||||
|
||||
@@ -184,6 +184,16 @@ export class CodecSet extends Set<string> implements ISet<string> {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Returns a breakdown of the hex encoding for this Codec
|
||||
*/
|
||||
inspect (): Inspect {
|
||||
return {
|
||||
inner: [],
|
||||
value: this.toU8a()
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Returns a hex string representation of the value
|
||||
*/
|
||||
|
||||
@@ -335,5 +335,35 @@ describe('Struct', (): void => {
|
||||
new Struct(registry, def, val).toU8a({ method: true })
|
||||
).toEqual(new Uint8Array([2 << 2, 0x42, 0x69, 0x99, 1, 1, 0, 0, 0, 1]));
|
||||
});
|
||||
|
||||
it('has a sane inspect', (): void => {
|
||||
expect(
|
||||
new Struct(registry, def, val).inspect()
|
||||
).toEqual({
|
||||
inner: [
|
||||
{
|
||||
inner: [{ inner: [], value: new Uint8Array([0x42, 0x69]) }],
|
||||
name: 'foo',
|
||||
value: new Uint8Array([2 << 2])
|
||||
},
|
||||
{
|
||||
inner: [{ inner: [], value: new Uint8Array([0x99]) }],
|
||||
name: 'method',
|
||||
value: new Uint8Array([1 << 2])
|
||||
},
|
||||
{
|
||||
inner: [{ inner: [], value: new Uint8Array([1, 0, 0, 0]) }],
|
||||
name: 'bar',
|
||||
value: new Uint8Array([1])
|
||||
},
|
||||
{
|
||||
inner: [],
|
||||
name: 'baz',
|
||||
value: new Uint8Array([1])
|
||||
}
|
||||
],
|
||||
value: new Uint8Array()
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { AnyJson, BareOpts, Codec, CodecClass, CodecClassDef, IStruct, IU8a, Registry } from '../types';
|
||||
import type { AnyJson, BareOpts, Codec, CodecClass, CodecClassDef, Inspect, IStruct, IU8a, Registry } from '../types';
|
||||
|
||||
import { assert, isBoolean, isFunction, isHex, isObject, isU8a, isUndefined, objectProperties, stringCamelCase, stringify, u8aConcat, u8aToHex, u8aToU8a } from '@polkadot/util';
|
||||
|
||||
@@ -216,6 +216,22 @@ export class Struct<
|
||||
return this.toArray()[index];
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Returns a breakdown of the hex encoding for this Codec
|
||||
*/
|
||||
inspect (): Inspect {
|
||||
const inner = new Array<Inspect>();
|
||||
|
||||
for (const [k, v] of this.entries()) {
|
||||
inner.push({ ...v.inspect(), name: k.toString() });
|
||||
}
|
||||
|
||||
return {
|
||||
inner,
|
||||
value: new Uint8Array()
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Converts the Object to an standard JavaScript Array
|
||||
*/
|
||||
|
||||
@@ -5,6 +5,7 @@ import type { CodecTo } from '@polkadot/types-codec/types';
|
||||
|
||||
import { TypeRegistry } from '@polkadot/types';
|
||||
import { Bytes, Raw, Text } from '@polkadot/types-codec';
|
||||
import { stringToU8a } from '@polkadot/util';
|
||||
|
||||
describe('Text', (): void => {
|
||||
const registry = new TypeRegistry();
|
||||
@@ -64,5 +65,14 @@ describe('Text', (): void => {
|
||||
expect(test.encodedLength).toEqual(7);
|
||||
expect(test).toHaveLength(2);
|
||||
});
|
||||
|
||||
it('has a snae inspect', (): void => {
|
||||
expect(
|
||||
new Text(registry, 'abcde').inspect()
|
||||
).toEqual({
|
||||
inner: [{ inner: [], value: stringToU8a('abcde') }],
|
||||
value: new Uint8Array([5 << 2])
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { AnyU8a, Codec, IU8a, Registry } from '../types';
|
||||
import type { AnyU8a, Codec, Inspect, IU8a, Registry } from '../types';
|
||||
|
||||
import { assert, compactAddLength, compactFromU8a, hexToU8a, isHex, isString, isU8a, stringToU8a, u8aToHex, u8aToString } from '@polkadot/util';
|
||||
import { assert, compactAddLength, compactFromU8a, compactToU8a, hexToU8a, isHex, isString, isU8a, stringToU8a, u8aToHex, u8aToString } from '@polkadot/util';
|
||||
|
||||
import { Raw } from './Raw';
|
||||
|
||||
@@ -110,6 +110,18 @@ export class Text extends String implements Codec {
|
||||
: false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Returns a breakdown of the hex encoding for this Codec
|
||||
*/
|
||||
inspect (): Inspect {
|
||||
const value = stringToU8a(super.toString());
|
||||
|
||||
return {
|
||||
inner: [{ inner: [], value }],
|
||||
value: compactToU8a(value.length)
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Set an override value for this
|
||||
*/
|
||||
|
||||
@@ -57,6 +57,11 @@ export interface Codec {
|
||||
*/
|
||||
eq (other?: unknown): boolean;
|
||||
|
||||
/**
|
||||
* @description Returns a breakdown of the hex encoding for this Codec
|
||||
*/
|
||||
inspect (): Inspect;
|
||||
|
||||
/**
|
||||
* @description Returns a hex string representation of the value. isLe returns a LE (number-only) representation
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user