Fix vec<u8> issue (#1591)

* fix vec u8 issue

* lint
This commit is contained in:
Keith Ingram
2019-11-19 21:41:15 +01:00
committed by Jaco Greeff
parent a3ae4cf5d2
commit d82bb63acb
+23 -11
View File
@@ -8,7 +8,7 @@ import { assert } from '@polkadot/util';
import { getTypeDef } from '../create';
const SPECIAL_TYPES = ['AccountId', 'AccountIndex', 'Address', 'Balance'];
export const SPECIAL_TYPES = ['AccountId', 'AccountIndex', 'Address', 'Balance'];
const identity = (value: string): string => value;
@@ -23,22 +23,32 @@ export function paramsNotation (outer: string, inner?: string | any[], transform
}
function encodeWithParams (typeDef: Pick<TypeDef, any>, outer = typeDef.displayName || typeDef.type): string {
const { params } = typeDef;
const { sub, params } = typeDef;
return paramsNotation(
outer,
params,
params || sub,
// eslint-disable-next-line @typescript-eslint/no-use-before-define
(param: TypeDef) => displayType(param)
);
}
function encodeSubTypes (sub: TypeDef[], asEnum?: boolean): string {
return `{ ${asEnum ? '"_enum": { ' : ''}${
sub
.map((type: TypeDef): string => `"${type.name}": "${encodeWithParams(type)}"`)
.join(', ')
}${asEnum ? '" }"' : ''} }`;
const inner = sub.reduce(
(result: Record<string, string>, type: TypeDef): Record<string, string> => {
return {
...result,
[type.name as string]: encodeWithParams(type)
};
},
{}
);
return JSON.stringify(
asEnum
? { _enum: inner }
: inner
);
}
function encodeEnum (typeDef: Pick<TypeDef, any>): string {
@@ -138,10 +148,12 @@ export function displayType (typeDef: Pick<TypeDef, any>): string {
}
export function withTypeString (typeDef: Pick<TypeDef, any>): Pick<TypeDef, any> {
const type = SPECIAL_TYPES.includes(typeDef.displayName)
? typeDef.displayName
: encodeType(typeDef);
return {
...typeDef,
type: SPECIAL_TYPES.includes(typeDef.name)
? typeDef.name
: encodeType(typeDef)
type
};
}