From 191b3c8cb12797bb0ce021b1db73ea88c74f045d Mon Sep 17 00:00:00 2001 From: fdymylja Date: Thu, 2 Dec 2021 18:54:14 +0100 Subject: [PATCH 1/4] add: init custom types --- Makefile | 2 +- cosmos_proto/cosmos.pulsar.go | 50 +++-- features/protoc/custom_type.go | 32 +++ features/protoc/main.go | 5 + internal/testprotos/test3/test.pulsar.go | 2 +- .../testprotos/test3/test_import.pulsar.go | 2 +- proto/cosmos_proto/cosmos.proto | 4 + runtime/runtime.go | 12 ++ testpb/1.proto | 3 + testpb/1.pulsar.go | 195 ++++++++++++------ testpb/custom_type.go | 4 + 11 files changed, 234 insertions(+), 77 deletions(-) create mode 100644 features/protoc/custom_type.go create mode 100644 testpb/custom_type.go diff --git a/Makefile b/Makefile index 9d12b8b..8490237 100644 --- a/Makefile +++ b/Makefile @@ -4,4 +4,4 @@ pulsar: docker build -t dev:proto-build -f Dockerfile . docker run -v "$(CURDIR):/genproto" -w /genproto dev:proto-build ./scripts/fastreflect.sh "$(DIRECTORIES_TO_BUILD)" -.PHONY: proto fastreflection \ No newline at end of file +.PHONY: pulsar \ No newline at end of file diff --git a/cosmos_proto/cosmos.pulsar.go b/cosmos_proto/cosmos.pulsar.go index 4c3b30f..abbff93 100644 --- a/cosmos_proto/cosmos.pulsar.go +++ b/cosmos_proto/cosmos.pulsar.go @@ -45,6 +45,14 @@ var file_cosmos_proto_cosmos_proto_extTypes = []protoimpl.ExtensionInfo{ Tag: "bytes,93002,opt,name=scalar", Filename: "cosmos_proto/cosmos.proto", }, + { + ExtendedType: (*descriptorpb.FieldOptions)(nil), + ExtensionType: (*string)(nil), + Field: 93003, + Name: "cosmos_proto.go_type", + Tag: "bytes,93003,opt,name=go_type", + Filename: "cosmos_proto/cosmos.proto", + }, } // Extension fields to descriptorpb.MessageOptions. @@ -65,6 +73,11 @@ var ( // // optional string scalar = 93002; E_Scalar = &file_cosmos_proto_cosmos_proto_extTypes[2] + // go_type defines a custom go type for the field. + // NOTE: it is not valid for repeated and map fields. + // + // optional string go_type = 93003; + E_GoType = &file_cosmos_proto_cosmos_proto_extTypes[3] ) var File_cosmos_proto_cosmos_proto protoreflect.FileDescriptor @@ -88,17 +101,21 @@ var file_cosmos_proto_cosmos_proto_rawDesc = []byte{ 0x37, 0x0a, 0x06, 0x73, 0x63, 0x61, 0x6c, 0x61, 0x72, 0x12, 0x1d, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x46, 0x69, 0x65, 0x6c, 0x64, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x18, 0xca, 0xd6, 0x05, 0x20, 0x01, 0x28, 0x09, - 0x52, 0x06, 0x73, 0x63, 0x61, 0x6c, 0x61, 0x72, 0x42, 0x98, 0x01, 0x0a, 0x10, 0x63, 0x6f, 0x6d, - 0x2e, 0x63, 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x5f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x42, 0x0b, 0x43, - 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x50, 0x01, 0x5a, 0x2b, 0x67, 0x69, - 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x63, 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x2f, - 0x63, 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x2d, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x63, 0x6f, 0x73, - 0x6d, 0x6f, 0x73, 0x5f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0xa2, 0x02, 0x03, 0x43, 0x58, 0x58, 0xaa, - 0x02, 0x0b, 0x43, 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0xca, 0x02, 0x0b, - 0x43, 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0xe2, 0x02, 0x17, 0x43, 0x6f, - 0x73, 0x6d, 0x6f, 0x73, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x5c, 0x47, 0x50, 0x42, 0x4d, 0x65, 0x74, - 0x61, 0x64, 0x61, 0x74, 0x61, 0xea, 0x02, 0x0b, 0x43, 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x50, 0x72, - 0x6f, 0x74, 0x6f, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x52, 0x06, 0x73, 0x63, 0x61, 0x6c, 0x61, 0x72, 0x3a, 0x38, 0x0a, 0x07, 0x67, 0x6f, 0x5f, 0x74, + 0x79, 0x70, 0x65, 0x12, 0x1d, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, + 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x46, 0x69, 0x65, 0x6c, 0x64, 0x4f, 0x70, 0x74, 0x69, 0x6f, + 0x6e, 0x73, 0x18, 0xcb, 0xd6, 0x05, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x67, 0x6f, 0x54, 0x79, + 0x70, 0x65, 0x42, 0x98, 0x01, 0x0a, 0x10, 0x63, 0x6f, 0x6d, 0x2e, 0x63, 0x6f, 0x73, 0x6d, 0x6f, + 0x73, 0x5f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x42, 0x0b, 0x43, 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x50, + 0x72, 0x6f, 0x74, 0x6f, 0x50, 0x01, 0x5a, 0x2b, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, + 0x6f, 0x6d, 0x2f, 0x63, 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x2f, 0x63, 0x6f, 0x73, 0x6d, 0x6f, 0x73, + 0x2d, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x63, 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x5f, 0x70, 0x72, + 0x6f, 0x74, 0x6f, 0xa2, 0x02, 0x03, 0x43, 0x58, 0x58, 0xaa, 0x02, 0x0b, 0x43, 0x6f, 0x73, 0x6d, + 0x6f, 0x73, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0xca, 0x02, 0x0b, 0x43, 0x6f, 0x73, 0x6d, 0x6f, 0x73, + 0x50, 0x72, 0x6f, 0x74, 0x6f, 0xe2, 0x02, 0x17, 0x43, 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x50, 0x72, + 0x6f, 0x74, 0x6f, 0x5c, 0x47, 0x50, 0x42, 0x4d, 0x65, 0x74, 0x61, 0x64, 0x61, 0x74, 0x61, 0xea, + 0x02, 0x0b, 0x43, 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x06, 0x70, + 0x72, 0x6f, 0x74, 0x6f, 0x33, } var file_cosmos_proto_cosmos_proto_goTypes = []interface{}{ @@ -109,10 +126,11 @@ var file_cosmos_proto_cosmos_proto_depIdxs = []int32{ 0, // 0: cosmos_proto.implements_interface:extendee -> google.protobuf.MessageOptions 1, // 1: cosmos_proto.accepts_interface:extendee -> google.protobuf.FieldOptions 1, // 2: cosmos_proto.scalar:extendee -> google.protobuf.FieldOptions - 3, // [3:3] is the sub-list for method output_type - 3, // [3:3] is the sub-list for method input_type - 3, // [3:3] is the sub-list for extension type_name - 0, // [0:3] is the sub-list for extension extendee + 1, // 3: cosmos_proto.go_type:extendee -> google.protobuf.FieldOptions + 4, // [4:4] is the sub-list for method output_type + 4, // [4:4] is the sub-list for method input_type + 4, // [4:4] is the sub-list for extension type_name + 0, // [0:4] is the sub-list for extension extendee 0, // [0:0] is the sub-list for field type_name } @@ -128,7 +146,7 @@ func file_cosmos_proto_cosmos_proto_init() { RawDescriptor: file_cosmos_proto_cosmos_proto_rawDesc, NumEnums: 0, NumMessages: 0, - NumExtensions: 3, + NumExtensions: 4, NumServices: 0, }, GoTypes: file_cosmos_proto_cosmos_proto_goTypes, diff --git a/features/protoc/custom_type.go b/features/protoc/custom_type.go new file mode 100644 index 0000000..f924668 --- /dev/null +++ b/features/protoc/custom_type.go @@ -0,0 +1,32 @@ +package protoc + +import ( + "github.com/cosmos/cosmos-proto/cosmos_proto" + "github.com/cosmos/cosmos-proto/generator" + "google.golang.org/protobuf/compiler/protogen" + "google.golang.org/protobuf/proto" + "strings" +) + +func isCustomType(field *protogen.Field) bool { + fd := field.Desc + t := proto.GetExtension(fd.Options(), cosmos_proto.E_GoType).(string) + return t != "" +} + +func customFieldType(g *generator.GeneratedFile, field *protogen.Field) string { + if field.Desc.IsList() || field.Desc.IsMap() { + panic("invalid custom type") // TODO(fdymylja): better err msg + } + t := proto.GetExtension(field.Desc.Options(), cosmos_proto.E_GoType).(string) + switch { + case !strings.Contains(t, "."): + return "*" + t + default: + s := strings.Split(t, ".") + typeName := s[len(s)-1] + pkg := protogen.GoImportPath(strings.Join(s[0:len(s)-1], ".")) + g.Import(pkg) + return "*" + g.QualifiedGoIdent(pkg.Ident(typeName)) + } +} diff --git a/features/protoc/main.go b/features/protoc/main.go index 3cc5a1a..7c7feaf 100644 --- a/features/protoc/main.go +++ b/features/protoc/main.go @@ -1688,6 +1688,11 @@ func fieldGoType(g *generator.GeneratedFile, f *fileInfo, field *protogen.Field) } pointer = field.Desc.HasPresence() + if isCustomType(field) { + goType = customFieldType(g, field) + return + } + switch field.Desc.Kind() { case protoreflect.BoolKind: goType = "bool" diff --git a/internal/testprotos/test3/test.pulsar.go b/internal/testprotos/test3/test.pulsar.go index a181a59..87d4b0f 100644 --- a/internal/testprotos/test3/test.pulsar.go +++ b/internal/testprotos/test3/test.pulsar.go @@ -11221,7 +11221,7 @@ func (x *fastReflection_ForeignMessage) ProtoMethods() *protoiface.Methods { // Code generated by protoc-gen-go. DO NOT EDIT. // versions: // protoc-gen-go v1.27.0 -// protoc v3.15.7 +// protoc v3.18.1 // source: internal/testprotos/test3/test.proto const ( diff --git a/internal/testprotos/test3/test_import.pulsar.go b/internal/testprotos/test3/test_import.pulsar.go index d8dc8f9..24509ce 100644 --- a/internal/testprotos/test3/test_import.pulsar.go +++ b/internal/testprotos/test3/test_import.pulsar.go @@ -374,7 +374,7 @@ func (x *fastReflection_ImportMessage) ProtoMethods() *protoiface.Methods { // Code generated by protoc-gen-go. DO NOT EDIT. // versions: // protoc-gen-go v1.27.0 -// protoc v3.15.7 +// protoc v3.18.1 // source: internal/testprotos/test3/test_import.proto const ( diff --git a/proto/cosmos_proto/cosmos.proto b/proto/cosmos_proto/cosmos.proto index 682d98a..befc951 100644 --- a/proto/cosmos_proto/cosmos.proto +++ b/proto/cosmos_proto/cosmos.proto @@ -15,4 +15,8 @@ extend google.protobuf.FieldOptions { string accepts_interface = 93001; // scalar is used to define scalar types string scalar = 93002; + + // go_type defines a custom go type for the field. + // NOTE: it is not valid for repeated and map fields. + string go_type = 93003; } diff --git a/runtime/runtime.go b/runtime/runtime.go index 7a6e4a0..5338cc4 100644 --- a/runtime/runtime.go +++ b/runtime/runtime.go @@ -3,11 +3,23 @@ package runtime import ( "fmt" "google.golang.org/protobuf/proto" + "google.golang.org/protobuf/reflect/protoreflect" "google.golang.org/protobuf/runtime/protoiface" "io" "math/bits" ) +type CustomType interface { + UnmarshalBytes(in protoiface.UnmarshalInput, b []byte) (out protoiface.UnmarshalOutput, err error) + MarshalBytes(in protoiface.MarshalOutput) (out protoiface.MarshalOutput, err error) + Size(in protoiface.SizeInput) (out protoiface.SizeOutput, err error) + Set(value protoreflect.Value) + Get(value protoreflect.Value) + Clear() + IsSet() bool + Mutable() protoreflect.Value +} + func Sov(x uint64) (n int) { return (bits.Len64(x|1) + 6) / 7 } diff --git a/testpb/1.proto b/testpb/1.proto index 4504a52..b7185d7 100644 --- a/testpb/1.proto +++ b/testpb/1.proto @@ -3,6 +3,7 @@ syntax="proto3"; option go_package = "github.com/cosmos/cosmos-proto/testpb"; import "google/protobuf/descriptor.proto"; +import "proto/cosmos_proto/cosmos.proto"; enum Enumeration { One = 0; @@ -35,6 +36,8 @@ message A { }; repeated Enumeration LIST_ENUM = 22; google.protobuf.FileDescriptorProto imported = 23; + + string custom_type = 24 [(cosmos_proto.go_type) = "CustomType"]; } message B { diff --git a/testpb/1.pulsar.go b/testpb/1.pulsar.go index 28499e4..178b081 100644 --- a/testpb/1.pulsar.go +++ b/testpb/1.pulsar.go @@ -227,6 +227,7 @@ var ( fd_A_ONEOF_STRING protoreflect.FieldDescriptor fd_A_LIST_ENUM protoreflect.FieldDescriptor fd_A_imported protoreflect.FieldDescriptor + fd_A_custom_type protoreflect.FieldDescriptor ) func init() { @@ -255,6 +256,7 @@ func init() { fd_A_ONEOF_STRING = md_A.Fields().ByName("ONEOF_STRING") fd_A_LIST_ENUM = md_A.Fields().ByName("LIST_ENUM") fd_A_imported = md_A.Fields().ByName("imported") + fd_A_custom_type = md_A.Fields().ByName("custom_type") } var _ protoreflect.Message = (*fastReflection_A)(nil) @@ -464,6 +466,12 @@ func (x *fastReflection_A) Range(f func(protoreflect.FieldDescriptor, protorefle return } } + if x.CustomType != "" { + value := protoreflect.ValueOfString(x.CustomType) + if !f(fd_A_custom_type, value) { + return + } + } } // Has reports whether a field is populated. @@ -537,6 +545,8 @@ func (x *fastReflection_A) Has(fd protoreflect.FieldDescriptor) bool { return len(x.LIST_ENUM) != 0 case "A.imported": return x.Imported != nil + case "A.custom_type": + return x.CustomType != "" default: if fd.IsExtension() { panic(fmt.Errorf("proto3 declared messages do not support extensions: A")) @@ -599,6 +609,8 @@ func (x *fastReflection_A) Clear(fd protoreflect.FieldDescriptor) { x.LIST_ENUM = nil case "A.imported": x.Imported = nil + case "A.custom_type": + x.CustomType = "" default: if fd.IsExtension() { panic(fmt.Errorf("proto3 declared messages do not support extensions: A")) @@ -703,6 +715,9 @@ func (x *fastReflection_A) Get(descriptor protoreflect.FieldDescriptor) protoref case "A.imported": value := x.Imported return protoreflect.ValueOfMessage(value.ProtoReflect()) + case "A.custom_type": + value := x.CustomType + return protoreflect.ValueOfString(value) default: if descriptor.IsExtension() { panic(fmt.Errorf("proto3 declared messages do not support extensions: A")) @@ -777,6 +792,8 @@ func (x *fastReflection_A) Set(fd protoreflect.FieldDescriptor, value protorefle x.LIST_ENUM = *clv.list case "A.imported": x.Imported = value.Message().Interface().(*descriptorpb.FileDescriptorProto) + case "A.custom_type": + x.CustomType = value.Interface().(string) default: if fd.IsExtension() { panic(fmt.Errorf("proto3 declared messages do not support extensions: A")) @@ -875,6 +892,8 @@ func (x *fastReflection_A) Mutable(fd protoreflect.FieldDescriptor) protoreflect panic(fmt.Errorf("field BYTES of message A is not mutable")) case "A.ONEOF_STRING": panic(fmt.Errorf("field ONEOF_STRING of message A is not mutable")) + case "A.custom_type": + panic(fmt.Errorf("field custom_type of message A is not mutable")) default: if fd.IsExtension() { panic(fmt.Errorf("proto3 declared messages do not support extensions: A")) @@ -940,6 +959,8 @@ func (x *fastReflection_A) NewField(fd protoreflect.FieldDescriptor) protoreflec case "A.imported": m := new(descriptorpb.FileDescriptorProto) return protoreflect.ValueOfMessage(m.ProtoReflect()) + case "A.custom_type": + return protoreflect.ValueOfString("") default: if fd.IsExtension() { panic(fmt.Errorf("proto3 declared messages do not support extensions: A")) @@ -1130,6 +1151,10 @@ func (x *fastReflection_A) ProtoMethods() *protoiface.Methods { l = options.Size(x.Imported) n += 2 + l + runtime.Sov(uint64(l)) } + l = len(x.CustomType) + if l > 0 { + n += 2 + l + runtime.Sov(uint64(l)) + } if x.unknownFields != nil { n += len(x.unknownFields) } @@ -1184,6 +1209,15 @@ func (x *fastReflection_A) ProtoMethods() *protoiface.Methods { i-- dAtA[i] = 0xaa } + if len(x.CustomType) > 0 { + i -= len(x.CustomType) + copy(dAtA[i:], x.CustomType) + i = runtime.EncodeVarint(dAtA, i, uint64(len(x.CustomType))) + i-- + dAtA[i] = 0x1 + i-- + dAtA[i] = 0xc2 + } if x.Imported != nil { encoded, err := options.Marshal(x.Imported) if err != nil { @@ -2111,6 +2145,38 @@ func (x *fastReflection_A) ProtoMethods() *protoiface.Methods { return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, err } iNdEx = postIndex + case 24: + if wireType != 2 { + return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, fmt.Errorf("proto: wrong wireType = %d for field CustomType", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, runtime.ErrIntOverflow + } + if iNdEx >= l { + return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, runtime.ErrInvalidLength + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, runtime.ErrInvalidLength + } + if postIndex > l { + return protoiface.UnmarshalOutput{NoUnkeyedLiterals: input.NoUnkeyedLiterals, Flags: input.Flags}, io.ErrUnexpectedEOF + } + x.CustomType = string(dAtA[iNdEx:postIndex]) + iNdEx = postIndex default: iNdEx = preIndex skippy, err := runtime.Skip(dAtA[iNdEx:]) @@ -2569,7 +2635,7 @@ func (x *fastReflection_B) ProtoMethods() *protoiface.Methods { // Code generated by protoc-gen-go. DO NOT EDIT. // versions: // protoc-gen-go v1.27.0 -// protoc v3.15.7 +// protoc v3.18.1 // source: testpb/1.proto const ( @@ -2652,9 +2718,10 @@ type A struct { // Types that are assignable to ONEOF: // *A_ONEOF_B // *A_ONEOF_STRING - ONEOF isA_ONEOF `protobuf_oneof:"ONEOF"` - LIST_ENUM []Enumeration `protobuf:"varint,22,rep,packed,name=LIST_ENUM,json=LISTENUM,proto3,enum=Enumeration" json:"LIST_ENUM,omitempty"` - Imported *descriptorpb.FileDescriptorProto `protobuf:"bytes,23,opt,name=imported,proto3" json:"imported,omitempty"` + ONEOF isA_ONEOF `protobuf_oneof:"ONEOF"` + LIST_ENUM []Enumeration `protobuf:"varint,22,rep,packed,name=LIST_ENUM,json=LISTENUM,proto3,enum=Enumeration" json:"LIST_ENUM,omitempty"` + Imported *descriptorpb.FileDescriptorProto `protobuf:"bytes,23,opt,name=imported,proto3" json:"imported,omitempty"` + CustomType *CustomType `protobuf:"bytes,24,opt,name=custom_type,json=customType,proto3" json:"custom_type,omitempty"` } func (x *A) Reset() { @@ -2845,6 +2912,13 @@ func (x *A) GetImported() *descriptorpb.FileDescriptorProto { return nil } +func (x *A) GetCustomType() *CustomType { + if x != nil { + return x.CustomType + } + return "" +} + type isA_ONEOF interface { isA_ONEOF() } @@ -2902,60 +2976,65 @@ var file_testpb_1_proto_rawDesc = []byte{ 0x0a, 0x0e, 0x74, 0x65, 0x73, 0x74, 0x70, 0x62, 0x2f, 0x31, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x1a, 0x20, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2f, 0x64, 0x65, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x6f, 0x72, 0x2e, 0x70, 0x72, 0x6f, - 0x74, 0x6f, 0x22, 0xe7, 0x05, 0x0a, 0x01, 0x41, 0x12, 0x20, 0x0a, 0x04, 0x65, 0x6e, 0x75, 0x6d, - 0x18, 0x01, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x0c, 0x2e, 0x45, 0x6e, 0x75, 0x6d, 0x65, 0x72, 0x61, - 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x04, 0x65, 0x6e, 0x75, 0x6d, 0x12, 0x21, 0x0a, 0x0c, 0x73, 0x6f, - 0x6d, 0x65, 0x5f, 0x62, 0x6f, 0x6f, 0x6c, 0x65, 0x61, 0x6e, 0x18, 0x02, 0x20, 0x01, 0x28, 0x08, - 0x52, 0x0b, 0x73, 0x6f, 0x6d, 0x65, 0x42, 0x6f, 0x6f, 0x6c, 0x65, 0x61, 0x6e, 0x12, 0x14, 0x0a, - 0x05, 0x49, 0x4e, 0x54, 0x33, 0x32, 0x18, 0x03, 0x20, 0x01, 0x28, 0x05, 0x52, 0x05, 0x49, 0x4e, - 0x54, 0x33, 0x32, 0x12, 0x16, 0x0a, 0x06, 0x53, 0x49, 0x4e, 0x54, 0x33, 0x32, 0x18, 0x04, 0x20, - 0x01, 0x28, 0x11, 0x52, 0x06, 0x53, 0x49, 0x4e, 0x54, 0x33, 0x32, 0x12, 0x16, 0x0a, 0x06, 0x55, - 0x49, 0x4e, 0x54, 0x33, 0x32, 0x18, 0x05, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x06, 0x55, 0x49, 0x4e, - 0x54, 0x33, 0x32, 0x12, 0x14, 0x0a, 0x05, 0x49, 0x4e, 0x54, 0x36, 0x34, 0x18, 0x06, 0x20, 0x01, - 0x28, 0x03, 0x52, 0x05, 0x49, 0x4e, 0x54, 0x36, 0x34, 0x12, 0x16, 0x0a, 0x06, 0x53, 0x49, 0x4e, - 0x47, 0x36, 0x34, 0x18, 0x07, 0x20, 0x01, 0x28, 0x12, 0x52, 0x06, 0x53, 0x49, 0x4e, 0x47, 0x36, - 0x34, 0x12, 0x16, 0x0a, 0x06, 0x55, 0x49, 0x4e, 0x54, 0x36, 0x34, 0x18, 0x08, 0x20, 0x01, 0x28, - 0x04, 0x52, 0x06, 0x55, 0x49, 0x4e, 0x54, 0x36, 0x34, 0x12, 0x1a, 0x0a, 0x08, 0x53, 0x46, 0x49, - 0x58, 0x45, 0x44, 0x33, 0x32, 0x18, 0x09, 0x20, 0x01, 0x28, 0x0f, 0x52, 0x08, 0x53, 0x46, 0x49, - 0x58, 0x45, 0x44, 0x33, 0x32, 0x12, 0x18, 0x0a, 0x07, 0x46, 0x49, 0x58, 0x45, 0x44, 0x33, 0x32, - 0x18, 0x0a, 0x20, 0x01, 0x28, 0x07, 0x52, 0x07, 0x46, 0x49, 0x58, 0x45, 0x44, 0x33, 0x32, 0x12, - 0x14, 0x0a, 0x05, 0x46, 0x4c, 0x4f, 0x41, 0x54, 0x18, 0x0b, 0x20, 0x01, 0x28, 0x02, 0x52, 0x05, - 0x46, 0x4c, 0x4f, 0x41, 0x54, 0x12, 0x1a, 0x0a, 0x08, 0x53, 0x46, 0x49, 0x58, 0x45, 0x44, 0x36, - 0x34, 0x18, 0x0c, 0x20, 0x01, 0x28, 0x10, 0x52, 0x08, 0x53, 0x46, 0x49, 0x58, 0x45, 0x44, 0x36, - 0x34, 0x12, 0x18, 0x0a, 0x07, 0x46, 0x49, 0x58, 0x45, 0x44, 0x36, 0x34, 0x18, 0x0d, 0x20, 0x01, - 0x28, 0x06, 0x52, 0x07, 0x46, 0x49, 0x58, 0x45, 0x44, 0x36, 0x34, 0x12, 0x16, 0x0a, 0x06, 0x44, - 0x4f, 0x55, 0x42, 0x4c, 0x45, 0x18, 0x0e, 0x20, 0x01, 0x28, 0x01, 0x52, 0x06, 0x44, 0x4f, 0x55, - 0x42, 0x4c, 0x45, 0x12, 0x16, 0x0a, 0x06, 0x53, 0x54, 0x52, 0x49, 0x4e, 0x47, 0x18, 0x0f, 0x20, - 0x01, 0x28, 0x09, 0x52, 0x06, 0x53, 0x54, 0x52, 0x49, 0x4e, 0x47, 0x12, 0x14, 0x0a, 0x05, 0x42, - 0x59, 0x54, 0x45, 0x53, 0x18, 0x10, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x05, 0x42, 0x59, 0x54, 0x45, - 0x53, 0x12, 0x1c, 0x0a, 0x07, 0x4d, 0x45, 0x53, 0x53, 0x41, 0x47, 0x45, 0x18, 0x11, 0x20, 0x01, - 0x28, 0x0b, 0x32, 0x02, 0x2e, 0x42, 0x52, 0x07, 0x4d, 0x45, 0x53, 0x53, 0x41, 0x47, 0x45, 0x12, - 0x1d, 0x0a, 0x03, 0x4d, 0x41, 0x50, 0x18, 0x12, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x0b, 0x2e, 0x41, - 0x2e, 0x4d, 0x41, 0x50, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x52, 0x03, 0x4d, 0x41, 0x50, 0x12, 0x16, - 0x0a, 0x04, 0x4c, 0x49, 0x53, 0x54, 0x18, 0x13, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x02, 0x2e, 0x42, - 0x52, 0x04, 0x4c, 0x49, 0x53, 0x54, 0x12, 0x1d, 0x0a, 0x07, 0x4f, 0x4e, 0x45, 0x4f, 0x46, 0x5f, - 0x42, 0x18, 0x14, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x02, 0x2e, 0x42, 0x48, 0x00, 0x52, 0x06, 0x4f, - 0x4e, 0x45, 0x4f, 0x46, 0x42, 0x12, 0x23, 0x0a, 0x0c, 0x4f, 0x4e, 0x45, 0x4f, 0x46, 0x5f, 0x53, - 0x54, 0x52, 0x49, 0x4e, 0x47, 0x18, 0x15, 0x20, 0x01, 0x28, 0x09, 0x48, 0x00, 0x52, 0x0b, 0x4f, - 0x4e, 0x45, 0x4f, 0x46, 0x53, 0x54, 0x52, 0x49, 0x4e, 0x47, 0x12, 0x29, 0x0a, 0x09, 0x4c, 0x49, - 0x53, 0x54, 0x5f, 0x45, 0x4e, 0x55, 0x4d, 0x18, 0x16, 0x20, 0x03, 0x28, 0x0e, 0x32, 0x0c, 0x2e, - 0x45, 0x6e, 0x75, 0x6d, 0x65, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x08, 0x4c, 0x49, 0x53, - 0x54, 0x45, 0x4e, 0x55, 0x4d, 0x12, 0x40, 0x0a, 0x08, 0x69, 0x6d, 0x70, 0x6f, 0x72, 0x74, 0x65, - 0x64, 0x18, 0x17, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x24, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, - 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x46, 0x69, 0x6c, 0x65, 0x44, 0x65, - 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x6f, 0x72, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x52, 0x08, 0x69, - 0x6d, 0x70, 0x6f, 0x72, 0x74, 0x65, 0x64, 0x1a, 0x3a, 0x0a, 0x08, 0x4d, 0x41, 0x50, 0x45, 0x6e, - 0x74, 0x72, 0x79, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, - 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x18, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, - 0x20, 0x01, 0x28, 0x0b, 0x32, 0x02, 0x2e, 0x42, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x3a, - 0x02, 0x38, 0x01, 0x42, 0x07, 0x0a, 0x05, 0x4f, 0x4e, 0x45, 0x4f, 0x46, 0x22, 0x11, 0x0a, 0x01, - 0x42, 0x12, 0x0c, 0x0a, 0x01, 0x78, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x01, 0x78, 0x2a, - 0x1f, 0x0a, 0x0b, 0x45, 0x6e, 0x75, 0x6d, 0x65, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x07, - 0x0a, 0x03, 0x4f, 0x6e, 0x65, 0x10, 0x00, 0x12, 0x07, 0x0a, 0x03, 0x54, 0x77, 0x6f, 0x10, 0x01, - 0x42, 0x27, 0x5a, 0x25, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x63, - 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x2f, 0x63, 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x2d, 0x70, 0x72, 0x6f, - 0x74, 0x6f, 0x2f, 0x74, 0x65, 0x73, 0x74, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, - 0x33, + 0x74, 0x6f, 0x1a, 0x1f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x63, 0x6f, 0x73, 0x6d, 0x6f, 0x73, + 0x5f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x63, 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x2e, 0x70, 0x72, + 0x6f, 0x74, 0x6f, 0x22, 0x98, 0x06, 0x0a, 0x01, 0x41, 0x12, 0x20, 0x0a, 0x04, 0x65, 0x6e, 0x75, + 0x6d, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x0c, 0x2e, 0x45, 0x6e, 0x75, 0x6d, 0x65, 0x72, + 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x04, 0x65, 0x6e, 0x75, 0x6d, 0x12, 0x21, 0x0a, 0x0c, 0x73, + 0x6f, 0x6d, 0x65, 0x5f, 0x62, 0x6f, 0x6f, 0x6c, 0x65, 0x61, 0x6e, 0x18, 0x02, 0x20, 0x01, 0x28, + 0x08, 0x52, 0x0b, 0x73, 0x6f, 0x6d, 0x65, 0x42, 0x6f, 0x6f, 0x6c, 0x65, 0x61, 0x6e, 0x12, 0x14, + 0x0a, 0x05, 0x49, 0x4e, 0x54, 0x33, 0x32, 0x18, 0x03, 0x20, 0x01, 0x28, 0x05, 0x52, 0x05, 0x49, + 0x4e, 0x54, 0x33, 0x32, 0x12, 0x16, 0x0a, 0x06, 0x53, 0x49, 0x4e, 0x54, 0x33, 0x32, 0x18, 0x04, + 0x20, 0x01, 0x28, 0x11, 0x52, 0x06, 0x53, 0x49, 0x4e, 0x54, 0x33, 0x32, 0x12, 0x16, 0x0a, 0x06, + 0x55, 0x49, 0x4e, 0x54, 0x33, 0x32, 0x18, 0x05, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x06, 0x55, 0x49, + 0x4e, 0x54, 0x33, 0x32, 0x12, 0x14, 0x0a, 0x05, 0x49, 0x4e, 0x54, 0x36, 0x34, 0x18, 0x06, 0x20, + 0x01, 0x28, 0x03, 0x52, 0x05, 0x49, 0x4e, 0x54, 0x36, 0x34, 0x12, 0x16, 0x0a, 0x06, 0x53, 0x49, + 0x4e, 0x47, 0x36, 0x34, 0x18, 0x07, 0x20, 0x01, 0x28, 0x12, 0x52, 0x06, 0x53, 0x49, 0x4e, 0x47, + 0x36, 0x34, 0x12, 0x16, 0x0a, 0x06, 0x55, 0x49, 0x4e, 0x54, 0x36, 0x34, 0x18, 0x08, 0x20, 0x01, + 0x28, 0x04, 0x52, 0x06, 0x55, 0x49, 0x4e, 0x54, 0x36, 0x34, 0x12, 0x1a, 0x0a, 0x08, 0x53, 0x46, + 0x49, 0x58, 0x45, 0x44, 0x33, 0x32, 0x18, 0x09, 0x20, 0x01, 0x28, 0x0f, 0x52, 0x08, 0x53, 0x46, + 0x49, 0x58, 0x45, 0x44, 0x33, 0x32, 0x12, 0x18, 0x0a, 0x07, 0x46, 0x49, 0x58, 0x45, 0x44, 0x33, + 0x32, 0x18, 0x0a, 0x20, 0x01, 0x28, 0x07, 0x52, 0x07, 0x46, 0x49, 0x58, 0x45, 0x44, 0x33, 0x32, + 0x12, 0x14, 0x0a, 0x05, 0x46, 0x4c, 0x4f, 0x41, 0x54, 0x18, 0x0b, 0x20, 0x01, 0x28, 0x02, 0x52, + 0x05, 0x46, 0x4c, 0x4f, 0x41, 0x54, 0x12, 0x1a, 0x0a, 0x08, 0x53, 0x46, 0x49, 0x58, 0x45, 0x44, + 0x36, 0x34, 0x18, 0x0c, 0x20, 0x01, 0x28, 0x10, 0x52, 0x08, 0x53, 0x46, 0x49, 0x58, 0x45, 0x44, + 0x36, 0x34, 0x12, 0x18, 0x0a, 0x07, 0x46, 0x49, 0x58, 0x45, 0x44, 0x36, 0x34, 0x18, 0x0d, 0x20, + 0x01, 0x28, 0x06, 0x52, 0x07, 0x46, 0x49, 0x58, 0x45, 0x44, 0x36, 0x34, 0x12, 0x16, 0x0a, 0x06, + 0x44, 0x4f, 0x55, 0x42, 0x4c, 0x45, 0x18, 0x0e, 0x20, 0x01, 0x28, 0x01, 0x52, 0x06, 0x44, 0x4f, + 0x55, 0x42, 0x4c, 0x45, 0x12, 0x16, 0x0a, 0x06, 0x53, 0x54, 0x52, 0x49, 0x4e, 0x47, 0x18, 0x0f, + 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x53, 0x54, 0x52, 0x49, 0x4e, 0x47, 0x12, 0x14, 0x0a, 0x05, + 0x42, 0x59, 0x54, 0x45, 0x53, 0x18, 0x10, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x05, 0x42, 0x59, 0x54, + 0x45, 0x53, 0x12, 0x1c, 0x0a, 0x07, 0x4d, 0x45, 0x53, 0x53, 0x41, 0x47, 0x45, 0x18, 0x11, 0x20, + 0x01, 0x28, 0x0b, 0x32, 0x02, 0x2e, 0x42, 0x52, 0x07, 0x4d, 0x45, 0x53, 0x53, 0x41, 0x47, 0x45, + 0x12, 0x1d, 0x0a, 0x03, 0x4d, 0x41, 0x50, 0x18, 0x12, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x0b, 0x2e, + 0x41, 0x2e, 0x4d, 0x41, 0x50, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x52, 0x03, 0x4d, 0x41, 0x50, 0x12, + 0x16, 0x0a, 0x04, 0x4c, 0x49, 0x53, 0x54, 0x18, 0x13, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x02, 0x2e, + 0x42, 0x52, 0x04, 0x4c, 0x49, 0x53, 0x54, 0x12, 0x1d, 0x0a, 0x07, 0x4f, 0x4e, 0x45, 0x4f, 0x46, + 0x5f, 0x42, 0x18, 0x14, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x02, 0x2e, 0x42, 0x48, 0x00, 0x52, 0x06, + 0x4f, 0x4e, 0x45, 0x4f, 0x46, 0x42, 0x12, 0x23, 0x0a, 0x0c, 0x4f, 0x4e, 0x45, 0x4f, 0x46, 0x5f, + 0x53, 0x54, 0x52, 0x49, 0x4e, 0x47, 0x18, 0x15, 0x20, 0x01, 0x28, 0x09, 0x48, 0x00, 0x52, 0x0b, + 0x4f, 0x4e, 0x45, 0x4f, 0x46, 0x53, 0x54, 0x52, 0x49, 0x4e, 0x47, 0x12, 0x29, 0x0a, 0x09, 0x4c, + 0x49, 0x53, 0x54, 0x5f, 0x45, 0x4e, 0x55, 0x4d, 0x18, 0x16, 0x20, 0x03, 0x28, 0x0e, 0x32, 0x0c, + 0x2e, 0x45, 0x6e, 0x75, 0x6d, 0x65, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x08, 0x4c, 0x49, + 0x53, 0x54, 0x45, 0x4e, 0x55, 0x4d, 0x12, 0x40, 0x0a, 0x08, 0x69, 0x6d, 0x70, 0x6f, 0x72, 0x74, + 0x65, 0x64, 0x18, 0x17, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x24, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, + 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x46, 0x69, 0x6c, 0x65, 0x44, + 0x65, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x6f, 0x72, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x52, 0x08, + 0x69, 0x6d, 0x70, 0x6f, 0x72, 0x74, 0x65, 0x64, 0x12, 0x2f, 0x0a, 0x0b, 0x63, 0x75, 0x73, 0x74, + 0x6f, 0x6d, 0x5f, 0x74, 0x79, 0x70, 0x65, 0x18, 0x18, 0x20, 0x01, 0x28, 0x09, 0x42, 0x0e, 0xda, + 0xb4, 0x2d, 0x0a, 0x43, 0x75, 0x73, 0x74, 0x6f, 0x6d, 0x54, 0x79, 0x70, 0x65, 0x52, 0x0a, 0x63, + 0x75, 0x73, 0x74, 0x6f, 0x6d, 0x54, 0x79, 0x70, 0x65, 0x1a, 0x3a, 0x0a, 0x08, 0x4d, 0x41, 0x50, + 0x45, 0x6e, 0x74, 0x72, 0x79, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, + 0x28, 0x09, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x18, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, + 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x02, 0x2e, 0x42, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, + 0x65, 0x3a, 0x02, 0x38, 0x01, 0x42, 0x07, 0x0a, 0x05, 0x4f, 0x4e, 0x45, 0x4f, 0x46, 0x22, 0x11, + 0x0a, 0x01, 0x42, 0x12, 0x0c, 0x0a, 0x01, 0x78, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x01, + 0x78, 0x2a, 0x1f, 0x0a, 0x0b, 0x45, 0x6e, 0x75, 0x6d, 0x65, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, + 0x12, 0x07, 0x0a, 0x03, 0x4f, 0x6e, 0x65, 0x10, 0x00, 0x12, 0x07, 0x0a, 0x03, 0x54, 0x77, 0x6f, + 0x10, 0x01, 0x42, 0x27, 0x5a, 0x25, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, + 0x2f, 0x63, 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x2f, 0x63, 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x2d, 0x70, + 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x74, 0x65, 0x73, 0x74, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, + 0x74, 0x6f, 0x33, } var ( diff --git a/testpb/custom_type.go b/testpb/custom_type.go new file mode 100644 index 0000000..634ac07 --- /dev/null +++ b/testpb/custom_type.go @@ -0,0 +1,4 @@ +package testpb + +type Int struct { +} From 7f0ad2e78fa7b646b25e6e6d06a6cfe11460379e Mon Sep 17 00:00:00 2001 From: fdymylja Date: Thu, 2 Dec 2021 18:55:35 +0100 Subject: [PATCH 2/4] change: custom type interface --- runtime/runtime.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime/runtime.go b/runtime/runtime.go index 5338cc4..b27ef8e 100644 --- a/runtime/runtime.go +++ b/runtime/runtime.go @@ -12,7 +12,7 @@ import ( type CustomType interface { UnmarshalBytes(in protoiface.UnmarshalInput, b []byte) (out protoiface.UnmarshalOutput, err error) MarshalBytes(in protoiface.MarshalOutput) (out protoiface.MarshalOutput, err error) - Size(in protoiface.SizeInput) (out protoiface.SizeOutput, err error) + Size(in protoiface.SizeInput) (out protoiface.SizeOutput) Set(value protoreflect.Value) Get(value protoreflect.Value) Clear() From d4ffa4cb8f1d761e66a9dd4a1949630bee476e05 Mon Sep 17 00:00:00 2001 From: fdymylja Date: Thu, 2 Dec 2021 19:03:39 +0100 Subject: [PATCH 3/4] add: range for custom types --- features/fastreflection/range.go | 13 +++++++++++ runtime/runtime.go | 2 +- testpb/1.proto | 2 +- testpb/1.pulsar.go | 40 ++++++++++++++++---------------- testpb/custom_type.go | 40 ++++++++++++++++++++++++++++++++ 5 files changed, 75 insertions(+), 22 deletions(-) diff --git a/features/fastreflection/range.go b/features/fastreflection/range.go index 30d073e..97cec56 100644 --- a/features/fastreflection/range.go +++ b/features/fastreflection/range.go @@ -1,8 +1,10 @@ package fastreflection import ( + "github.com/cosmos/cosmos-proto/cosmos_proto" "github.com/cosmos/cosmos-proto/generator" "google.golang.org/protobuf/compiler/protogen" + "google.golang.org/protobuf/proto" "google.golang.org/protobuf/reflect/protoreflect" ) @@ -40,6 +42,13 @@ func (g *rangeGen) genField(field *protogen.Field) { } switch { + case isCustomType(field): + g.P("if x.", field.GoName, ".IsSet() {") + g.P("value := x.", field.GoName, ".Get()") + g.P("if !f(", fieldDescriptorName(field), ", value) {") + g.P("return") + g.P("}") + g.P("}") case field.Desc.IsMap(): g.P("if len(x.", field.GoName, ") != 0 {") g.P("value := ", protoreflectPkg.Ident("ValueOfMap"), "(&", mapTypeName(field), "{m: &x.", field.GoName, "})") @@ -97,6 +106,10 @@ func (g *rangeGen) genField(field *protogen.Field) { } } +func isCustomType(field *protogen.Field) bool { + return proto.GetExtension(field.Desc.Options(), cosmos_proto.E_GoType).(string) != "" +} + func (g *rangeGen) genOneof(field *protogen.Field) { // we check if it was processed or not if _, ok := g.processedOneofs[field.Oneof.GoIdent.String()]; ok { diff --git a/runtime/runtime.go b/runtime/runtime.go index b27ef8e..14c7eb5 100644 --- a/runtime/runtime.go +++ b/runtime/runtime.go @@ -14,7 +14,7 @@ type CustomType interface { MarshalBytes(in protoiface.MarshalOutput) (out protoiface.MarshalOutput, err error) Size(in protoiface.SizeInput) (out protoiface.SizeOutput) Set(value protoreflect.Value) - Get(value protoreflect.Value) + Get() protoreflect.Value Clear() IsSet() bool Mutable() protoreflect.Value diff --git a/testpb/1.proto b/testpb/1.proto index b7185d7..e43efc5 100644 --- a/testpb/1.proto +++ b/testpb/1.proto @@ -37,7 +37,7 @@ message A { repeated Enumeration LIST_ENUM = 22; google.protobuf.FileDescriptorProto imported = 23; - string custom_type = 24 [(cosmos_proto.go_type) = "CustomType"]; + string custom_type = 24 [(cosmos_proto.go_type) = "Int"]; } message B { diff --git a/testpb/1.pulsar.go b/testpb/1.pulsar.go index 178b081..a27937f 100644 --- a/testpb/1.pulsar.go +++ b/testpb/1.pulsar.go @@ -3,6 +3,7 @@ package testpb import ( binary "encoding/binary" fmt "fmt" + _ "github.com/cosmos/cosmos-proto" runtime "github.com/cosmos/cosmos-proto/runtime" protoreflect "google.golang.org/protobuf/reflect/protoreflect" protoiface "google.golang.org/protobuf/runtime/protoiface" @@ -466,8 +467,8 @@ func (x *fastReflection_A) Range(f func(protoreflect.FieldDescriptor, protorefle return } } - if x.CustomType != "" { - value := protoreflect.ValueOfString(x.CustomType) + if x.CustomType.IsSet() { + value := x.CustomType.Get() if !f(fd_A_custom_type, value) { return } @@ -2721,7 +2722,7 @@ type A struct { ONEOF isA_ONEOF `protobuf_oneof:"ONEOF"` LIST_ENUM []Enumeration `protobuf:"varint,22,rep,packed,name=LIST_ENUM,json=LISTENUM,proto3,enum=Enumeration" json:"LIST_ENUM,omitempty"` Imported *descriptorpb.FileDescriptorProto `protobuf:"bytes,23,opt,name=imported,proto3" json:"imported,omitempty"` - CustomType *CustomType `protobuf:"bytes,24,opt,name=custom_type,json=customType,proto3" json:"custom_type,omitempty"` + CustomType *Int `protobuf:"bytes,24,opt,name=custom_type,json=customType,proto3" json:"custom_type,omitempty"` } func (x *A) Reset() { @@ -2912,7 +2913,7 @@ func (x *A) GetImported() *descriptorpb.FileDescriptorProto { return nil } -func (x *A) GetCustomType() *CustomType { +func (x *A) GetCustomType() *Int { if x != nil { return x.CustomType } @@ -2978,7 +2979,7 @@ var file_testpb_1_proto_rawDesc = []byte{ 0x66, 0x2f, 0x64, 0x65, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x6f, 0x72, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x1a, 0x1f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x63, 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x5f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x63, 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x2e, 0x70, 0x72, - 0x6f, 0x74, 0x6f, 0x22, 0x98, 0x06, 0x0a, 0x01, 0x41, 0x12, 0x20, 0x0a, 0x04, 0x65, 0x6e, 0x75, + 0x6f, 0x74, 0x6f, 0x22, 0x91, 0x06, 0x0a, 0x01, 0x41, 0x12, 0x20, 0x0a, 0x04, 0x65, 0x6e, 0x75, 0x6d, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x0c, 0x2e, 0x45, 0x6e, 0x75, 0x6d, 0x65, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x04, 0x65, 0x6e, 0x75, 0x6d, 0x12, 0x21, 0x0a, 0x0c, 0x73, 0x6f, 0x6d, 0x65, 0x5f, 0x62, 0x6f, 0x6f, 0x6c, 0x65, 0x61, 0x6e, 0x18, 0x02, 0x20, 0x01, 0x28, @@ -3020,21 +3021,20 @@ var file_testpb_1_proto_rawDesc = []byte{ 0x65, 0x64, 0x18, 0x17, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x24, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x46, 0x69, 0x6c, 0x65, 0x44, 0x65, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x6f, 0x72, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x52, 0x08, - 0x69, 0x6d, 0x70, 0x6f, 0x72, 0x74, 0x65, 0x64, 0x12, 0x2f, 0x0a, 0x0b, 0x63, 0x75, 0x73, 0x74, - 0x6f, 0x6d, 0x5f, 0x74, 0x79, 0x70, 0x65, 0x18, 0x18, 0x20, 0x01, 0x28, 0x09, 0x42, 0x0e, 0xda, - 0xb4, 0x2d, 0x0a, 0x43, 0x75, 0x73, 0x74, 0x6f, 0x6d, 0x54, 0x79, 0x70, 0x65, 0x52, 0x0a, 0x63, - 0x75, 0x73, 0x74, 0x6f, 0x6d, 0x54, 0x79, 0x70, 0x65, 0x1a, 0x3a, 0x0a, 0x08, 0x4d, 0x41, 0x50, - 0x45, 0x6e, 0x74, 0x72, 0x79, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, - 0x28, 0x09, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12, 0x18, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, - 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x02, 0x2e, 0x42, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, - 0x65, 0x3a, 0x02, 0x38, 0x01, 0x42, 0x07, 0x0a, 0x05, 0x4f, 0x4e, 0x45, 0x4f, 0x46, 0x22, 0x11, - 0x0a, 0x01, 0x42, 0x12, 0x0c, 0x0a, 0x01, 0x78, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x01, - 0x78, 0x2a, 0x1f, 0x0a, 0x0b, 0x45, 0x6e, 0x75, 0x6d, 0x65, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, - 0x12, 0x07, 0x0a, 0x03, 0x4f, 0x6e, 0x65, 0x10, 0x00, 0x12, 0x07, 0x0a, 0x03, 0x54, 0x77, 0x6f, - 0x10, 0x01, 0x42, 0x27, 0x5a, 0x25, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, - 0x2f, 0x63, 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x2f, 0x63, 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x2d, 0x70, - 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x74, 0x65, 0x73, 0x74, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, - 0x74, 0x6f, 0x33, + 0x69, 0x6d, 0x70, 0x6f, 0x72, 0x74, 0x65, 0x64, 0x12, 0x28, 0x0a, 0x0b, 0x63, 0x75, 0x73, 0x74, + 0x6f, 0x6d, 0x5f, 0x74, 0x79, 0x70, 0x65, 0x18, 0x18, 0x20, 0x01, 0x28, 0x09, 0x42, 0x07, 0xda, + 0xb4, 0x2d, 0x03, 0x49, 0x6e, 0x74, 0x52, 0x0a, 0x63, 0x75, 0x73, 0x74, 0x6f, 0x6d, 0x54, 0x79, + 0x70, 0x65, 0x1a, 0x3a, 0x0a, 0x08, 0x4d, 0x41, 0x50, 0x45, 0x6e, 0x74, 0x72, 0x79, 0x12, 0x10, + 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x6b, 0x65, 0x79, + 0x12, 0x18, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, + 0x02, 0x2e, 0x42, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x3a, 0x02, 0x38, 0x01, 0x42, 0x07, + 0x0a, 0x05, 0x4f, 0x4e, 0x45, 0x4f, 0x46, 0x22, 0x11, 0x0a, 0x01, 0x42, 0x12, 0x0c, 0x0a, 0x01, + 0x78, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x01, 0x78, 0x2a, 0x1f, 0x0a, 0x0b, 0x45, 0x6e, + 0x75, 0x6d, 0x65, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x07, 0x0a, 0x03, 0x4f, 0x6e, 0x65, + 0x10, 0x00, 0x12, 0x07, 0x0a, 0x03, 0x54, 0x77, 0x6f, 0x10, 0x01, 0x42, 0x27, 0x5a, 0x25, 0x67, + 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x63, 0x6f, 0x73, 0x6d, 0x6f, 0x73, + 0x2f, 0x63, 0x6f, 0x73, 0x6d, 0x6f, 0x73, 0x2d, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x74, 0x65, + 0x73, 0x74, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( diff --git a/testpb/custom_type.go b/testpb/custom_type.go index 634ac07..7d06e29 100644 --- a/testpb/custom_type.go +++ b/testpb/custom_type.go @@ -1,4 +1,44 @@ package testpb +import ( + "github.com/cosmos/cosmos-proto/runtime" + "google.golang.org/protobuf/reflect/protoreflect" + "google.golang.org/protobuf/runtime/protoiface" +) + +var _ runtime.CustomType = (*Int)(nil) + type Int struct { } + +func (i Int) UnmarshalBytes(in protoiface.UnmarshalInput, b []byte) (out protoiface.UnmarshalOutput, err error) { + panic("implement me") +} + +func (i Int) MarshalBytes(in protoiface.MarshalOutput) (out protoiface.MarshalOutput, err error) { + panic("implement me") +} + +func (i Int) Size(in protoiface.SizeInput) (out protoiface.SizeOutput) { + panic("implement me") +} + +func (i Int) Set(value protoreflect.Value) { + panic("implement me") +} + +func (i Int) Get() protoreflect.Value { + panic("implement me") +} + +func (i Int) Clear() { + panic("implement me") +} + +func (i Int) IsSet() bool { + panic("implement me") +} + +func (i Int) Mutable() protoreflect.Value { + panic("implement me") +} From 23fcf9c8c894e7e6b0467ca70b6aed18225e7a8f Mon Sep 17 00:00:00 2001 From: fdymylja Date: Thu, 2 Dec 2021 19:18:56 +0100 Subject: [PATCH 4/4] add: finalize protoreflect --- features/fastreflection/clear.go | 7 ++++++- features/fastreflection/get.go | 5 +++++ features/fastreflection/has.go | 5 +++++ features/fastreflection/set.go | 10 ++++++---- features/protoc/main.go | 3 +++ testpb/1.pulsar.go | 12 +++++------- 6 files changed, 30 insertions(+), 12 deletions(-) diff --git a/features/fastreflection/clear.go b/features/fastreflection/clear.go index a47cea5..f81fb23 100644 --- a/features/fastreflection/clear.go +++ b/features/fastreflection/clear.go @@ -51,7 +51,12 @@ func (g *clearGen) genField(field *protogen.Field) { return } - g.P("x.", field.GoName, " = ", zeroValueForField(nil, field)) + switch { + case isCustomType(field): + g.P("x.", field.GoName, ".Clear()") + default: + g.P("x.", field.GoName, " = ", zeroValueForField(nil, field)) + } } func (g *clearGen) genNullable(field *protogen.Field) { diff --git a/features/fastreflection/get.go b/features/fastreflection/get.go index ce34047..6c5a435 100644 --- a/features/fastreflection/get.go +++ b/features/fastreflection/get.go @@ -53,6 +53,11 @@ func (g *getGen) genFieldGetter(field *protogen.Field) { return } + if isCustomType(field) { + g.P("return x.", field.GoName, ".Get()") + return + } + fieldRef := "x." + field.GoName g.P("value := ", fieldRef) switch field.Desc.Kind() { diff --git a/features/fastreflection/has.go b/features/fastreflection/has.go index 64c9d96..bd5d337 100644 --- a/features/fastreflection/has.go +++ b/features/fastreflection/has.go @@ -60,6 +60,11 @@ func (g *hasGen) genField(field *protogen.Field) { return } + if isCustomType(field) { + g.P("return x.", field.GoName, ".IsSet()") + return + } + switch field.Desc.Kind() { case protoreflect.FloatKind: g.P("return x.", field.GoName, " != ", zeroValueForField(nil, field), " || ", mathPkg.Ident("Signbit"), "(float64(x.", field.GoName, "))") diff --git a/features/fastreflection/set.go b/features/fastreflection/set.go index d271221..071c96e 100644 --- a/features/fastreflection/set.go +++ b/features/fastreflection/set.go @@ -41,18 +41,20 @@ func (g *setGen) genComment() { } func (g *setGen) genField(field *protogen.Field) { - if field.Oneof != nil { - g.genOneof(field) - return - } switch { + case field.Oneof != nil: + g.genOneof(field) + return case field.Desc.IsMap(): g.genMap(field) return case field.Desc.IsList(): g.genList(field) return + case isCustomType(field): + g.P("x.", field.GoName, ".Set(value)") + return } fieldRef := "x." + field.GoName diff --git a/features/protoc/main.go b/features/protoc/main.go index 7c7feaf..260cf8a 100644 --- a/features/protoc/main.go +++ b/features/protoc/main.go @@ -1749,6 +1749,9 @@ func fieldDefaultValue(g *generator.GeneratedFile, f *fileInfo, m *messageInfo, } return defVarName } + if isCustomType(field) { + return "nil" + } switch field.Desc.Kind() { case protoreflect.BoolKind: return "false" diff --git a/testpb/1.pulsar.go b/testpb/1.pulsar.go index a27937f..11d2e51 100644 --- a/testpb/1.pulsar.go +++ b/testpb/1.pulsar.go @@ -3,7 +3,6 @@ package testpb import ( binary "encoding/binary" fmt "fmt" - _ "github.com/cosmos/cosmos-proto" runtime "github.com/cosmos/cosmos-proto/runtime" protoreflect "google.golang.org/protobuf/reflect/protoreflect" protoiface "google.golang.org/protobuf/runtime/protoiface" @@ -547,7 +546,7 @@ func (x *fastReflection_A) Has(fd protoreflect.FieldDescriptor) bool { case "A.imported": return x.Imported != nil case "A.custom_type": - return x.CustomType != "" + return x.CustomType.IsSet() default: if fd.IsExtension() { panic(fmt.Errorf("proto3 declared messages do not support extensions: A")) @@ -611,7 +610,7 @@ func (x *fastReflection_A) Clear(fd protoreflect.FieldDescriptor) { case "A.imported": x.Imported = nil case "A.custom_type": - x.CustomType = "" + x.CustomType.Clear() default: if fd.IsExtension() { panic(fmt.Errorf("proto3 declared messages do not support extensions: A")) @@ -717,8 +716,7 @@ func (x *fastReflection_A) Get(descriptor protoreflect.FieldDescriptor) protoref value := x.Imported return protoreflect.ValueOfMessage(value.ProtoReflect()) case "A.custom_type": - value := x.CustomType - return protoreflect.ValueOfString(value) + return x.CustomType.Get() default: if descriptor.IsExtension() { panic(fmt.Errorf("proto3 declared messages do not support extensions: A")) @@ -794,7 +792,7 @@ func (x *fastReflection_A) Set(fd protoreflect.FieldDescriptor, value protorefle case "A.imported": x.Imported = value.Message().Interface().(*descriptorpb.FileDescriptorProto) case "A.custom_type": - x.CustomType = value.Interface().(string) + x.CustomType.Set(value) default: if fd.IsExtension() { panic(fmt.Errorf("proto3 declared messages do not support extensions: A")) @@ -2917,7 +2915,7 @@ func (x *A) GetCustomType() *Int { if x != nil { return x.CustomType } - return "" + return nil } type isA_ONEOF interface {