From 9d05373ffd4ff1a95c0d337387901e46f49d79c5 Mon Sep 17 00:00:00 2001 From: Zetazzz Date: Thu, 16 Nov 2023 18:10:53 +0800 Subject: [PATCH] edited decode --- .../cosmos/authz/v1beta1/authz.ts | 4 +- .../ast/src/encoding/proto/decode/utils.ts | 49 +++++++++++++++++++ .../types/encoding/proto/decode/utils.d.ts | 3 +- 3 files changed, 53 insertions(+), 3 deletions(-) diff --git a/__fixtures__/misc/output-impl-interfaces-gen/cosmos/authz/v1beta1/authz.ts b/__fixtures__/misc/output-impl-interfaces-gen/cosmos/authz/v1beta1/authz.ts index 446f0b1a0..3e7d94711 100644 --- a/__fixtures__/misc/output-impl-interfaces-gen/cosmos/authz/v1beta1/authz.ts +++ b/__fixtures__/misc/output-impl-interfaces-gen/cosmos/authz/v1beta1/authz.ts @@ -266,7 +266,7 @@ export const Grant = { const tag = reader.uint32(); switch (tag >>> 3) { case 1: - message.authorization = (Authorization_InterfaceDecoder(reader) as Any); + message.authorization = GlobalDecoderRegistry.unwrapAny(reader); break; case 2: message.expiration = fromTimestamp(Timestamp.decode(reader, reader.uint32())); @@ -381,7 +381,7 @@ export const GrantAuthorization = { message.grantee = reader.string(); break; case 3: - message.authorization = (Authorization_InterfaceDecoder(reader) as Any); + message.authorization = GlobalDecoderRegistry.unwrapAny(reader); break; case 4: message.expiration = fromTimestamp(Timestamp.decode(reader, reader.uint32())); diff --git a/packages/ast/src/encoding/proto/decode/utils.ts b/packages/ast/src/encoding/proto/decode/utils.ts index eeaa8f535..cfd6019f2 100644 --- a/packages/ast/src/encoding/proto/decode/utils.ts +++ b/packages/ast/src/encoding/proto/decode/utils.ts @@ -148,6 +148,16 @@ export const decode = { args.field.options['(cosmos_proto.accepts_interface)'] ) { + const isGlobalRegistry = args.context.options.interfaces?.enabled && args.context.options.interfaces?.useGlobalDecoderRegistry; + + if(isGlobalRegistry){ + return switchAnyTypeArrayUnwrap( + num, + prop, + name, + ) + } + const interfaceName = args.field.options['(cosmos_proto.accepts_interface)']; const interfaceFnName = getInterfaceDecoderName(interfaceName) @@ -382,6 +392,17 @@ export const baseTypes = { }, anyType(args: DecodeMethod) { + const isGlobalRegistry = args.context.options.interfaces?.enabled && args.context.options.interfaces?.useGlobalDecoderRegistry; + + if(isGlobalRegistry) { + return t.callExpression( + t.memberExpression(t.identifier("GlobalDecoderRegistry"), t.identifier("unwrapAny")), + [ + t.identifier('reader') + ] + ); + } + const interfaceName = args.field.options['(cosmos_proto.accepts_interface)']; const interfaceFnName = getInterfaceDecoderName(interfaceName) const asAny = t.tsAsExpression( @@ -668,6 +689,34 @@ export const switchProtoTypeArray = ( ) }; +export const switchAnyTypeArrayUnwrap = (num: number, prop: string, name: string) => { + return t.switchCase( + t.numericLiteral(num), + [ + t.expressionStatement( + t.callExpression( + t.memberExpression( + t.memberExpression( + t.identifier('message'), + t.identifier(prop) + ), + t.identifier('push') + ), + [ + t.callExpression( + t.memberExpression(t.identifier("GlobalDecoderRegistry"), t.identifier("unwrapAny")), + [ + t.identifier('reader') + ] + ) + ] + ) + ), + t.breakStatement() + ] + ) +}; + export const switchAnyTypeArray = (num: number, prop: string, name: string) => { return t.switchCase( t.numericLiteral(num), diff --git a/packages/ast/types/encoding/proto/decode/utils.d.ts b/packages/ast/types/encoding/proto/decode/utils.d.ts index caeee063b..41318dd5b 100644 --- a/packages/ast/types/encoding/proto/decode/utils.d.ts +++ b/packages/ast/types/encoding/proto/decode/utils.d.ts @@ -42,7 +42,7 @@ export declare const baseTypes: { fixed64(args: DecodeMethod): t.CallExpression | t.TSAsExpression; sfixed64(args: DecodeMethod): t.CallExpression | t.TSAsExpression; protoType(args: DecodeMethod): t.CallExpression; - anyType(args: DecodeMethod): t.ConditionalExpression | t.TSAsExpression; + anyType(args: DecodeMethod): t.CallExpression | t.ConditionalExpression | t.TSAsExpression; type(args: DecodeMethod): t.CallExpression | t.ConditionalExpression | t.TSAsExpression; enum(args: DecodeMethod): t.TSAsExpression; bytes(args: DecodeMethod): t.CallExpression; @@ -54,6 +54,7 @@ export declare const baseTypes: { export declare const switchOnTag: (num: number, prop: string, expr: t.Expression) => t.SwitchCase; export declare const switchOnTagTakesArray: (num: number, prop: string, expr: t.Statement[]) => t.SwitchCase; export declare const switchProtoTypeArray: (args: DecodeMethod, num: number, prop: string, name: string) => t.SwitchCase; +export declare const switchAnyTypeArrayUnwrap: (num: number, prop: string, name: string) => t.SwitchCase; export declare const switchAnyTypeArray: (num: number, prop: string, name: string) => t.SwitchCase; export declare const switchAnyTypeArrayUseInterfaces: (num: number, prop: string, typeName: string, interfaceName: string) => t.SwitchCase; export declare const switchTagDelimArray: (num: number, prop: string, expr: t.Expression) => t.SwitchCase;