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 # CHANGELOG
## master
Changes:
- Adjust typegen formatting for metadata documentation
## 6.0.2 Sep 18, 2021 ## 6.0.2 Sep 18, 2021
Upgrade priority: Low. Recommended for all users on 6.0.1, especially those using `api.query.substrate.*` calls 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}`), ].map(([n, t]) => `${n}: ${t}`),
docs 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] { 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)); 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) { if (storageEntry.type.isPlain) {
const typeDef = lookup.getTypeDef(storageEntry.type.asPlain); const typeDef = lookup.getTypeDef(storageEntry.type.asPlain);
+20 -8
View File
@@ -1,7 +1,7 @@
// Copyright 2017-2021 @polkadot/typegen authors & contributors // Copyright 2017-2021 @polkadot/typegen authors & contributors
// SPDX-License-Identifier: Apache-2.0 // 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 type { Codec, DefinitionRpcParam } from '@polkadot/types/types';
import fs from 'fs'; 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()); 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 */ /** @internal */
function addRpc (): string { function addRpc (): string {
const sections = Object const sections = Object
@@ -185,7 +191,7 @@ function addConstants ({ lookup, pallets }: MetadataLatest): string {
return { return {
interface: '`' + `api.consts.${sectionName}.${methodName}` + '`', interface: '`' + `api.consts.${sectionName}.${methodName}` + '`',
name: `${methodName}: ` + '`' + lookup.getTypeDef(type).type + '`', name: `${methodName}: ` + '`' + getSiName(lookup, type) + '`',
...(docs.length && { summary: docs }) ...(docs.length && { summary: docs })
}; };
}), }),
@@ -214,9 +220,11 @@ function addStorage ({ lookup, pallets, registry }: MetadataLatest): string {
if (func.type.isMap) { if (func.type.isMap) {
const { hashers, key } = func.type.asMap; const { hashers, key } = func.type.asMap;
arg = hashers.length === 1 arg = '`' + (
? ('`' + key.toString() + '`') hashers.length === 1
: ('`' + lookup.getSiType(key).def.asTuple.map((t) => t).join(', ') + '`'); ? getSiName(lookup, key)
: lookup.getSiType(key).def.asTuple.map((t) => getSiName(lookup, t)).join(', ')
) + '`';
} }
const methodName = stringLowerFirst(func.name); 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.', 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 }]) => { items: Object.entries(substrate).map(([name, { meta }]) => {
const arg = meta.type.isMap const arg = meta.type.isMap
? ('`' + lookup.getTypeDef(meta.type.asMap.key).type + '`') ? ('`' + getSiName(lookup, meta.type.asMap.key) + '`')
: ''; : '';
const methodName = stringLowerFirst(name); const methodName = stringLowerFirst(name);
const outputType = unwrapStorageType(registry, meta.type, meta.modifier.isOptional); const outputType = unwrapStorageType(registry, meta.type, meta.modifier.isOptional);
@@ -270,7 +278,9 @@ function addExtrinsics ({ lookup, pallets }: MetadataLatest): string {
.sort(sortByName) .sort(sortByName)
.map(({ docs, fields, name }, index) => { .map(({ docs, fields, name }, index) => {
const methodName = stringCamelCase(name); 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 { return {
interface: '`' + `api.tx.${sectionName}.${methodName}` + '`', interface: '`' + `api.tx.${sectionName}.${methodName}` + '`',
@@ -297,7 +307,9 @@ function addEvents ({ lookup, pallets }: MetadataLatest): string {
.sort(sortByName) .sort(sortByName)
.map(({ docs, fields, name }) => { .map(({ docs, fields, name }) => {
const methodName = name.toString(); const methodName = name.toString();
const args = fields.map(({ type }) => '`' + lookup.getTypeDef(type).type + '`').join(', '); const args = fields.map(({ type }) =>
'`' + getSiName(lookup, type) + '`'
).join(', ');
return { return {
interface: '`' + `api.events.${stringCamelCase(meta.name)}.${methodName}.is` + '`', interface: '`' + `api.events.${stringCamelCase(meta.name)}.${methodName}.is` + '`',
@@ -10,8 +10,10 @@ import substrate from './substrate';
export default { export default {
rpc: {}, rpc: {},
types: { types: {
// Not 100% sure it is relevant, however the order here is the same
// as exposed in the typegen lookup order
...substrate,
...polkadot, ...polkadot,
...kusama, ...kusama
...substrate
} }
} as Definitions; } 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 */
/* eslint-disable sort-keys */ /* 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 */
/* eslint-disable sort-keys */ /* 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 */
/* eslint-disable sort-keys */ /* eslint-disable sort-keys */
+2 -1
View File
@@ -42,7 +42,8 @@ export function unwrapStorageSi (type: StorageEntryTypeLatest): SiLookupTypeId {
/** @internal */ /** @internal */
export function unwrapStorageType (registry: Registry, type: StorageEntryTypeLatest, isOptional?: boolean): keyof InterfaceTypes { 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 return isOptional
? `Option<${outputType}>` as keyof InterfaceTypes ? `Option<${outputType}>` as keyof InterfaceTypes