From edfd62558f26c22310465906141ac482400fe11e Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Tue, 18 Dec 2018 16:26:42 +0100 Subject: [PATCH] Allow creating Struct from JSON (#489) * Allow creating Struct from JSON * Fix Struct import --- packages/types/src/codec/createType.spec.ts | 56 +++++++++++++++++++-- packages/types/src/codec/createType.ts | 47 +++++++++++++---- 2 files changed, 89 insertions(+), 14 deletions(-) diff --git a/packages/types/src/codec/createType.spec.ts b/packages/types/src/codec/createType.spec.ts index 1ad51ce02c..6b6b8ad30a 100644 --- a/packages/types/src/codec/createType.spec.ts +++ b/packages/types/src/codec/createType.spec.ts @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import { TypeDefInfo, typeSplit, getTypeClass, getTypeDef } from './createType'; +import createType, { TypeDefInfo, getTypeClass, getTypeDef, typeSplit } from './createType'; describe('typeSplit', () => { it('splits simple types into an array', () => { @@ -38,13 +38,13 @@ describe('typeSplit', () => { it('checks for unclosed vec', () => { expect( () => typeSplit('Text, Vec { expect( () => typeSplit('Text, (u64, u32') - ).toThrow(/Invalid Tuple/); + ).toThrow(/Invalid defintion/); }); }); @@ -154,6 +154,42 @@ describe('getTypeValue', () => { } }); }); + + it('returns an actual Struct', () => { + expect( + getTypeDef('{"balance":"Balance","account_id":"AccountId","log":"(u64, Signature)"}') + ).toEqual({ + info: TypeDefInfo.Struct, + type: '{"balance":"Balance","account_id":"AccountId","log":"(u64, Signature)"}', + sub: [ + { + info: TypeDefInfo.Plain, + name: 'balance', + type: 'Balance' + }, + { + info: TypeDefInfo.Plain, + name: 'account_id', + type: 'AccountId' + }, + { + info: TypeDefInfo.Tuple, + name: 'log', + type: '(u64, Signature)', + sub: [ + { + info: TypeDefInfo.Plain, + type: 'u64' + }, + { + info: TypeDefInfo.Plain, + type: 'Signature' + } + ] + } + ] + }); + }); }); describe('getTypeClass', () => { @@ -163,3 +199,17 @@ describe('getTypeClass', () => { ).toThrow(/determine type/); }); }); + +describe('createType', () => { + it('allows creation of a Struct', () => { + expect( + createType('{"balance":"Balance","index":"u32"}', { + balance: 1234, + index: '0x10' + }).toJSON() + ).toEqual({ + balance: '0x000000000000000000000000000004d2', + index: 16 + }); + }); +}); diff --git a/packages/types/src/codec/createType.ts b/packages/types/src/codec/createType.ts index 16eade10c4..3527406c7a 100644 --- a/packages/types/src/codec/createType.ts +++ b/packages/types/src/codec/createType.ts @@ -8,6 +8,7 @@ import { Codec, Constructor } from '../types'; import Text from '../Text'; import Compact from './Compact'; import Option from './Option'; +import Struct from './Struct'; import Tuple from './Tuple'; import UInt from './UInt'; import Vector from './Vector'; @@ -17,18 +18,21 @@ export enum TypeDefInfo { Compact, Option, Plain, + Struct, Tuple, Vector } export type TypeDef = { info: TypeDefInfo, + name?: string, type: string, sub?: TypeDef | Array }; // safely split a string on ', ' while taking care of any nested occurences export function typeSplit (type: string): Array { + let sDepth = 0; let tDepth = 0; let vDepth = 0; let start = 0; @@ -38,12 +42,18 @@ export function typeSplit (type: string): Array { switch (type[index]) { case ',': // we are not nested, add the type - if (tDepth === 0 && vDepth === 0) { + if (sDepth === 0 && tDepth === 0 && vDepth === 0) { result.push(type.substr(start, index - start).trim()); start = index + 1; } break; + // inc struct depth, start found + case '{': sDepth++; break; + + // dec struct depth, end found + case '}': sDepth--; break; + // inc tuple depth, start found case '(': tDepth++; break; @@ -61,8 +71,7 @@ export function typeSplit (type: string): Array { } } - assert(!tDepth, `Invalid Tuple in ${type}`); - assert(!vDepth, `Invalid Compact/Vector in ${type}`); + assert(!sDepth && !tDepth && !vDepth, `Invalid defintion (missing terminators) found in ${type}`); // the final leg of the journey result.push(type.substr(start, type.length - start).trim()); @@ -70,10 +79,11 @@ export function typeSplit (type: string): Array { return result; } -export function getTypeDef (_type: Text | string): TypeDef { +export function getTypeDef (_type: Text | string, name?: string): TypeDef { const type = _type.toString().trim(); const value: TypeDef = { info: TypeDefInfo.Plain, + name, type }; let subType = ''; @@ -93,6 +103,11 @@ export function getTypeDef (_type: Text | string): TypeDef { if (startingWith(type, '(', ')')) { value.info = TypeDefInfo.Tuple; value.sub = typeSplit(subType).map((inner) => getTypeDef(inner)); + } else if (startingWith(type, '{', '}')) { + const parsed = JSON.parse(type); + + value.info = TypeDefInfo.Struct; + value.sub = Object.keys(parsed).map((name) => getTypeDef(parsed[name], name)); } else if (startingWith(type, 'Compact<', '>')) { value.info = TypeDefInfo.Compact; value.sub = getTypeDef(subType); @@ -109,13 +124,7 @@ export function getTypeDef (_type: Text | string): TypeDef { // Returns the type Class for construction export function getTypeClass (value: TypeDef): Constructor { - if (value.info === TypeDefInfo.Tuple) { - assert(Array.isArray(value.sub), 'Expected nested subtypes for Tuple'); - - return Tuple.with( - (value.sub as Array).map(getTypeClass) - ); - } else if (value.info === TypeDefInfo.Compact) { + if (value.info === TypeDefInfo.Compact) { assert(value.sub && !Array.isArray(value.sub), 'Expected subtype for Compact'); return Compact.with( @@ -127,6 +136,22 @@ export function getTypeClass (value: TypeDef): Constructor { return Option.with( getTypeClass(value.sub as TypeDef) ); + } else if (value.info === TypeDefInfo.Struct) { + assert(Array.isArray(value.sub), 'Expected nested subtypes for Struct'); + + return Struct.with( + (value.sub as Array).reduce((result, sub) => { + result[sub.name as string] = getTypeClass(sub); + + return result; + }, {} as { [index: string]: Constructor }) + ); + } else if (value.info === TypeDefInfo.Tuple) { + assert(Array.isArray(value.sub), 'Expected nested subtypes for Tuple'); + + return Tuple.with( + (value.sub as Array).map(getTypeClass) + ); } else if (value.info === TypeDefInfo.Vector) { assert(value.sub && !Array.isArray(value.sub), 'Expected subtype for Vector');