Augment Registry (w/o CodecRegistry & CreateRegistry) (#4463)

* Augment Registry (w/o CodecRegistry & CreateRegistry)

* Build fixes

* Final build fixes

* licence headers
This commit is contained in:
Jaco
2022-01-18 12:52:01 +02:00
committed by GitHub
parent f8c2701751
commit 500e2022c3
112 changed files with 570 additions and 537 deletions
+2 -2
View File
@@ -219,8 +219,8 @@ function generateLookup (destDir: string, entries: [string | undefined, HexStrin
const { lookup, registry } = initMeta(staticMeta).metadata.asLatest;
const filtered = getFilteredTypes(lookup, exclude);
generateLookupDefs(registry as Registry, filtered, destDir, subPath);
generateLookupTypes(registry as Registry, filtered, destDir, subPath);
generateLookupDefs(registry, filtered, destDir, subPath);
generateLookupTypes(registry, filtered, destDir, subPath);
return exclude.concat(
...filtered
+27 -28
View File
@@ -4,7 +4,6 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import type { Registry } from '@polkadot/types/types';
import type { CodecRegistry } from '@polkadot/types-codec/types';
import type { TypeDef } from '@polkadot/types-create/types';
import Handlebars from 'handlebars';
@@ -109,7 +108,7 @@ export function exportInterface (lookupIndex = -1, name = '', base: string, body
return `${doc}export interface ${name} extends ${base} {${body.length ? '\n' : ''}${body}}`;
}
function singleParamNotation (registry: CodecRegistry, wrapper: string, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean): string {
function singleParamNotation (registry: Registry, wrapper: string, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean): string {
const sub = (typeDef.sub as TypeDef);
setImports(definitions, imports, [wrapper, sub.lookupName]);
@@ -117,7 +116,7 @@ function singleParamNotation (registry: CodecRegistry, wrapper: string, typeDef:
return paramsNotation(wrapper, sub.lookupName || formatType(registry, definitions, sub.type, imports, withShortcut));
}
function dualParamsNotation (registry: CodecRegistry, wrapper: string, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean): string {
function dualParamsNotation (registry: Registry, wrapper: string, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean): string {
const [a, b] = (typeDef.sub as TypeDef[]);
setImports(definitions, imports, [wrapper, a.lookupName, b.lookupName]);
@@ -128,18 +127,18 @@ function dualParamsNotation (registry: CodecRegistry, wrapper: string, typeDef:
]);
}
const formatters: Record<TypeDefInfo, (registry: CodecRegistry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => string> = {
[TypeDefInfo.Compact]: (registry: CodecRegistry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
const formatters: Record<TypeDefInfo, (registry: Registry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => string> = {
[TypeDefInfo.Compact]: (registry: Registry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
return singleParamNotation(registry, 'Compact', typeDef, definitions, imports, withShortcut);
},
[TypeDefInfo.DoNotConstruct]: (registry: CodecRegistry, { lookupName }: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
[TypeDefInfo.DoNotConstruct]: (registry: Registry, { lookupName }: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
setImports(definitions, imports, ['DoNotConstruct']);
return 'DoNotConstruct';
},
[TypeDefInfo.Enum]: (registry: CodecRegistry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
[TypeDefInfo.Enum]: (registry: Registry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
if (typeDef.lookupName) {
return typeDef.lookupName;
}
@@ -147,43 +146,43 @@ const formatters: Record<TypeDefInfo, (registry: CodecRegistry, typeDef: TypeDef
throw new Error(`TypeDefInfo.Enum: Not implemented on ${stringify(typeDef)}`);
},
[TypeDefInfo.Int]: (registry: CodecRegistry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
[TypeDefInfo.Int]: (registry: Registry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
throw new Error(`TypeDefInfo.Int: Not implemented on ${stringify(typeDef)}`);
},
[TypeDefInfo.UInt]: (registry: CodecRegistry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
[TypeDefInfo.UInt]: (registry: Registry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
throw new Error(`TypeDefInfo.UInt: Not implemented on ${stringify(typeDef)}`);
},
[TypeDefInfo.Null]: (registry: CodecRegistry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
[TypeDefInfo.Null]: (registry: Registry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
setImports(definitions, imports, ['Null']);
return 'Null';
},
[TypeDefInfo.Option]: (registry: CodecRegistry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
[TypeDefInfo.Option]: (registry: Registry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
return singleParamNotation(registry, 'Option', typeDef, definitions, imports, withShortcut);
},
[TypeDefInfo.Plain]: (registry: CodecRegistry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
[TypeDefInfo.Plain]: (registry: Registry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
setImports(definitions, imports, [typeDef.type]);
return typeDef.type;
},
[TypeDefInfo.Range]: (registry: CodecRegistry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
[TypeDefInfo.Range]: (registry: Registry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
throw new Error(`TypeDefInfo.Range: Not implemented on ${stringify(typeDef)}`);
},
[TypeDefInfo.Set]: (registry: CodecRegistry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
[TypeDefInfo.Set]: (registry: Registry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
throw new Error(`TypeDefInfo.Set: Not implemented on ${stringify(typeDef)}`);
},
[TypeDefInfo.Si]: (registry: CodecRegistry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
return formatType(registry, definitions, (registry as Registry).lookup.getTypeDef(typeDef.type), imports, withShortcut);
[TypeDefInfo.Si]: (registry: Registry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
return formatType(registry, definitions, (registry).lookup.getTypeDef(typeDef.type), imports, withShortcut);
},
[TypeDefInfo.Struct]: (registry: CodecRegistry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
[TypeDefInfo.Struct]: (registry: Registry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
if (typeDef.lookupName) {
return typeDef.lookupName;
}
@@ -200,7 +199,7 @@ const formatters: Record<TypeDefInfo, (registry: CodecRegistry, typeDef: TypeDef
}${withShortcut ? ' ' : '\n '}} & Struct`;
},
[TypeDefInfo.Tuple]: (registry: CodecRegistry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
[TypeDefInfo.Tuple]: (registry: Registry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
const sub = typeDef.sub as TypeDef[];
setImports(definitions, imports, ['ITuple', ...sub.map(({ lookupName }) => lookupName)]);
@@ -213,11 +212,11 @@ const formatters: Record<TypeDefInfo, (registry: CodecRegistry, typeDef: TypeDef
}]`);
},
[TypeDefInfo.Vec]: (registry: CodecRegistry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
[TypeDefInfo.Vec]: (registry: Registry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
return singleParamNotation(registry, 'Vec', typeDef, definitions, imports, withShortcut);
},
[TypeDefInfo.VecFixed]: (registry: CodecRegistry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
[TypeDefInfo.VecFixed]: (registry: Registry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
const sub = (typeDef.sub as TypeDef);
if (sub.type === 'u8') {
@@ -229,31 +228,31 @@ const formatters: Record<TypeDefInfo, (registry: CodecRegistry, typeDef: TypeDef
return singleParamNotation(registry, 'Vec', typeDef, definitions, imports, withShortcut);
},
[TypeDefInfo.BTreeMap]: (registry: CodecRegistry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
[TypeDefInfo.BTreeMap]: (registry: Registry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
return dualParamsNotation(registry, 'BTreeMap', typeDef, definitions, imports, withShortcut);
},
[TypeDefInfo.BTreeSet]: (registry: CodecRegistry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
[TypeDefInfo.BTreeSet]: (registry: Registry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
return singleParamNotation(registry, 'BTreeSet', typeDef, definitions, imports, withShortcut);
},
[TypeDefInfo.HashMap]: (registry: CodecRegistry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
[TypeDefInfo.HashMap]: (registry: Registry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
return dualParamsNotation(registry, 'HashMap', typeDef, definitions, imports, withShortcut);
},
[TypeDefInfo.Linkage]: (registry: CodecRegistry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
[TypeDefInfo.Linkage]: (registry: Registry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
return singleParamNotation(registry, 'Linkage', typeDef, definitions, imports, withShortcut);
},
[TypeDefInfo.Result]: (registry: CodecRegistry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
[TypeDefInfo.Result]: (registry: Registry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
return dualParamsNotation(registry, 'Result', typeDef, definitions, imports, withShortcut);
},
[TypeDefInfo.WrapperKeepOpaque]: (registry: CodecRegistry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
[TypeDefInfo.WrapperKeepOpaque]: (registry: Registry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
return singleParamNotation(registry, 'WrapperKeepOpaque', typeDef, definitions, imports, withShortcut);
},
[TypeDefInfo.WrapperOpaque]: (registry: CodecRegistry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
[TypeDefInfo.WrapperOpaque]: (registry: Registry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
return singleParamNotation(registry, 'WrapperOpaque', typeDef, definitions, imports, withShortcut);
}
};
@@ -263,7 +262,7 @@ const formatters: Record<TypeDefInfo, (registry: CodecRegistry, typeDef: TypeDef
*/
/** @internal */
// eslint-disable-next-line @typescript-eslint/ban-types
export function formatType (registry: CodecRegistry, definitions: Record<string, ModuleTypes>, type: string | String | TypeDef, imports: TypeImports, withShortcut = false): string {
export function formatType (registry: Registry, definitions: Record<string, ModuleTypes>, type: string | String | TypeDef, imports: TypeImports, withShortcut = false): string {
let typeDef: TypeDef;
if (isString(type)) {