Add amino compatibility layer for proto Any (#6151)

* WIP on Any amino compatibility layer

* Add tests & JSON

* Refactor Marshal/UnmarshalAny

* remove extra test

* Add support for nested Any's

* Add docs

* Update codec/any_test.go

Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com>
This commit is contained in:
Aaron Craelius
2020-05-06 20:47:03 +00:00
committed by GitHub
co-authored by Federico Kunze
parent f3e3a30e5e
commit 9d022c17b7
14 changed files with 939 additions and 94 deletions
+44
View File
@@ -27,3 +27,47 @@ func (m HasAnimal) UnpackInterfaces(unpacker types.AnyUnpacker) error {
var animal Animal
return unpacker.UnpackAny(m.Animal, &animal)
}
type HasAnimalI interface {
TheAnimal() Animal
}
var _ HasAnimalI = &HasAnimal{}
func (m HasAnimal) TheAnimal() Animal {
return m.Animal.GetCachedValue().(Animal)
}
type HasHasAnimalI interface {
TheHasAnimal() HasAnimalI
}
var _ HasHasAnimalI = &HasHasAnimal{}
func (m HasHasAnimal) TheHasAnimal() HasAnimalI {
return m.HasAnimal.GetCachedValue().(HasAnimalI)
}
var _ types.UnpackInterfacesMessage = HasHasAnimal{}
func (m HasHasAnimal) UnpackInterfaces(unpacker types.AnyUnpacker) error {
var animal HasAnimalI
return unpacker.UnpackAny(m.HasAnimal, &animal)
}
type HasHasHasAnimalI interface {
TheHasHasAnimal() HasHasAnimalI
}
var _ HasHasAnimalI = &HasHasAnimal{}
func (m HasHasHasAnimal) TheHasHasAnimal() HasHasAnimalI {
return m.HasHasAnimal.GetCachedValue().(HasHasAnimalI)
}
var _ types.UnpackInterfacesMessage = HasHasHasAnimal{}
func (m HasHasHasAnimal) UnpackInterfaces(unpacker types.AnyUnpacker) error {
var animal HasHasAnimalI
return unpacker.UnpackAny(m.HasHasAnimal, &animal)
}
+384 -18
View File
@@ -179,33 +179,125 @@ func (m *HasAnimal) GetX() int64 {
return 0
}
type HasHasAnimal struct {
HasAnimal *types.Any `protobuf:"bytes,1,opt,name=has_animal,json=hasAnimal,proto3" json:"has_animal,omitempty"`
}
func (m *HasHasAnimal) Reset() { *m = HasHasAnimal{} }
func (m *HasHasAnimal) String() string { return proto.CompactTextString(m) }
func (*HasHasAnimal) ProtoMessage() {}
func (*HasHasAnimal) Descriptor() ([]byte, []int) {
return fileDescriptor_ae1353846770e6e2, []int{3}
}
func (m *HasHasAnimal) XXX_Unmarshal(b []byte) error {
return m.Unmarshal(b)
}
func (m *HasHasAnimal) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
if deterministic {
return xxx_messageInfo_HasHasAnimal.Marshal(b, m, deterministic)
} else {
b = b[:cap(b)]
n, err := m.MarshalToSizedBuffer(b)
if err != nil {
return nil, err
}
return b[:n], nil
}
}
func (m *HasHasAnimal) XXX_Merge(src proto.Message) {
xxx_messageInfo_HasHasAnimal.Merge(m, src)
}
func (m *HasHasAnimal) XXX_Size() int {
return m.Size()
}
func (m *HasHasAnimal) XXX_DiscardUnknown() {
xxx_messageInfo_HasHasAnimal.DiscardUnknown(m)
}
var xxx_messageInfo_HasHasAnimal proto.InternalMessageInfo
func (m *HasHasAnimal) GetHasAnimal() *types.Any {
if m != nil {
return m.HasAnimal
}
return nil
}
type HasHasHasAnimal struct {
HasHasAnimal *types.Any `protobuf:"bytes,1,opt,name=has_has_animal,json=hasHasAnimal,proto3" json:"has_has_animal,omitempty"`
}
func (m *HasHasHasAnimal) Reset() { *m = HasHasHasAnimal{} }
func (m *HasHasHasAnimal) String() string { return proto.CompactTextString(m) }
func (*HasHasHasAnimal) ProtoMessage() {}
func (*HasHasHasAnimal) Descriptor() ([]byte, []int) {
return fileDescriptor_ae1353846770e6e2, []int{4}
}
func (m *HasHasHasAnimal) XXX_Unmarshal(b []byte) error {
return m.Unmarshal(b)
}
func (m *HasHasHasAnimal) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
if deterministic {
return xxx_messageInfo_HasHasHasAnimal.Marshal(b, m, deterministic)
} else {
b = b[:cap(b)]
n, err := m.MarshalToSizedBuffer(b)
if err != nil {
return nil, err
}
return b[:n], nil
}
}
func (m *HasHasHasAnimal) XXX_Merge(src proto.Message) {
xxx_messageInfo_HasHasHasAnimal.Merge(m, src)
}
func (m *HasHasHasAnimal) XXX_Size() int {
return m.Size()
}
func (m *HasHasHasAnimal) XXX_DiscardUnknown() {
xxx_messageInfo_HasHasHasAnimal.DiscardUnknown(m)
}
var xxx_messageInfo_HasHasHasAnimal proto.InternalMessageInfo
func (m *HasHasHasAnimal) GetHasHasAnimal() *types.Any {
if m != nil {
return m.HasHasAnimal
}
return nil
}
func init() {
proto.RegisterType((*Dog)(nil), "cosmos_sdk.codec.v1.Dog")
proto.RegisterType((*Cat)(nil), "cosmos_sdk.codec.v1.Cat")
proto.RegisterType((*HasAnimal)(nil), "cosmos_sdk.codec.v1.HasAnimal")
proto.RegisterType((*HasHasAnimal)(nil), "cosmos_sdk.codec.v1.HasHasAnimal")
proto.RegisterType((*HasHasHasAnimal)(nil), "cosmos_sdk.codec.v1.HasHasHasAnimal")
}
func init() { proto.RegisterFile("codec/testdata/proto.proto", fileDescriptor_ae1353846770e6e2) }
var fileDescriptor_ae1353846770e6e2 = []byte{
// 264 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x54, 0x90, 0xbd, 0x4e, 0xc3, 0x30,
0x14, 0x85, 0x63, 0x42, 0x8b, 0x6a, 0x98, 0x4c, 0x87, 0xd0, 0xc1, 0x42, 0x99, 0x90, 0xa0, 0xb6,
0x00, 0xf1, 0x00, 0xe5, 0x47, 0x30, 0x67, 0x64, 0x41, 0x4e, 0x62, 0x82, 0x95, 0xd8, 0x17, 0xd5,
0x6e, 0xd5, 0xf2, 0x14, 0x3c, 0x16, 0x63, 0x47, 0x46, 0x94, 0xbc, 0x08, 0xaa, 0x9d, 0x0e, 0x5d,
0xec, 0x73, 0xaf, 0xbe, 0x63, 0x1f, 0x1d, 0x3c, 0x29, 0xa0, 0x94, 0x05, 0x77, 0xd2, 0xba, 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304 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x8c, 0x91, 0xbf, 0x4e, 0xc3, 0x30,
0x10, 0xc6, 0x6b, 0x4a, 0x8b, 0x7a, 0x54, 0x20, 0x99, 0x0e, 0xa1, 0x83, 0x85, 0x32, 0x21, 0x41,
0x1d, 0x41, 0xc5, 0xc2, 0x56, 0x0a, 0xa2, 0x0b, 0x4b, 0x46, 0x96, 0xca, 0x49, 0x4c, 0x12, 0xe5,
0x8f, 0x51, 0xed, 0x56, 0x2d, 0x4f, 0xc1, 0x63, 0x31, 0x76, 0x64, 0x44, 0xc9, 0x8b, 0xa0, 0xd8,
0x89, 0x0a, 0x5b, 0x17, 0xfb, 0xbb, 0xd3, 0xf7, 0xfd, 0xee, 0xa4, 0x83, 0xa1, 0x2f, 0x02, 0xee,
0x3b, 0x8a, 0x4b, 0x15, 0x30, 0xc5, 0x9c, 0xf7, 0x85, 0x50, 0x82, 0xea, 0x17, 0x9f, 0xf9, 0x42,
0x66, 0x42, 0xce, 0x65, 0x90, 0x50, 0x6d, 0xa3, 0xab, 0x9b, 0xe1, 0x79, 0x28, 0x44, 0x98, 0x72,
0x63, 0xf4, 0x96, 0x6f, 0x0e, 0xcb, 0x37, 0xc6, 0x6f, 0x8f, 0xa0, 0xfd, 0x28, 0x42, 0x8c, 0xe1,
0x50, 0xc6, 0x1f, 0xdc, 0x42, 0x17, 0xe8, 0xb2, 0xe7, 0x6a, 0x5d, 0xf5, 0x72, 0x96, 0x71, 0xeb,
0xc0, 0xf4, 0x2a, 0x6d, 0xdf, 0x41, 0x7b, 0xca, 0x14, 0xb6, 0xe0, 0x28, 0x13, 0x79, 0x9c, 0xf0,
0x45, 0x9d, 0x68, 0x4a, 0x3c, 0x80, 0x4e, 0x1a, 0xaf, 0xb8, 0xd4, 0xa9, 0x8e, 0x6b, 0x0a, 0xfb,
0x19, 0x7a, 0x33, 0x26, 0x27, 0x79, 0x9c, 0xb1, 0x14, 0x5f, 0x43, 0x97, 0x69, 0xa5, 0xb3, 0xc7,
0xb7, 0x03, 0x6a, 0xd6, 0xa3, 0xcd, 0x7a, 0x74, 0x92, 0x6f, 0xdc, 0xda, 0x83, 0xfb, 0x80, 0xd6,
0x1a, 0xd6, 0x76, 0xd1, 0xda, 0x9e, 0x42, 0x7f, 0xc6, 0xe4, 0x8e, 0x35, 0x06, 0x88, 0x98, 0x9c,
0xef, 0xc1, 0xeb, 0x45, 0x4d, 0xc8, 0x7e, 0x81, 0x53, 0x03, 0xd9, 0x71, 0xee, 0xe1, 0xa4, 0xe2,
0xec, 0xc9, 0xea, 0x47, 0x7f, 0xb2, 0x0f, 0x4f, 0x5f, 0x05, 0x41, 0xdb, 0x82, 0xa0, 0x9f, 0x82,
0xa0, 0xcf, 0x92, 0xb4, 0xb6, 0x25, 0x69, 0x7d, 0x97, 0xa4, 0xf5, 0x7a, 0x15, 0xc6, 0x2a, 0x5a,
0x7a, 0xd4, 0x17, 0x99, 0x63, 0xee, 0x52, 0x7f, 0x23, 0x19, 0x24, 0xce, 0xff, 0x2b, 0x7a, 0x5d,
0x3d, 0x62, 0xfc, 0x1b, 0x00, 0x00, 0xff, 0xff, 0x65, 0xc6, 0xd8, 0xe9, 0xde, 0x01, 0x00, 0x00,
}
func (m *Dog) Marshal() (dAtA []byte, err error) {
@@ -320,6 +412,76 @@ func (m *HasAnimal) MarshalToSizedBuffer(dAtA []byte) (int, error) {
return len(dAtA) - i, nil
}
func (m *HasHasAnimal) Marshal() (dAtA []byte, err error) {
size := m.Size()
dAtA = make([]byte, size)
n, err := m.MarshalToSizedBuffer(dAtA[:size])
if err != nil {
return nil, err
}
return dAtA[:n], nil
}
func (m *HasHasAnimal) MarshalTo(dAtA []byte) (int, error) {
size := m.Size()
return m.MarshalToSizedBuffer(dAtA[:size])
}
func (m *HasHasAnimal) MarshalToSizedBuffer(dAtA []byte) (int, error) {
i := len(dAtA)
_ = i
var l int
_ = l
if m.HasAnimal != nil {
{
size, err := m.HasAnimal.MarshalToSizedBuffer(dAtA[:i])
if err != nil {
return 0, err
}
i -= size
i = encodeVarintProto(dAtA, i, uint64(size))
}
i--
dAtA[i] = 0xa
}
return len(dAtA) - i, nil
}
func (m *HasHasHasAnimal) Marshal() (dAtA []byte, err error) {
size := m.Size()
dAtA = make([]byte, size)
n, err := m.MarshalToSizedBuffer(dAtA[:size])
if err != nil {
return nil, err
}
return dAtA[:n], nil
}
func (m *HasHasHasAnimal) MarshalTo(dAtA []byte) (int, error) {
size := m.Size()
return m.MarshalToSizedBuffer(dAtA[:size])
}
func (m *HasHasHasAnimal) MarshalToSizedBuffer(dAtA []byte) (int, error) {
i := len(dAtA)
_ = i
var l int
_ = l
if m.HasHasAnimal != nil {
{
size, err := m.HasHasAnimal.MarshalToSizedBuffer(dAtA[:i])
if err != nil {
return 0, err
}
i -= size
i = encodeVarintProto(dAtA, i, uint64(size))
}
i--
dAtA[i] = 0xa
}
return len(dAtA) - i, nil
}
func encodeVarintProto(dAtA []byte, offset int, v uint64) int {
offset -= sovProto(v)
base := offset
@@ -380,6 +542,32 @@ func (m *HasAnimal) Size() (n int) {
return n
}
func (m *HasHasAnimal) Size() (n int) {
if m == nil {
return 0
}
var l int
_ = l
if m.HasAnimal != nil {
l = m.HasAnimal.Size()
n += 1 + l + sovProto(uint64(l))
}
return n
}
func (m *HasHasHasAnimal) Size() (n int) {
if m == nil {
return 0
}
var l int
_ = l
if m.HasHasAnimal != nil {
l = m.HasHasAnimal.Size()
n += 1 + l + sovProto(uint64(l))
}
return n
}
func sovProto(x uint64) (n int) {
return (math_bits.Len64(x|1) + 6) / 7
}
@@ -715,6 +903,184 @@ func (m *HasAnimal) Unmarshal(dAtA []byte) error {
}
return nil
}
func (m *HasHasAnimal) Unmarshal(dAtA []byte) error {
l := len(dAtA)
iNdEx := 0
for iNdEx < l {
preIndex := iNdEx
var wire uint64
for shift := uint(0); ; shift += 7 {
if shift >= 64 {
return ErrIntOverflowProto
}
if iNdEx >= l {
return io.ErrUnexpectedEOF
}
b := dAtA[iNdEx]
iNdEx++
wire |= uint64(b&0x7F) << shift
if b < 0x80 {
break
}
}
fieldNum := int32(wire >> 3)
wireType := int(wire & 0x7)
if wireType == 4 {
return fmt.Errorf("proto: HasHasAnimal: wiretype end group for non-group")
}
if fieldNum <= 0 {
return fmt.Errorf("proto: HasHasAnimal: illegal tag %d (wire type %d)", fieldNum, wire)
}
switch fieldNum {
case 1:
if wireType != 2 {
return fmt.Errorf("proto: wrong wireType = %d for field HasAnimal", wireType)
}
var msglen int
for shift := uint(0); ; shift += 7 {
if shift >= 64 {
return ErrIntOverflowProto
}
if iNdEx >= l {
return io.ErrUnexpectedEOF
}
b := dAtA[iNdEx]
iNdEx++
msglen |= int(b&0x7F) << shift
if b < 0x80 {
break
}
}
if msglen < 0 {
return ErrInvalidLengthProto
}
postIndex := iNdEx + msglen
if postIndex < 0 {
return ErrInvalidLengthProto
}
if postIndex > l {
return io.ErrUnexpectedEOF
}
if m.HasAnimal == nil {
m.HasAnimal = &types.Any{}
}
if err := m.HasAnimal.Unmarshal(dAtA[iNdEx:postIndex]); err != nil {
return err
}
iNdEx = postIndex
default:
iNdEx = preIndex
skippy, err := skipProto(dAtA[iNdEx:])
if err != nil {
return err
}
if skippy < 0 {
return ErrInvalidLengthProto
}
if (iNdEx + skippy) < 0 {
return ErrInvalidLengthProto
}
if (iNdEx + skippy) > l {
return io.ErrUnexpectedEOF
}
iNdEx += skippy
}
}
if iNdEx > l {
return io.ErrUnexpectedEOF
}
return nil
}
func (m *HasHasHasAnimal) Unmarshal(dAtA []byte) error {
l := len(dAtA)
iNdEx := 0
for iNdEx < l {
preIndex := iNdEx
var wire uint64
for shift := uint(0); ; shift += 7 {
if shift >= 64 {
return ErrIntOverflowProto
}
if iNdEx >= l {
return io.ErrUnexpectedEOF
}
b := dAtA[iNdEx]
iNdEx++
wire |= uint64(b&0x7F) << shift
if b < 0x80 {
break
}
}
fieldNum := int32(wire >> 3)
wireType := int(wire & 0x7)
if wireType == 4 {
return fmt.Errorf("proto: HasHasHasAnimal: wiretype end group for non-group")
}
if fieldNum <= 0 {
return fmt.Errorf("proto: HasHasHasAnimal: illegal tag %d (wire type %d)", fieldNum, wire)
}
switch fieldNum {
case 1:
if wireType != 2 {
return fmt.Errorf("proto: wrong wireType = %d for field HasHasAnimal", wireType)
}
var msglen int
for shift := uint(0); ; shift += 7 {
if shift >= 64 {
return ErrIntOverflowProto
}
if iNdEx >= l {
return io.ErrUnexpectedEOF
}
b := dAtA[iNdEx]
iNdEx++
msglen |= int(b&0x7F) << shift
if b < 0x80 {
break
}
}
if msglen < 0 {
return ErrInvalidLengthProto
}
postIndex := iNdEx + msglen
if postIndex < 0 {
return ErrInvalidLengthProto
}
if postIndex > l {
return io.ErrUnexpectedEOF
}
if m.HasHasAnimal == nil {
m.HasHasAnimal = &types.Any{}
}
if err := m.HasHasAnimal.Unmarshal(dAtA[iNdEx:postIndex]); err != nil {
return err
}
iNdEx = postIndex
default:
iNdEx = preIndex
skippy, err := skipProto(dAtA[iNdEx:])
if err != nil {
return err
}
if skippy < 0 {
return ErrInvalidLengthProto
}
if (iNdEx + skippy) < 0 {
return ErrInvalidLengthProto
}
if (iNdEx + skippy) > l {
return io.ErrUnexpectedEOF
}
iNdEx += skippy
}
}
if iNdEx > l {
return io.ErrUnexpectedEOF
}
return nil
}
func skipProto(dAtA []byte) (n int, err error) {
l := len(dAtA)
iNdEx := 0
+8
View File
@@ -19,3 +19,11 @@ message HasAnimal {
google.protobuf.Any animal = 1;
int64 x = 2;
}
message HasHasAnimal {
google.protobuf.Any has_animal = 1;
}
message HasHasHasAnimal {
google.protobuf.Any has_has_animal = 1;
}