Skip to content

Commit

Permalink
edited decode
Browse files Browse the repository at this point in the history
  • Loading branch information
Zetazzz committed Nov 16, 2023
1 parent e011744 commit 9d05373
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down Expand Up @@ -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()));
Expand Down
49 changes: 49 additions & 0 deletions packages/ast/src/encoding/proto/decode/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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),
Expand Down
3 changes: 2 additions & 1 deletion packages/ast/types/encoding/proto/decode/utils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit 9d05373

Please sign in to comment.