Add .keys on storage entries (#2032)
* Add .keys on storage entries * Pass isOptional to storage type unwrap * Linked maps TS types
This commit is contained in:
@@ -8,30 +8,22 @@ import * as defaultDefs from '@polkadot/types/interfaces/definitions';
|
||||
|
||||
import staticData from '@polkadot/metadata/Metadata/static';
|
||||
import Metadata from '@polkadot/metadata/Metadata';
|
||||
import { unwrapStorageType } from '@polkadot/types/primitive/StorageKey';
|
||||
import { TypeRegistry } from '@polkadot/types/create';
|
||||
import { stringLowerFirst } from '@polkadot/util';
|
||||
|
||||
import { FOOTER, HEADER, TypeImports, createDocComments, createImportCode, createImports, formatType, getSimilarTypes, indent, registerDefinitions, setImports, writeFile } from '../util';
|
||||
|
||||
// If the StorageEntry returns T, output `Option<T>` if the modifier is optional
|
||||
/** @internal */
|
||||
function addModifier (storageEntry: StorageEntryMetadataLatest, returnType: string): string {
|
||||
if (storageEntry.modifier.isOptional) {
|
||||
return `Option<${returnType}>`;
|
||||
}
|
||||
|
||||
return returnType;
|
||||
}
|
||||
|
||||
// From a storage entry metadata, we return [args, returnType]
|
||||
/** @internal */
|
||||
function entrySignature (allDefs: object, registry: Registry, storageEntry: StorageEntryMetadataLatest, imports: TypeImports): [string, string] {
|
||||
const format = (type: string): string => formatType(allDefs, type, imports);
|
||||
const outputType = unwrapStorageType(storageEntry.type, storageEntry.modifier.isOptional);
|
||||
|
||||
if (storageEntry.type.isPlain) {
|
||||
setImports(allDefs, imports, [storageEntry.type.asPlain.toString()]);
|
||||
|
||||
return ['', formatType(allDefs, addModifier(storageEntry, storageEntry.type.asPlain.toString()), imports)];
|
||||
return ['', formatType(allDefs, outputType, imports)];
|
||||
} else if (storageEntry.type.isMap) {
|
||||
// Find similar types of the `key` type
|
||||
const similarTypes = getSimilarTypes(allDefs, registry, storageEntry.type.asMap.key.toString(), imports);
|
||||
@@ -43,7 +35,7 @@ function entrySignature (allDefs: object, registry: Registry, storageEntry: Stor
|
||||
|
||||
return [
|
||||
`arg: ${similarTypes.map(format).join(' | ')}`,
|
||||
formatType(allDefs, addModifier(storageEntry, storageEntry.type.asMap.value.toString()), imports)
|
||||
formatType(allDefs, outputType, imports)
|
||||
];
|
||||
} else if (storageEntry.type.isDoubleMap) {
|
||||
// Find similar types of `key1` and `key2` types
|
||||
@@ -61,7 +53,7 @@ function entrySignature (allDefs: object, registry: Registry, storageEntry: Stor
|
||||
|
||||
return [
|
||||
`key1: ${key1Types}, key2: ${key2Types}`,
|
||||
formatType(allDefs, addModifier(storageEntry, storageEntry.type.asDoubleMap.value.toString()), imports)
|
||||
formatType(allDefs, outputType, imports)
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { MetadataLatest } from '@polkadot/types/interfaces/metadata';
|
||||
import { InterfaceTypes } from '@polkadot/types/types';
|
||||
|
||||
import fs from 'fs';
|
||||
import interfaces from '@polkadot/jsonrpc';
|
||||
@@ -181,14 +180,10 @@ function addStorage (metadata: MetadataLatest): string {
|
||||
? ('`' + func.type.asDoubleMap.key1.toString() + ', ' + func.type.asDoubleMap.key2.toString() + '`')
|
||||
: '';
|
||||
const methodName = stringLowerFirst(func.name.toString());
|
||||
let result = unwrapStorageType(func.type);
|
||||
|
||||
if (func.modifier.isOptional) {
|
||||
result = `Option<${result}>` as keyof InterfaceTypes;
|
||||
}
|
||||
const outputType = unwrapStorageType(func.type, func.modifier.isOptional);
|
||||
|
||||
return {
|
||||
name: `${methodName}(${arg}): ` + '`' + result + '`',
|
||||
name: `${methodName}(${arg}): ` + '`' + outputType + '`',
|
||||
interface: '`' + `api.query.${sectionName}.${methodName}` + '`',
|
||||
...(func.documentation.length && { summary: func.documentation })
|
||||
};
|
||||
|
||||
@@ -105,6 +105,14 @@ function formatHashMap (key: string, val: string): string {
|
||||
return `HashMap<${key}, ${val}>`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Given the inner `T`, return a `Vec<T>` string
|
||||
*/
|
||||
/** @internal */
|
||||
function formatLinkage (inner: string): string {
|
||||
return paramsNotation('Linkage', inner);
|
||||
}
|
||||
|
||||
/**
|
||||
* Given the inner `O` & `E`, return a `Result<O, E>` string
|
||||
*/
|
||||
@@ -221,6 +229,13 @@ export function formatType (definitions: object, type: string | TypeDef, imports
|
||||
|
||||
return formatHashMap(formatType(definitions, keyDef.type, imports), formatType(definitions, valDef.type, imports));
|
||||
}
|
||||
case TypeDefInfo.Linkage: {
|
||||
const type = (typeDef.sub as TypeDef).type;
|
||||
|
||||
setImports(definitions, imports, ['Linkage']);
|
||||
|
||||
return formatLinkage(formatType(definitions, type, imports));
|
||||
}
|
||||
case TypeDefInfo.Result: {
|
||||
setImports(definitions, imports, ['Result']);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user