Cater for nested aliassed types (#2560)
* Cater for nested aliassed types * linting
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
// Copyright 2017-2020 @polkadot/metadata authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { TypeDef, TypeDefInfo } from '@polkadot/types/types';
|
||||
|
||||
import { getTypeDef } from '@polkadot/types/create';
|
||||
|
||||
type Extracted = string | Extracted[];
|
||||
|
||||
/** @internal */
|
||||
export default function extractTypes (types: string[]): Extracted[] {
|
||||
return types.map((type): Extracted => {
|
||||
const decoded = getTypeDef(type);
|
||||
|
||||
switch (decoded.info) {
|
||||
case TypeDefInfo.Plain:
|
||||
return decoded.type;
|
||||
|
||||
case TypeDefInfo.BTreeSet:
|
||||
case TypeDefInfo.Compact:
|
||||
case TypeDefInfo.Option:
|
||||
case TypeDefInfo.Vec:
|
||||
case TypeDefInfo.VecFixed:
|
||||
return extractTypes([(decoded.sub as TypeDef).type]);
|
||||
|
||||
case TypeDefInfo.BTreeMap:
|
||||
case TypeDefInfo.HashMap:
|
||||
case TypeDefInfo.Result:
|
||||
case TypeDefInfo.Tuple:
|
||||
return extractTypes((decoded.sub as TypeDef[]).map((sub): string => sub.type));
|
||||
|
||||
default:
|
||||
throw new Error(`Unhandled: Unable to create and validate type from ${type}`);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -2,42 +2,11 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { Registry, TypeDef, TypeDefInfo } from '@polkadot/types/types';
|
||||
|
||||
import { getTypeDef } from '@polkadot/types/create';
|
||||
import { Registry } from '@polkadot/types/types';
|
||||
|
||||
import extractTypes from './extractTypes';
|
||||
import flattenUniq from './flattenUniq';
|
||||
|
||||
type Extracted = string | Extracted[];
|
||||
|
||||
/** @internal */
|
||||
function extractTypes (types: string[]): Extracted[] {
|
||||
return types.map((type): Extracted => {
|
||||
const decoded = getTypeDef(type);
|
||||
|
||||
switch (decoded.info) {
|
||||
case TypeDefInfo.Plain:
|
||||
return decoded.type;
|
||||
|
||||
case TypeDefInfo.BTreeSet:
|
||||
case TypeDefInfo.Compact:
|
||||
case TypeDefInfo.Option:
|
||||
case TypeDefInfo.Vec:
|
||||
case TypeDefInfo.VecFixed:
|
||||
return extractTypes([(decoded.sub as TypeDef).type]);
|
||||
|
||||
case TypeDefInfo.BTreeMap:
|
||||
case TypeDefInfo.HashMap:
|
||||
case TypeDefInfo.Result:
|
||||
case TypeDefInfo.Tuple:
|
||||
return extractTypes((decoded.sub as TypeDef[]).map((sub): string => sub.type));
|
||||
|
||||
default:
|
||||
throw new Error(`Unhandled: Unable to create and validate type from ${type}`);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
export default function validateTypes (registry: Registry, types: string[], throwError: boolean): void {
|
||||
const missing = flattenUniq(extractTypes(types)).filter((type) => !registry.hasType(type));
|
||||
|
||||
@@ -10780,7 +10780,7 @@
|
||||
"Map": {
|
||||
"hasher": "Twox64Concat",
|
||||
"key": "AccountId",
|
||||
"value": "(Vec<Announcement>,BalanceOf)",
|
||||
"value": "(Vec<ProxyAnnouncement>,BalanceOf)",
|
||||
"linked": false
|
||||
}
|
||||
},
|
||||
|
||||
@@ -16,10 +16,20 @@ import { stringCamelCase } from '@polkadot/util';
|
||||
* @internal
|
||||
**/
|
||||
function setTypeOverride (sectionTypes: OverrideModuleType, type: Type): void {
|
||||
const override = Object.keys(sectionTypes).find((aliased): boolean => type.eq(aliased));
|
||||
const override = Object.keys(sectionTypes).find((aliased) => type.eq(aliased));
|
||||
|
||||
if (override) {
|
||||
type.setOverride(sectionTypes[override]);
|
||||
} else {
|
||||
// FIXME: NOT happy with this approach, but gets over the initial hump cased by (Vec<Announcement>,BalanceOf)
|
||||
const orig = type.toString();
|
||||
const alias = Object.entries(sectionTypes).reduce((result: string, [from, to]): string =>
|
||||
[['<', '>'], ['<', ','], [',', '>'], ['(', ')'], ['(', ','], [',', ','], [',', ')']].reduce((result, [one, two]): string =>
|
||||
result.replace(`${one}${from}${two}`, `${one}${to}${two}`), result), orig);
|
||||
|
||||
if (orig !== alias) {
|
||||
type.setOverride(alias);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,7 +39,7 @@ function setTypeOverride (sectionTypes: OverrideModuleType, type: Type): void {
|
||||
**/
|
||||
function convertCalls (registry: Registry, calls: FunctionMetadataV11[], sectionTypes: OverrideModuleType): FunctionMetadataLatest[] {
|
||||
return calls.map(({ args, documentation, name }): FunctionMetadataLatest => {
|
||||
args.forEach(({ type }): void => setTypeOverride(sectionTypes, type));
|
||||
args.forEach(({ type }) => setTypeOverride(sectionTypes, type));
|
||||
|
||||
return registry.createType('FunctionMetadataLatest', { args, documentation, name });
|
||||
});
|
||||
@@ -41,7 +51,7 @@ function convertCalls (registry: Registry, calls: FunctionMetadataV11[], section
|
||||
**/
|
||||
function convertEvents (registry: Registry, events: EventMetadataV11[], sectionTypes: OverrideModuleType): EventMetadataLatest[] {
|
||||
return events.map(({ args, documentation, name }): EventMetadataLatest => {
|
||||
args.forEach((type): void => setTypeOverride(sectionTypes, type));
|
||||
args.forEach((type) => setTypeOverride(sectionTypes, type));
|
||||
|
||||
return registry.createType('EventMetadataLatest', { args, documentation, name });
|
||||
});
|
||||
|
||||
@@ -26,6 +26,14 @@ describe('sanitize', (): void => {
|
||||
'Text Text (Text,[Text;32],Text)"Text<Text>'
|
||||
);
|
||||
});
|
||||
|
||||
it('handles emdedded Vec/Tuples', (): void => {
|
||||
const ann = alias(['Announcement'], 'ProxyAnnouncement');
|
||||
|
||||
expect(ann('(Vec<Announcement>,BalanceOf)')).toEqual(
|
||||
'(Vec<ProxyAnnouncement>,BalanceOf)'
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('removeColons', (): void => {
|
||||
|
||||
@@ -22,8 +22,6 @@ export default {
|
||||
real: 'AccountId',
|
||||
callHash: 'Hash',
|
||||
height: 'BlockNumber'
|
||||
},
|
||||
// FIXME We don't want this here
|
||||
Announcement: 'ProxyAnnouncement'
|
||||
}
|
||||
}
|
||||
} as Definitions;
|
||||
|
||||
Reference in New Issue
Block a user