Allow Struct to take Map (and Struct) in constructor (#464)
* Allow Struct to take Map (and Struct) in constructor * First do key mapping, then reduce * Remove extra type refs * Revert "cleanups" - KISS
This commit is contained in:
@@ -66,6 +66,19 @@ describe('Struct', () => {
|
||||
expect(s.toString()).toBe('{"foo":[{"bar":"1"},{"bar":"2"}]}');
|
||||
});
|
||||
|
||||
it('decodes from a Map input', () => {
|
||||
const input = new Struct({
|
||||
a: U32,
|
||||
txt: Text
|
||||
}, { a: 42, txt: 'fubar' });
|
||||
const s = new Struct({
|
||||
txt: Text,
|
||||
foo: U32,
|
||||
bar: U32
|
||||
}, input);
|
||||
expect(s.toString()).toEqual('{"txt":"fubar","foo":0,"bar":0}');
|
||||
});
|
||||
|
||||
it('throws when it cannot decode', () => {
|
||||
expect(
|
||||
() => new (
|
||||
|
||||
@@ -29,7 +29,7 @@ export default class Struct<
|
||||
protected _jsonMap: Map<keyof S, string>;
|
||||
protected _Types: E;
|
||||
|
||||
constructor (Types: S, value: V | Array<any> = {} as V, jsonMap: Map<keyof S, string> = new Map()) {
|
||||
constructor (Types: S, value: V | Map<any, any> | Array<any> = {} as V, jsonMap: Map<keyof S, string> = new Map()) {
|
||||
const decoded = Struct.decodeStruct<S, V, T>(Types, value, jsonMap);
|
||||
|
||||
super(
|
||||
@@ -74,6 +74,14 @@ export default class Struct<
|
||||
return {} as T;
|
||||
}
|
||||
|
||||
return Struct.decodeStructFromObject(Types, value, jsonMap);
|
||||
}
|
||||
|
||||
private static decodeStructFromObject<
|
||||
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;
|
||||
@@ -92,6 +100,12 @@ export default class Struct<
|
||||
raw[key] = value[index] instanceof Types[key]
|
||||
? value[index]
|
||||
: new Types[key](value[index]);
|
||||
} else if (value instanceof Map) {
|
||||
const mapped = value.get(jsonKey);
|
||||
|
||||
raw[key] = mapped instanceof Types[key]
|
||||
? mapped
|
||||
: new Types[key](mapped);
|
||||
} else if (isObject(value)) {
|
||||
raw[key] = value[jsonKey as string] instanceof Types[key]
|
||||
? value[jsonKey as string]
|
||||
|
||||
Reference in New Issue
Block a user