reflect/protoreflect: add Descriptor specific methods
Added methods:
Enum.Descriptor
Message.Descriptor
EnumType.Descriptor
MessageType.Descriptor
ExtensionType.Descriptor
Message.New
All functionality is switched over to use those methods instead of
implicitly relying on the fact that {Enum,Message}Type implicitly
implement the associated descriptor interface.
This CL does not yet remove {Enum,Message}.Type or prevent
{Enum,Message,Extension}Type from implementating a descriptor.
That is a subsequent CL.
The Message.New method is also added to replace functionality
that will be lost when the Type methods are removed.
Change-Id: I7fefde1673bbd40bfdac489aca05cec9a6c98eb1
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/174918
Reviewed-by: Damien Neil <dneil@google.com>
Reviewed-by: Herbie Ong <herbie@google.com>
diff --git a/encoding/testprotos/pb2/test.pb.go b/encoding/testprotos/pb2/test.pb.go
index f5b9f40..abc47c8 100644
--- a/encoding/testprotos/pb2/test.pb.go
+++ b/encoding/testprotos/pb2/test.pb.go
@@ -43,9 +43,14 @@
}
func (x Enum) String() string {
- return protoimpl.X.EnumStringOf(x.Type(), protoreflect.EnumNumber(x))
+ return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x))
}
+func (Enum) Descriptor() protoreflect.EnumDescriptor {
+ return file_pb2_test_proto_enumTypes[0].Descriptor()
+}
+
+// Deprecated: Use Descriptor instead.
func (Enum) Type() protoreflect.EnumType {
return file_pb2_test_proto_enumTypes[0]
}
@@ -56,7 +61,7 @@
// Deprecated: Do not use.
func (x *Enum) UnmarshalJSON(b []byte) error {
- num, err := protoimpl.X.UnmarshalJSONEnum(x.Type(), b)
+ num, err := protoimpl.X.UnmarshalJSONEnum(x.Descriptor(), b)
if err != nil {
return err
}
@@ -98,9 +103,14 @@
}
func (x Enums_NestedEnum) String() string {
- return protoimpl.X.EnumStringOf(x.Type(), protoreflect.EnumNumber(x))
+ return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x))
}
+func (Enums_NestedEnum) Descriptor() protoreflect.EnumDescriptor {
+ return file_pb2_test_proto_enumTypes[1].Descriptor()
+}
+
+// Deprecated: Use Descriptor instead.
func (Enums_NestedEnum) Type() protoreflect.EnumType {
return file_pb2_test_proto_enumTypes[1]
}
@@ -111,7 +121,7 @@
// Deprecated: Do not use.
func (x *Enums_NestedEnum) UnmarshalJSON(b []byte) error {
- num, err := protoimpl.X.UnmarshalJSONEnum(x.Type(), b)
+ num, err := protoimpl.X.UnmarshalJSONEnum(x.Descriptor(), b)
if err != nil {
return err
}
diff --git a/encoding/testprotos/pb3/test.pb.go b/encoding/testprotos/pb3/test.pb.go
index 0e0b2b3..c5892af 100644
--- a/encoding/testprotos/pb3/test.pb.go
+++ b/encoding/testprotos/pb3/test.pb.go
@@ -39,9 +39,14 @@
}
func (x Enum) String() string {
- return protoimpl.X.EnumStringOf(x.Type(), protoreflect.EnumNumber(x))
+ return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x))
}
+func (Enum) Descriptor() protoreflect.EnumDescriptor {
+ return file_pb3_test_proto_enumTypes[0].Descriptor()
+}
+
+// Deprecated: Use Descriptor instead.
func (Enum) Type() protoreflect.EnumType {
return file_pb3_test_proto_enumTypes[0]
}
@@ -81,9 +86,14 @@
}
func (x Enums_NestedEnum) String() string {
- return protoimpl.X.EnumStringOf(x.Type(), protoreflect.EnumNumber(x))
+ return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x))
}
+func (Enums_NestedEnum) Descriptor() protoreflect.EnumDescriptor {
+ return file_pb3_test_proto_enumTypes[1].Descriptor()
+}
+
+// Deprecated: Use Descriptor instead.
func (Enums_NestedEnum) Type() protoreflect.EnumType {
return file_pb3_test_proto_enumTypes[1]
}