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/internal/impl/message.go b/internal/impl/message.go
index 8879015..5c2ae28 100644
--- a/internal/impl/message.go
+++ b/internal/impl/message.go
@@ -113,8 +113,8 @@
// any discrepancies.
func (mi *MessageType) makeKnownFieldsFunc(si structInfo) {
mi.fields = map[pref.FieldNumber]*fieldInfo{}
- for i := 0; i < mi.PBType.Fields().Len(); i++ {
- fd := mi.PBType.Fields().Get(i)
+ for i := 0; i < mi.PBType.Descriptor().Fields().Len(); i++ {
+ fd := mi.PBType.Descriptor().Fields().Get(i)
fs := si.fieldsByNumber[fd.Number()]
var fi fieldInfo
switch {
@@ -133,8 +133,8 @@
}
mi.oneofs = map[pref.Name]*oneofInfo{}
- for i := 0; i < mi.PBType.Oneofs().Len(); i++ {
- od := mi.PBType.Oneofs().Get(i)
+ for i := 0; i < mi.PBType.Descriptor().Oneofs().Len(); i++ {
+ od := mi.PBType.Descriptor().Oneofs().Get(i)
mi.oneofs[od.Name()] = makeOneofInfo(od, si.oneofsByName[od.Name()], si.oneofWrappersByType)
}
}
@@ -203,9 +203,13 @@
type messageReflectWrapper messageDataType
+// TODO: Remove this.
func (m *messageReflectWrapper) Type() pref.MessageType {
return m.mi.PBType
}
+func (m *messageReflectWrapper) Descriptor() pref.MessageDescriptor {
+ return m.mi.PBType.Descriptor()
+}
func (m *messageReflectWrapper) KnownFields() pref.KnownFields {
m.mi.init()
return (*knownFields)(m)
@@ -214,6 +218,9 @@
m.mi.init()
return m.mi.unknownFields((*messageDataType)(m))
}
+func (m *messageReflectWrapper) New() pref.Message {
+ return m.mi.PBType.New()
+}
func (m *messageReflectWrapper) Interface() pref.ProtoMessage {
if m, ok := m.ProtoUnwrap().(pref.ProtoMessage); ok {
return m
@@ -266,7 +273,7 @@
fi.set(fs.p, v)
return
}
- if fs.mi.PBType.ExtensionRanges().Has(n) {
+ if fs.mi.PBType.Descriptor().ExtensionRanges().Has(n) {
fs.extensionFields().Set(n, v)
return
}
@@ -277,7 +284,7 @@
fi.clear(fs.p)
return
}
- if fs.mi.PBType.ExtensionRanges().Has(n) {
+ if fs.mi.PBType.Descriptor().ExtensionRanges().Has(n) {
fs.extensionFields().Clear(n)
return
}
@@ -302,7 +309,7 @@
if fi := fs.mi.fields[n]; fi != nil {
return fi.newMessage()
}
- if fs.mi.PBType.ExtensionRanges().Has(n) {
+ if fs.mi.PBType.Descriptor().ExtensionRanges().Has(n) {
return fs.extensionFields().NewMessage(n)
}
panic(fmt.Sprintf("invalid field: %d", n))