Detect resulting types from createType (#3849)
* Detect resulting types from createType * Additional detect overrides * additional * Empy (), never for invalids
This commit is contained in:
@@ -8,7 +8,7 @@ import { TypeRegistry } from '@polkadot/types/create';
|
||||
import * as defaultDefinitions from '@polkadot/types/interfaces/definitions';
|
||||
import * as defaultPrimitives from '@polkadot/types/primitive';
|
||||
|
||||
import { createImports, getDerivedTypes, readTemplate, setImports, writeFile } from '../util';
|
||||
import { createImports, readTemplate, setImports, writeFile } from '../util';
|
||||
import { ModuleTypes } from '../util/imports';
|
||||
|
||||
const primitiveClasses = {
|
||||
@@ -35,10 +35,10 @@ export function generateInterfaceTypes (importDefinitions: { [importPath: string
|
||||
Object
|
||||
.keys(primitiveClasses)
|
||||
.filter((name) => !name.includes('Generic'))
|
||||
.forEach((primitiveName) => {
|
||||
.forEach((primitiveName): void => {
|
||||
setImports(definitions, imports, [primitiveName]);
|
||||
|
||||
items.push(...getDerivedTypes(registry, definitions, primitiveName, primitiveName, imports));
|
||||
items.push(primitiveName);
|
||||
});
|
||||
|
||||
const existingTypes: Record<string, boolean> = {};
|
||||
@@ -55,10 +55,10 @@ export function generateInterfaceTypes (importDefinitions: { [importPath: string
|
||||
|
||||
const uniqueTypes = Object.keys(types).filter((type) => !existingTypes[type]);
|
||||
|
||||
uniqueTypes.forEach((type) => {
|
||||
uniqueTypes.forEach((type): void => {
|
||||
existingTypes[type] = true;
|
||||
|
||||
items.push(...getDerivedTypes(registry, definitions, type, types[type] as string, imports));
|
||||
items.push(type);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
declare module '@polkadot/types/types/registry' {
|
||||
export interface InterfaceTypes {
|
||||
{{#each items}}
|
||||
{{{this}}}
|
||||
{{{this}}}: {{{this}}};
|
||||
{{/each}}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
// Copyright 2017-2021 @polkadot/typegen authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { Constructor } from '@polkadot/types/types';
|
||||
|
||||
import { UInt } from '@polkadot/types/codec';
|
||||
import { isChildClass } from '@polkadot/util';
|
||||
|
||||
/** @internal */
|
||||
export function isCompactEncodable (Child: Constructor<any>): boolean {
|
||||
return isChildClass(UInt, Child);
|
||||
}
|
||||
@@ -11,10 +11,8 @@ import { TypeDefInfo } from '@polkadot/types/create/types';
|
||||
import { GenericAccountId, GenericLookupSource, GenericVote } from '@polkadot/types/generic';
|
||||
import { AllConvictions } from '@polkadot/types/interfaces/democracy/definitions';
|
||||
import { bool, Null } from '@polkadot/types/primitive';
|
||||
import * as primitiveClasses from '@polkadot/types/primitive';
|
||||
import { isChildClass, stringify } from '@polkadot/util';
|
||||
|
||||
import { isCompactEncodable } from './class';
|
||||
import { formatType } from './formatting';
|
||||
import { ModuleTypes, setImports, TypeImports } from './imports';
|
||||
|
||||
@@ -24,46 +22,6 @@ function arrayToStrType (arr: string[]): string {
|
||||
|
||||
const voteConvictions = arrayToStrType(AllConvictions);
|
||||
|
||||
// From `T`, generate `Compact<T>, Option<T>, Vec<T>`
|
||||
/** @internal */
|
||||
export function getDerivedTypes (registry: Registry, definitions: Record<string, ModuleTypes>, type: string, primitiveName: string, imports: TypeImports): string[] {
|
||||
// `primitiveName` represents the actual primitive type our type is mapped to
|
||||
const isCompact = isCompactEncodable(
|
||||
(primitiveClasses as Record<string, any>)[primitiveName] ||
|
||||
registry.createClass(type)
|
||||
);
|
||||
const def = getTypeDef(type);
|
||||
|
||||
setImports(definitions, imports, ['Option', 'Vec', isCompact ? 'Compact' : '']);
|
||||
|
||||
const types = [
|
||||
{
|
||||
info: TypeDefInfo.Option,
|
||||
sub: def,
|
||||
type
|
||||
},
|
||||
{
|
||||
info: TypeDefInfo.Vec,
|
||||
sub: def,
|
||||
type
|
||||
}
|
||||
];
|
||||
|
||||
if (isCompact) {
|
||||
types.unshift({
|
||||
info: TypeDefInfo.Compact,
|
||||
sub: def,
|
||||
type
|
||||
});
|
||||
}
|
||||
|
||||
const result = types.map((t) => formatType(registry, definitions, t, imports)).map((t) => `'${t}': ${t};`);
|
||||
|
||||
result.unshift(`${type}: ${type};`);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// Make types a little bit more flexible
|
||||
// - if param instanceof AbstractInt, then param: u64 | Uint8array | AnyNumber
|
||||
// etc
|
||||
|
||||
Reference in New Issue
Block a user