Skip to content

Commit

Permalink
fix impl interface array field bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Zetazzz committed Dec 25, 2023
1 parent 11e57b2 commit b6a9c94
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,29 @@ export interface GrantQueueItemAminoMsg {
export interface GrantQueueItemSDKType {
msg_type_urls: string[];
}
/** test Any array */
export interface Grants {
authorization: (GenericAuthorization | DepositDeploymentAuthorization | SendAuthorization | Any)[] | Any[];
}
export interface GrantsProtoMsg {
typeUrl: "/cosmos.authz.v1beta1.Grants";
value: Uint8Array;
}
export type GrantsEncoded = Omit<Grants, "authorization"> & {
authorization: (GenericAuthorizationProtoMsg | DepositDeploymentAuthorizationProtoMsg | SendAuthorizationProtoMsg | AnyProtoMsg)[];
};
/** test Any array */
export interface GrantsAmino {
authorization?: AnyAmino[];
}
export interface GrantsAminoMsg {
type: "cosmos-sdk/Grants";
value: GrantsAmino;
}
/** test Any array */
export interface GrantsSDKType {
authorization: (GenericAuthorizationSDKType | DepositDeploymentAuthorizationSDKType | SendAuthorizationSDKType | AnySDKType)[];
}
function createBaseGenericAuthorization(): GenericAuthorization {
return {
msg: ""
Expand Down Expand Up @@ -778,4 +801,120 @@ export const GrantQueueItem = {
}
};
GlobalDecoderRegistry.register(GrantQueueItem.typeUrl, GrantQueueItem);
GlobalDecoderRegistry.registerAminoProtoMapping(GrantQueueItem.aminoType, GrantQueueItem.typeUrl);
GlobalDecoderRegistry.registerAminoProtoMapping(GrantQueueItem.aminoType, GrantQueueItem.typeUrl);
function createBaseGrants(): Grants {
return {
authorization: []
};
}
export const Grants = {
typeUrl: "/cosmos.authz.v1beta1.Grants",
aminoType: "cosmos-sdk/Grants",
is(o: any): o is Grants {
return o && (o.$typeUrl === Grants.typeUrl || Array.isArray(o.authorization) && (!o.authorization.length || GenericAuthorization.is(o.authorization[0]) || DepositDeploymentAuthorization.is(o.authorization[0]) || SendAuthorization.is(o.authorization[0]) || Any.is(o.authorization[0])));
},
isSDK(o: any): o is GrantsSDKType {
return o && (o.$typeUrl === Grants.typeUrl || Array.isArray(o.authorization) && (!o.authorization.length || GenericAuthorization.isSDK(o.authorization[0]) || DepositDeploymentAuthorization.isSDK(o.authorization[0]) || SendAuthorization.isSDK(o.authorization[0]) || Any.isSDK(o.authorization[0])));
},
isAmino(o: any): o is GrantsAmino {
return o && (o.$typeUrl === Grants.typeUrl || Array.isArray(o.authorization) && (!o.authorization.length || GenericAuthorization.isAmino(o.authorization[0]) || DepositDeploymentAuthorization.isAmino(o.authorization[0]) || SendAuthorization.isAmino(o.authorization[0]) || Any.isAmino(o.authorization[0])));
},
encode(message: Grants, writer: BinaryWriter = BinaryWriter.create()): BinaryWriter {
for (const v of message.authorization) {
Any.encode(GlobalDecoderRegistry.wrapAny(v!), writer.uint32(10).fork()).ldelim();
}
return writer;
},
decode(input: BinaryReader | Uint8Array, length?: number): Grants {
const reader = input instanceof BinaryReader ? input : new BinaryReader(input);
let end = length === undefined ? reader.len : reader.pos + length;
const message = createBaseGrants();
while (reader.pos < end) {
const tag = reader.uint32();
switch (tag >>> 3) {
case 1:
message.authorization.push(GlobalDecoderRegistry.unwrapAny(reader));
break;
default:
reader.skipType(tag & 7);
break;
}
}
return message;
},
fromJSON(object: any): Grants {
const obj = createBaseGrants();
if (Array.isArray(object?.authorization)) obj.authorization = object.authorization.map((e: any) => GlobalDecoderRegistry.fromJSON(e));
return obj;
},
toJSON(message: Grants): unknown {
const obj: any = {};
if (message.authorization) {
obj.authorization = message.authorization.map(e => e ? GlobalDecoderRegistry.toJSON(e) : undefined);
} else {
obj.authorization = [];
}
return obj;
},
fromPartial(object: DeepPartial<Grants>): Grants {
const message = createBaseGrants();
message.authorization = object.authorization?.map(e => (Any.fromPartial(e) as any)) || [];
return message;
},
fromSDK(object: GrantsSDKType): Grants {
return {
authorization: Array.isArray(object?.authorization) ? object.authorization.map((e: any) => GlobalDecoderRegistry.fromSDK(e)) : []
};
},
fromSDKJSON(object: any): GrantsSDKType {
return {
authorization: Array.isArray(object?.authorization) ? object.authorization.map((e: any) => GlobalDecoderRegistry.fromSDKJSON(e)) : []
};
},
toSDK(message: Grants): GrantsSDKType {
const obj: any = {};
if (message.authorization) {
obj.authorization = message.authorization.map(e => e ? GlobalDecoderRegistry.toSDK(e) : undefined);
} else {
obj.authorization = [];
}
return obj;
},
fromAmino(object: GrantsAmino): Grants {
const message = createBaseGrants();
message.authorization = object.authorization?.map(e => GlobalDecoderRegistry.fromAminoMsg(e)) || [];
return message;
},
toAmino(message: Grants): GrantsAmino {
const obj: any = {};
if (message.authorization) {
obj.authorization = message.authorization.map(e => e ? GlobalDecoderRegistry.toAminoMsg(e) : undefined);
} else {
obj.authorization = [];
}
return obj;
},
fromAminoMsg(object: GrantsAminoMsg): Grants {
return Grants.fromAmino(object.value);
},
toAminoMsg(message: Grants): GrantsAminoMsg {
return {
type: "cosmos-sdk/Grants",
value: Grants.toAmino(message)
};
},
fromProtoMsg(message: GrantsProtoMsg): Grants {
return Grants.decode(message.value);
},
toProto(message: Grants): Uint8Array {
return Grants.encode(message).finish();
},
toProtoMsg(message: Grants): GrantsProtoMsg {
return {
typeUrl: "/cosmos.authz.v1beta1.Grants",
value: Grants.encode(message).finish()
};
}
};
GlobalDecoderRegistry.register(Grants.typeUrl, Grants);
GlobalDecoderRegistry.registerAminoProtoMapping(Grants.aminoType, Grants.typeUrl);
Original file line number Diff line number Diff line change
Expand Up @@ -1764,8 +1764,8 @@ export const AminoEncodingTest = {
message.dOPeriods = object.dOPeriods?.map(e => Duration.fromPartial(e)) || [];
message.protos = object.protos?.map(e => AccessConfig.fromPartial(e)) || [];
message.dOProtos = object.dOProtos?.map(e => AccessConfig.fromPartial(e)) || [];
message.auths = object.auths?.map(e => GlobalDecoderRegistry.fromPartial(e)) || [];
message.dOAuths = object.dOAuths?.map(e => GlobalDecoderRegistry.fromPartial(e)) || [];
message.auths = object.auths?.map(e => (Any.fromPartial(e) as any)) || [];
message.dOAuths = object.dOAuths?.map(e => (Any.fromPartial(e) as any)) || [];
return message;
},
fromSDK(object: AminoEncodingTestSDKType): AminoEncodingTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1764,8 +1764,8 @@ export const AminoEncodingTest = {
message.dOPeriods = object.dOPeriods?.map(e => Duration.fromPartial(e)) || [];
message.protos = object.protos?.map(e => AccessConfig.fromPartial(e)) || [];
message.dOProtos = object.dOProtos?.map(e => AccessConfig.fromPartial(e)) || [];
message.auths = object.auths?.map(e => GlobalDecoderRegistry.fromPartial(e)) || [];
message.dOAuths = object.dOAuths?.map(e => GlobalDecoderRegistry.fromPartial(e)) || [];
message.auths = object.auths?.map(e => (Any.fromPartial(e) as any)) || [];
message.dOAuths = object.dOAuths?.map(e => (Any.fromPartial(e) as any)) || [];
return message;
},
fromSDK(object: AminoEncodingTestSDKType): AminoEncodingTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,8 @@ message GrantQueueItem {
// msg_type_urls contains the list of TypeURL of a sdk.Msg.
repeated string msg_type_urls = 1;
}

// test Any array
message Grants {
repeated google.protobuf.Any authorization = 1 [(gogoproto.nullable) = false, (cosmos_proto.accepts_interface) = "Authorization"];
}
25 changes: 16 additions & 9 deletions packages/ast/src/encoding/proto/from-partial/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,16 @@ export const arrayTypes = {
type(args: FromPartialMethod) {
let name = args.context.getTypeName(args.field);

const callExpr = t.callExpression(
t.memberExpression(
t.identifier(name),
t.identifier('fromPartial')
),
[
t.identifier('e')
]
);

if (
!args.context.options.aminoEncoding.useLegacyInlineEncoding &&
args.context.options.interfaces.enabled &&
Expand All @@ -605,16 +615,13 @@ export const arrayTypes = {
args.field.options['(cosmos_proto.accepts_interface)']
) {
name = 'GlobalDecoderRegistry';

return t.tsAsExpression(
callExpr,
t.tsAnyKeyword()
)
}

return t.callExpression(
t.memberExpression(
t.identifier(name),
t.identifier('fromPartial')
),
[
t.identifier('e')
]
);
return callExpr;
}
}
2 changes: 1 addition & 1 deletion packages/ast/types/encoding/proto/from-partial/utils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,5 @@ export declare const arrayTypes: {
sint64(args: FromPartialMethod): t.Expression;
fixed64(args: FromPartialMethod): t.Expression;
sfixed64(args: FromPartialMethod): t.Expression;
type(args: FromPartialMethod): t.CallExpression;
type(args: FromPartialMethod): t.CallExpression | t.TSAsExpression;
};

0 comments on commit b6a9c94

Please sign in to comment.