Add typeName to TypeDef (#4089)

* Add typeName to TypeDef

* Sanitize typeName

* Export typeName inside fuction args meta

* Correct typeName extraction
This commit is contained in:
Jaco
2021-10-22 08:35:09 +03:00
committed by GitHub
parent 2ded2052b7
commit 72fcd97cdb
6 changed files with 42 additions and 16 deletions
+1
View File
@@ -41,6 +41,7 @@ export interface TypeDef {
namespace?: string;
sub?: TypeDef | TypeDef[];
type: string;
typeName?: string;
}
export interface CreateOptions {
@@ -12,6 +12,7 @@ import { assert, isNumber, isString, stringCamelCase, stringify, stringUpperFirs
import { Struct } from '../../codec/Struct';
import { withTypeString } from '../../create/encodeTypes';
import { getTypeDef } from '../../create/getTypeDef';
import { sanitize } from '../../create/sanitize';
import { TypeDefInfo } from '../../types';
// Just a placeholder for a type.unrwapOr()
@@ -439,11 +440,15 @@ export class GenericPortableRegistry extends Struct {
} else if (['Range', 'RangeInclusive'].includes(specialVariant)) {
return withTypeString(this.registry, {
info: TypeDefInfo.Range,
sub: fields.map(({ name, type }, index) => ({
sub: fields.map(({ name, type, typeName }, index) => ({
name: name.isSome
? name.unwrap().toString()
: ['start', 'end'][index],
...this.#createSiDef(type)
...this.#createSiDef(type),
...(typeName.isSome
? { typeName: sanitize(typeName.unwrap()) }
: {}
)
}))
});
} else if (path.length && path[path.length - 1].toString() === 'WrapperOpaque') {
@@ -493,14 +498,17 @@ export class GenericPortableRegistry extends Struct {
return {
...typeDef,
...(
lookupIndex === -1
? {}
: {
lookupIndex,
lookupName: this.#names[lookupIndex],
lookupNameRoot: typeDef.lookupName
}
...(lookupIndex === -1
? {}
: {
lookupIndex,
lookupName: this.#names[lookupIndex],
lookupNameRoot: typeDef.lookupName
}
),
...(fields[0].typeName.isSome
? { typeName: sanitize(fields[0].typeName.unwrap()) }
: {}
)
};
}
@@ -530,7 +538,7 @@ export class GenericPortableRegistry extends Struct {
#extractFieldsAlias (fields: SiField[]): [TypeDef[], Map<string, string>] {
const alias = new Map<string, string>();
const sub = fields.map(({ docs, name, type }) => {
const sub = fields.map(({ docs, name, type, typeName }) => {
const typeDef = this.#createSiDef(type);
if (name.isNone) {
@@ -555,7 +563,11 @@ export class GenericPortableRegistry extends Struct {
return {
...typeDef,
docs: docs.map((d) => d.toString()),
name: nameField
name: nameField,
...(typeName.isSome
? { typeName: sanitize(typeName.unwrap()) }
: {}
)
};
});
@@ -103,7 +103,11 @@ export interface FunctionArgumentMetadataV12 extends FunctionArgumentMetadataV11
export interface FunctionArgumentMetadataV13 extends FunctionArgumentMetadataV12 {}
/** @name FunctionArgumentMetadataV14 */
export interface FunctionArgumentMetadataV14 extends FunctionArgumentMetadataV13 {}
export interface FunctionArgumentMetadataV14 extends Struct {
readonly name: Text;
readonly type: Type;
readonly typeName: Option<Type>;
}
/** @name FunctionArgumentMetadataV9 */
export interface FunctionArgumentMetadataV9 extends Struct {
@@ -25,7 +25,11 @@ export const v14: DefinitionsTypes = {
...Si1Variant,
args: 'Vec<Type>'
},
FunctionArgumentMetadataV14: 'FunctionArgumentMetadataV13',
FunctionArgumentMetadataV14: {
name: 'Text',
type: 'Type',
typeName: 'Option<Type>'
},
FunctionMetadataV14: {
...Si1Variant,
args: 'Vec<FunctionArgumentMetadataV14>'
@@ -24,9 +24,13 @@ export function decorateExtrinsics (registry: Registry, { lookup, pallets }: Met
.reduce((newModule: ModuleExtrinsics, variant): ModuleExtrinsics => {
const callMetadata = registry.createType('FunctionMetadataLatest', {
...variant,
args: variant.fields.map(({ name, type }, index) => ({
args: variant.fields.map(({ name, type, typeName }, index) => ({
name: stringCamelCase(name.unwrapOr(`param${index}`)),
type: getSiName(lookup, type)
type: getSiName(lookup, type),
...(typeName.isSome
? { typeName: typeName.unwrap() }
: {}
)
}))
});