Align struct .entries() looping between functions (#5433)

This commit is contained in:
Jaco
2023-01-20 17:30:16 +02:00
committed by GitHub
parent a7f96a19ca
commit f270fe693b
+13 -21
View File
@@ -4,7 +4,7 @@
import type { HexString } from '@polkadot/util/types';
import type { AnyJson, BareOpts, Codec, CodecClass, Inspect, IStruct, IU8a, Registry } from '../types';
import { isBoolean, isFunction, isHex, isObject, isU8a, isUndefined, objectProperties, stringCamelCase, stringify, u8aConcatStrict, u8aToHex, u8aToU8a } from '@polkadot/util';
import { isBoolean, isHex, isObject, isU8a, isUndefined, objectProperties, stringCamelCase, stringify, u8aConcatStrict, u8aToHex, u8aToU8a } from '@polkadot/util';
import { compareMap, decodeU8aStruct, mapToTypeMap, typesToMap } from '../utils';
@@ -276,7 +276,7 @@ export class Struct<
const json: Record<string, AnyJson> = {};
for (const [k, v] of this.entries()) {
json[k as string] = v && v.toHuman(isExtended);
json[k as string] = v.toHuman(isExtended);
}
return json;
@@ -289,11 +289,9 @@ export class Struct<
const json: Record<string, AnyJson> = {};
for (const [k, v] of this.entries()) {
const jsonKey = this.#jsonMap.get(k) || k;
// We actually log inside the U8a decoding and use JSON.stringify(...), which
// means that the Vec may be partially populated (same applies to toHuman, same check)
json[jsonKey as string] = v && v.toJSON();
// Here we pull out the entry against the JSON mapping (if supplied)
// since this representation goes over RPC and needs to be correct
json[(this.#jsonMap.get(k) || k) as string] = v.toJSON();
}
return json;
@@ -306,11 +304,7 @@ export class Struct<
const json: Record<string, AnyJson> = {};
for (const [k, v] of this.entries()) {
const jsonKey = this.#jsonMap.get(k) || k;
// We actually log inside the U8a decoding and use JSON.stringify(...), which
// means that the Vec may be partially populated (same applies to toHuman, same check)
json[jsonKey as string] = v && v.toPrimitive();
json[k as string] = v.toPrimitive();
}
return json;
@@ -338,15 +332,13 @@ export class Struct<
const encoded: Uint8Array[] = [];
for (const [k, v] of this.entries()) {
if (v && isFunction(v.toU8a)) {
encoded.push(
v.toU8a(
!isBare || isBoolean(isBare)
? isBare
: isBare[k]
)
);
}
encoded.push(
v.toU8a(
!isBare || isBoolean(isBare)
? isBare
: isBare[k]
)
);
}
return u8aConcatStrict(encoded);