From a307df1ba5025cf1a24115e89258dce023019c87 Mon Sep 17 00:00:00 2001 From: nedsalk Date: Tue, 17 Dec 2024 14:45:04 +0100 Subject: [PATCH 1/2] refactor from array of tuples into `Map` --- .../specifications/v1/abi-type-mappers.ts | 8 ++- .../specifications/v1/resolvable-type.ts | 61 +++++++++++-------- .../parser/specifications/v1/resolved-type.ts | 4 +- 3 files changed, 43 insertions(+), 30 deletions(-) diff --git a/packages/abi/src/parser/specifications/v1/abi-type-mappers.ts b/packages/abi/src/parser/specifications/v1/abi-type-mappers.ts index 385c80d106..4be87328a5 100644 --- a/packages/abi/src/parser/specifications/v1/abi-type-mappers.ts +++ b/packages/abi/src/parser/specifications/v1/abi-type-mappers.ts @@ -15,7 +15,7 @@ function mapMetadata(type: ResolvableType | ResolvedType) { }; if (type.typeParamsArgsMap && type.metadataType?.typeParameters?.length) { - result.typeArguments = type.typeParamsArgsMap.map((t) => toTypeArgument(t[1])); + result.typeArguments = [...type.typeParamsArgsMap.values()].map((rt) => toTypeArgument(rt)); } return result; @@ -81,8 +81,10 @@ export function toAbiType(t: ResolvableType | ResolvedType): AbiConcreteType | A metadataTypeId: t.metadataType?.metadataTypeId as number, }; - if (t.typeParamsArgsMap) { - result.typeParameters = t.typeParamsArgsMap.map(([, rt]) => toAbiType(rt) as AbiMetadataType); + if (t.typeParamsArgsMap && t.metadataType?.typeParameters?.length) { + result.typeParameters = [...t.typeParamsArgsMap.values()].map( + (rt) => toAbiType(rt) as AbiMetadataType + ); } } diff --git a/packages/abi/src/parser/specifications/v1/resolvable-type.ts b/packages/abi/src/parser/specifications/v1/resolvable-type.ts index 6f85aad337..249a074fb6 100644 --- a/packages/abi/src/parser/specifications/v1/resolvable-type.ts +++ b/packages/abi/src/parser/specifications/v1/resolvable-type.ts @@ -27,14 +27,18 @@ export class ResolvableType { concreteTypes: Map; }, public metadataTypeId: number, - public typeParamsArgsMap: Array<[number, ResolvedType | ResolvableType]> | undefined + public typeParamsArgsMap: Map | undefined ) { this.metadataType = this.findMetadataType(metadataTypeId); this.swayType = this.metadataType.type; - this.typeParamsArgsMap ??= this.metadataType.typeParameters?.map((tp) => [ - tp, - new ResolvableType(this.abiTypeMaps, tp, undefined), - ]); + this.typeParamsArgsMap ??= + this.metadataType.typeParameters && + new Map( + this.metadataType.typeParameters.map((tp) => [ + tp, + new ResolvableType(this.abiTypeMaps, tp, undefined), + ]) + ); this.components = this.metadataType.components?.map((c) => this.createResolvableComponent(this, c) @@ -82,8 +86,13 @@ export class ResolvableType { private static mapTypeParametersAndArgs( metadataType: AbiMetadataTypeV1, args: (ResolvableType | ResolvedType)[] - ): Array<[number, ResolvedType | ResolvableType]> | undefined { - return metadataType.typeParameters?.map((typeParameter, idx) => [typeParameter, args[idx]]); + ): Map | undefined { + return ( + metadataType.typeParameters && + new Map( + metadataType.typeParameters.map((typeParameter, idx) => [typeParameter, args[idx]]) + ) + ); } private createResolvableComponent( @@ -176,9 +185,7 @@ export class ResolvableType { * We check in the parent's typeParamsArgsMap if the metadata type we're solving for * has been substituted with a different generic type, and then we use that generic type. */ - const resolvableTypeParameter = parent.typeParamsArgsMap?.find( - ([typeParameterId]) => typeParameterId === metadataType.metadataTypeId - )?.[1]; + const resolvableTypeParameter = parent.typeParamsArgsMap?.get(metadataType.metadataTypeId); return ( resolvableTypeParameter ?? @@ -227,7 +234,7 @@ export class ResolvableType { private resolveInternal( typeId: string | number, - typeParamsArgsMap: Array<[number, ResolvedType]> | undefined + typeParamsArgsMap: Map | undefined ): ResolvedType { const resolvedType = new ResolvedType({ swayType: this.swayType, @@ -263,9 +270,7 @@ export class ResolvableType { * its corresponding type argument will be found in the typeArgs, * which will be used to substitute the component with. */ - const resolvedGenericType = typeArgs?.find( - ([typeParameterId]) => type.metadataTypeId === typeParameterId - )?.[1]; + const resolvedGenericType = typeArgs?.get(type.metadataTypeId); if (resolvedGenericType) { return { @@ -301,8 +306,8 @@ export class ResolvableType { } private resolveTypeArgs( - typeParamsArgsMap: Array<[number, ResolvedType]> | undefined - ): [number, ResolvedType][] | undefined { + typeParamsArgsMap: Map | undefined + ): Map | undefined { /** * This case only happens when the metadata type is *implicitly* generic. * The type itself doesn't have any type parameters that should be resolved, @@ -314,31 +319,36 @@ export class ResolvableType { return typeParamsArgsMap; } + const newMap = new Map(); + /** * We resolve the type parameters of the underlying metadata type * with the type arguments of the concrete type. */ - return this.typeParamsArgsMap.map(([tp, value]) => { + this.typeParamsArgsMap.forEach((value, tp) => { /** * Some type parameters can already be resolved * e.g. `struct MyStruct { a: DoubleGeneric }` * where the second type parameter of DoubleGeneric is already known. */ if (value instanceof ResolvedType) { - return [tp, value]; + newMap.set(tp, value); + return; } - const resolved = typeParamsArgsMap?.find( - ([typeParameterId]) => typeParameterId === value.metadataTypeId - ); - /** * The type parameter is either directly substituted with a type argument, * or it's metadata type which accepts the type argument, * so that metadata type needs to be resolved first. */ - return resolved ?? [tp, value.resolveInternal(value.metadataTypeId, typeParamsArgsMap)]; + const resolved = + typeParamsArgsMap?.get(value.metadataTypeId) ?? + value.resolveInternal(value.metadataTypeId, typeParamsArgsMap); + + newMap.set(value.metadataTypeId, resolved); }); + + return newMap; } public resolve(concreteType: AbiConcreteTypeV1) { @@ -348,8 +358,9 @@ export class ResolvableType { }); const typeParamsArgsMap = concreteTypeArgs - ? (ResolvableType.mapTypeParametersAndArgs(this.metadataType, concreteTypeArgs) as Array< - [number, ResolvedType] + ? (ResolvableType.mapTypeParametersAndArgs(this.metadataType, concreteTypeArgs) as Map< + number, + ResolvedType >) : undefined; diff --git a/packages/abi/src/parser/specifications/v1/resolved-type.ts b/packages/abi/src/parser/specifications/v1/resolved-type.ts index 45dc800b93..6465d10f6d 100644 --- a/packages/abi/src/parser/specifications/v1/resolved-type.ts +++ b/packages/abi/src/parser/specifications/v1/resolved-type.ts @@ -9,14 +9,14 @@ export class ResolvedType { public swayType: string; public typeId: string | number; public components: ResolvedComponent[] | undefined; - public typeParamsArgsMap: Array<[number, ResolvedType]> | undefined; + public typeParamsArgsMap: Map | undefined; public metadataType: AbiMetadataTypeV1 | undefined; constructor(params: { swayType: string; typeId: string | number; components?: ResolvedComponent[]; - typeParamsArgsMap?: Array<[number, ResolvedType]>; + typeParamsArgsMap?: Map; metadataType?: AbiMetadataTypeV1; }) { this.swayType = params.swayType; From 804feb19ec56ebcadfb518b00c51792fa30c0d1a Mon Sep 17 00:00:00 2001 From: nedsalk Date: Tue, 17 Dec 2024 14:47:43 +0100 Subject: [PATCH 2/2] rename variables --- .../specifications/v1/resolvable-type.ts | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/packages/abi/src/parser/specifications/v1/resolvable-type.ts b/packages/abi/src/parser/specifications/v1/resolvable-type.ts index 249a074fb6..f05f4040e5 100644 --- a/packages/abi/src/parser/specifications/v1/resolvable-type.ts +++ b/packages/abi/src/parser/specifications/v1/resolvable-type.ts @@ -34,9 +34,9 @@ export class ResolvableType { this.typeParamsArgsMap ??= this.metadataType.typeParameters && new Map( - this.metadataType.typeParameters.map((tp) => [ - tp, - new ResolvableType(this.abiTypeMaps, tp, undefined), + this.metadataType.typeParameters.map((typeParameter) => [ + typeParameter, + new ResolvableType(this.abiTypeMaps, typeParameter, undefined), ]) ); @@ -325,14 +325,14 @@ export class ResolvableType { * We resolve the type parameters of the underlying metadata type * with the type arguments of the concrete type. */ - this.typeParamsArgsMap.forEach((value, tp) => { + this.typeParamsArgsMap.forEach((arg, typeParameter) => { /** * Some type parameters can already be resolved * e.g. `struct MyStruct { a: DoubleGeneric }` * where the second type parameter of DoubleGeneric is already known. */ - if (value instanceof ResolvedType) { - newMap.set(tp, value); + if (arg instanceof ResolvedType) { + newMap.set(typeParameter, arg); return; } @@ -342,10 +342,10 @@ export class ResolvableType { * so that metadata type needs to be resolved first. */ const resolved = - typeParamsArgsMap?.get(value.metadataTypeId) ?? - value.resolveInternal(value.metadataTypeId, typeParamsArgsMap); + typeParamsArgsMap?.get(arg.metadataTypeId) ?? + arg.resolveInternal(arg.metadataTypeId, typeParamsArgsMap); - newMap.set(value.metadataTypeId, resolved); + newMap.set(arg.metadataTypeId, resolved); }); return newMap;