Fix Struct decoding for null (#4112)

* Fix Struct decoding for null

* CHANGELOG

* 6.5.2
This commit is contained in:
Jaco
2021-10-26 08:46:03 +03:00
committed by GitHub
parent 9ae87bed78
commit 12a54efde5
14 changed files with 84 additions and 91 deletions
+3 -4
View File
@@ -10,7 +10,6 @@
"license": "Apache-2.0",
"maintainers": [],
"name": "@polkadot/types",
"private": false,
"repository": {
"directory": "packages/types",
"type": "git",
@@ -21,7 +20,7 @@
"./detectPackage.cjs"
],
"type": "module",
"version": "6.5.2-3",
"version": "6.5.2",
"main": "index.js",
"dependencies": {
"@babel/runtime": "^7.15.4",
@@ -31,8 +30,8 @@
},
"devDependencies": {
"@polkadot/keyring": "^7.6.1",
"@polkadot/types-support": "6.5.2-3",
"@polkadot/types-support": "6.5.2",
"@types/bn.js": "^4.11.6",
"bn.js": "^4.12.0"
}
}
}
+10 -9
View File
@@ -52,15 +52,16 @@ describe('Struct', (): void => {
testEncode('toString', '{"foo":"bazzing","bar":69}');
});
it('decodes null', (): void => {
expect(
new (
Struct.with({
txt: Text,
u32: U32
})
)(registry, null).toString()
).toEqual('{}');
it('decodes null/undefined/empty correctly (& equivalently)', (): void => {
const Clazz = Struct.with({
txt: Text,
u32: U32
});
const expected = { txt: '', u32: '0' };
expect(new Clazz(registry, {}).toHuman()).toEqual(expected);
expect(new Clazz(registry, null).toHuman()).toEqual(expected);
expect(new Clazz(registry, undefined).toHuman()).toEqual(expected);
});
it('decodes reusing instantiated inputs', (): void => {
+2 -4
View File
@@ -113,12 +113,10 @@ function decodeStruct <T> (registry: Registry, Types: ConstructorDef, value: unk
return raw;
}, {} as T);
} else if (!value) {
return {} as T;
}
// We assume from here that value is a JS object (Array, Map, Object)
return decodeStructFromObject(registry, Types, value, jsonMap);
return decodeStructFromObject(registry, Types, value || {}, jsonMap);
}
/**
@@ -147,7 +145,7 @@ export class Struct<
readonly #Types: ConstructorDef;
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
constructor (registry: Registry, Types: S, value: V | Map<unknown, unknown> | unknown[] | string = {} as V, jsonMap: Map<keyof S, string> = new Map()) {
constructor (registry: Registry, Types: S, value?: V | Map<unknown, unknown> | unknown[] | string | null, jsonMap: Map<keyof S, string> = new Map()) {
super(Object.entries(decodeStruct(registry, mapToTypeMap(registry, Types), value, jsonMap)));
this.registry = registry;