Naming changes for #184 (#185)

* Create TypeValue structure

* Cleanup create

* More nesting in types

* Trim types (just in case)

* Export stuff from createType

* TypeValue -> TypeDef
This commit is contained in:
Jaco Greeff
2018-10-02 13:22:09 +02:00
committed by GitHub
parent ae6895954c
commit 29b221b94f
3 changed files with 35 additions and 35 deletions
+16 -16
View File
@@ -2,7 +2,7 @@
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
import { TypeValueInfo, typeSplit, getType, getTypeValue } from './createType';
import { TypeDefInfo, typeSplit, getTypeClass, getTypeDef } from './createType';
describe('typeSplit', () => {
it('splits simple types into an array', () => {
@@ -51,56 +51,56 @@ describe('typeSplit', () => {
describe('getTypeValue', () => {
it('does not allow invalid tuples, end )', () => {
expect(
() => getTypeValue('(u64, u32')
() => getTypeDef('(u64, u32')
).toThrow(/tuple wrapped/);
});
it('does not allow invalid vectors, end >', () => {
expect(
() => getTypeValue('Vec<u64')
() => getTypeDef('Vec<u64')
).toThrow(/Vec wrapped/);
});
it('returns a type structure', () => {
expect(
getTypeValue('(u32, Vec<u64>, (Text, Vec<(Bool, u128)>))')
getTypeDef('(u32, Vec<u64>, (Text, Vec<(Bool, u128)>))')
).toEqual({
info: TypeValueInfo.Tuple,
info: TypeDefInfo.Tuple,
type: '(u32, Vec<u64>, (Text, Vec<(Bool, u128)>))',
sub: [
{
info: TypeValueInfo.Plain,
info: TypeDefInfo.Plain,
type: 'u32'
},
{
info: TypeValueInfo.Vector,
info: TypeDefInfo.Vector,
type: 'Vec<u64>',
sub: {
info: TypeValueInfo.Plain,
info: TypeDefInfo.Plain,
type: 'u64'
}
},
{
info: TypeValueInfo.Tuple,
info: TypeDefInfo.Tuple,
type: '(Text, Vec<(Bool, u128)>)',
sub: [
{
info: TypeValueInfo.Plain,
info: TypeDefInfo.Plain,
type: 'Text'
},
{
info: TypeValueInfo.Vector,
info: TypeDefInfo.Vector,
type: 'Vec<(Bool, u128)>',
sub: {
info: TypeValueInfo.Tuple,
info: TypeDefInfo.Tuple,
type: '(Bool, u128)',
sub: [
{
info: TypeValueInfo.Plain,
info: TypeDefInfo.Plain,
type: 'Bool'
},
{
info: TypeValueInfo.Plain,
info: TypeDefInfo.Plain,
type: 'u128'
}
]
@@ -113,10 +113,10 @@ describe('getTypeValue', () => {
});
});
describe('getType', () => {
describe('getTypeClass', () => {
it('does not allow invalid types', () => {
expect(
() => getType('SomethingInvalid')
() => getTypeClass('SomethingInvalid')
).toThrow(/determine type/);
});
});
+18 -18
View File
@@ -11,16 +11,16 @@ import Vector from './Vector';
type Constructor = { new (value?: any): Base };
export enum TypeValueInfo {
export enum TypeDefInfo {
Plain,
Tuple,
Vector
}
export type TypeValue = {
info: TypeValueInfo,
export type TypeDef = {
info: TypeDefInfo,
type: string,
sub?: TypeValue | Array<TypeValue>
sub?: TypeDef | Array<TypeDef>
};
// safely split a string on ', ' while taking care of any nested occurences
@@ -74,10 +74,10 @@ export function typeSplit (type: string): Array<string> {
return result;
}
export function getTypeValue (_type: string): TypeValue {
export function getTypeDef (_type: string): TypeDef {
const type = _type.trim();
const value: TypeValue = {
info: TypeValueInfo.Plain,
const value: TypeDef = {
info: TypeDefInfo.Plain,
type
};
@@ -87,9 +87,9 @@ export function getTypeValue (_type: string): TypeValue {
// strip wrapping ()'s
const innerTypes = typeSplit(type.substr(1, type.length - 2));
value.info = TypeValueInfo.Tuple;
value.info = TypeDefInfo.Tuple;
value.sub = innerTypes.map((inner) =>
getTypeValue(inner)
getTypeDef(inner)
);
} else if (type.substr(0, 4) === 'Vec<') {
assert(type[type.length - 1] === '>', `Expected Vec wrapped with <>`);
@@ -97,34 +97,34 @@ export function getTypeValue (_type: string): TypeValue {
// strip wrapping Vec<>
const subType = type.substr(4, type.length - 5);
value.info = TypeValueInfo.Vector;
value.sub = getTypeValue(subType);
value.info = TypeDefInfo.Vector;
value.sub = getTypeDef(subType);
}
return value;
}
// Returns the type Class for construction
export function getType (value: TypeValue): Constructor {
if (value.info === TypeValueInfo.Tuple) {
export function getTypeClass (value: TypeDef): Constructor {
if (value.info === TypeDefInfo.Tuple) {
if (!Array.isArray(value.sub)) {
throw new Error(`Expected nested subtypes for Tuple`);
}
return Tuple.with(
value.sub.reduce((result, type, index) => {
result[`entry${index}`] = getType(type);
result[`entry${index}`] = getTypeClass(type);
return result;
}, {} as { [index: string]: Constructor })
);
} else if (value.info === TypeValueInfo.Vector) {
} else if (value.info === TypeDefInfo.Vector) {
if (!value.sub || Array.isArray(value.sub)) {
throw new Error(`Expected subtype for Vector`);
}
return Vector.with(
getType(value.sub)
getTypeClass(value.sub)
);
}
@@ -138,8 +138,8 @@ export function getType (value: TypeValue): Constructor {
export default function createType (type: string, value?: any): Base {
// l.debug(() => ['createType', { type, value }]);
const Type = getType(
getTypeValue(type)
const Type = getTypeClass(
getTypeDef(type)
);
return new Type(value);
+1 -1
View File
@@ -18,4 +18,4 @@ export { default as U8a } from './U8a';
export { default as UInt } from './UInt';
// Type management helper functions
export { default as createType } from './createType';
export { default as createType, getTypeClass, getTypeDef, TypeDef, TypeDefInfo } from './createType';