Formatting for metadata documentation (#3969)

* Formatting for metadata documentation

* unwrap
This commit is contained in:
Jaco
2021-09-19 08:18:13 +02:00
committed by GitHub
parent f6e50f14b5
commit 598e69002a
9 changed files with 38 additions and 36 deletions
+7
View File
@@ -1,5 +1,12 @@
# CHANGELOG
## master
Changes:
- Adjust typegen formatting for metadata documentation
## 6.0.2 Sep 18, 2021
Upgrade priority: Low. Recommended for all users on 6.0.1, especially those using `api.query.substrate.*` calls
+2 -1
View File
@@ -198,7 +198,8 @@ function generateLookupDefs (registry: Registry, filtered: [PortableType, TypeDe
].map(([n, t]) => `${n}: ${t}`),
docs
};
})
}),
headerType: 'defs'
});
});
}
-21
View File
@@ -21,27 +21,6 @@ import { ModuleTypes } from '../util/imports';
function entrySignature (lookup: PortableRegistry, allDefs: Record<string, ModuleTypes>, registry: Registry, storageEntry: StorageEntryMetadataLatest, imports: TypeImports): [boolean, string, string, string] {
const outputType = lookup.getTypeDef(unwrapStorageSi(storageEntry.type));
// else if (storageEntry.type.isMap) {
// const map = storageEntry.type.asMap;
// const keyDef = lookup.getTypeDef(map.key);
// const valDef = lookup.getTypeDef(map.value);
// // Find similar types of the `key` type
// const similarTypes = getSimilarTypes(registry, allDefs, keyDef.lookupName || keyDef.type, imports);
// setImports(allDefs, imports, [
// ...similarTypes,
// valDef.lookupName || valDef.type
// ]);
// return [
// storageEntry.modifier.isOptional,
// formatType(registry, allDefs, keyDef.lookupName || keyDef.type, imports),
// `arg: ${similarTypes.join(' | ')}`,
// formatType(registry, allDefs, outputType, imports)
// ];
// }
if (storageEntry.type.isPlain) {
const typeDef = lookup.getTypeDef(storageEntry.type.asPlain);
+20 -8
View File
@@ -1,7 +1,7 @@
// Copyright 2017-2021 @polkadot/typegen authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { MetadataLatest } from '@polkadot/types/interfaces/metadata';
import type { MetadataLatest, PortableRegistry, SiLookupTypeId } from '@polkadot/types/interfaces';
import type { Codec, DefinitionRpcParam } from '@polkadot/types/types';
import fs from 'fs';
@@ -118,6 +118,12 @@ function sortByName<T extends { name: Codec | string }> (a: T, b: T): number {
return a.name.toString().toUpperCase().localeCompare(b.name.toString().toUpperCase());
}
function getSiName (lookup: PortableRegistry, type: SiLookupTypeId): string {
const typeDef = lookup.getTypeDef(type);
return typeDef.lookupName || typeDef.type;
}
/** @internal */
function addRpc (): string {
const sections = Object
@@ -185,7 +191,7 @@ function addConstants ({ lookup, pallets }: MetadataLatest): string {
return {
interface: '`' + `api.consts.${sectionName}.${methodName}` + '`',
name: `${methodName}: ` + '`' + lookup.getTypeDef(type).type + '`',
name: `${methodName}: ` + '`' + getSiName(lookup, type) + '`',
...(docs.length && { summary: docs })
};
}),
@@ -214,9 +220,11 @@ function addStorage ({ lookup, pallets, registry }: MetadataLatest): string {
if (func.type.isMap) {
const { hashers, key } = func.type.asMap;
arg = hashers.length === 1
? ('`' + key.toString() + '`')
: ('`' + lookup.getSiType(key).def.asTuple.map((t) => t).join(', ') + '`');
arg = '`' + (
hashers.length === 1
? getSiName(lookup, key)
: lookup.getSiType(key).def.asTuple.map((t) => getSiName(lookup, t)).join(', ')
) + '`';
}
const methodName = stringLowerFirst(func.name);
@@ -238,7 +246,7 @@ function addStorage ({ lookup, pallets, registry }: MetadataLatest): string {
description: 'These are well-known keys that are always available to the runtime implementation of any Substrate-based network.',
items: Object.entries(substrate).map(([name, { meta }]) => {
const arg = meta.type.isMap
? ('`' + lookup.getTypeDef(meta.type.asMap.key).type + '`')
? ('`' + getSiName(lookup, meta.type.asMap.key) + '`')
: '';
const methodName = stringLowerFirst(name);
const outputType = unwrapStorageType(registry, meta.type, meta.modifier.isOptional);
@@ -270,7 +278,9 @@ function addExtrinsics ({ lookup, pallets }: MetadataLatest): string {
.sort(sortByName)
.map(({ docs, fields, name }, index) => {
const methodName = stringCamelCase(name);
const args = fields.map(({ name, type }) => `${name.isSome ? name.toString() : `param${index}`}: ` + '`' + lookup.getTypeDef(type).type + '`').join(', ');
const args = fields.map(({ name, type }) =>
`${name.isSome ? name.toString() : `param${index}`}: ` + '`' + getSiName(lookup, type) + '`'
).join(', ');
return {
interface: '`' + `api.tx.${sectionName}.${methodName}` + '`',
@@ -297,7 +307,9 @@ function addEvents ({ lookup, pallets }: MetadataLatest): string {
.sort(sortByName)
.map(({ docs, fields, name }) => {
const methodName = name.toString();
const args = fields.map(({ type }) => '`' + lookup.getTypeDef(type).type + '`').join(', ');
const args = fields.map(({ type }) =>
'`' + getSiName(lookup, type) + '`'
).join(', ');
return {
interface: '`' + `api.events.${stringCamelCase(meta.name)}.${methodName}.is` + '`',
@@ -10,8 +10,10 @@ import substrate from './substrate';
export default {
rpc: {},
types: {
// Not 100% sure it is relevant, however the order here is the same
// as exposed in the typegen lookup order
...substrate,
...polkadot,
...kusama,
...substrate
...kusama
}
} as Definitions;
+1 -1
View File
@@ -1,4 +1,4 @@
// Auto-generated via `yarn polkadot-types-from-`, do not edit
// Auto-generated via `yarn polkadot-types-from-defs`, do not edit
/* eslint-disable */
/* eslint-disable sort-keys */
@@ -1,4 +1,4 @@
// Auto-generated via `yarn polkadot-types-from-`, do not edit
// Auto-generated via `yarn polkadot-types-from-defs`, do not edit
/* eslint-disable */
/* eslint-disable sort-keys */
@@ -1,4 +1,4 @@
// Auto-generated via `yarn polkadot-types-from-`, do not edit
// Auto-generated via `yarn polkadot-types-from-defs`, do not edit
/* eslint-disable */
/* eslint-disable sort-keys */
+2 -1
View File
@@ -42,7 +42,8 @@ export function unwrapStorageSi (type: StorageEntryTypeLatest): SiLookupTypeId {
/** @internal */
export function unwrapStorageType (registry: Registry, type: StorageEntryTypeLatest, isOptional?: boolean): keyof InterfaceTypes {
const outputType = registry.lookup.getTypeDef(unwrapStorageSi(type)).type;
const typeDef = registry.lookup.getTypeDef(unwrapStorageSi(type));
const outputType = typeDef.lookupName || typeDef.type;
return isOptional
? `Option<${outputType}>` as keyof InterfaceTypes