reflect/protoreflect: add helper methods to FieldDescriptor
Added API:
FieldDescriptor.IsExtension
FieldDescriptor.IsList
FieldDescriptor.MapKey
FieldDescriptor.MapValue
FieldDescriptor.ContainingOneof
FieldDescriptor.ContainingMessage
Deprecated API (to be removed in subsequent CL):
FieldDescriptor.Oneof
FieldDescriptor.Extendee
These methods help cleanup several common usage patterns.
Change-Id: I9a3ffabc2edb2173c536509b22f330f98bba7cf3
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/176977
Reviewed-by: Damien Neil <dneil@google.com>
diff --git a/internal/impl/legacy_extension.go b/internal/impl/legacy_extension.go
index a0ae3f0..7a8fea7 100644
--- a/internal/impl/legacy_extension.go
+++ b/internal/impl/legacy_extension.go
@@ -64,7 +64,7 @@
}
t := extensionTypeFromDesc(x.Desc)
d := t.Descriptor()
- if d.Cardinality() == pref.Repeated {
+ if d.IsList() {
return t.ValueOf(x.Value).List().Len() > 0
}
return true
@@ -105,7 +105,7 @@
}
t := extensionTypeFromDesc(x.Desc)
d := t.Descriptor()
- if d.Cardinality() == pref.Repeated {
+ if d.IsList() {
t.ValueOf(x.Value).List().Truncate(0)
return
}
@@ -153,7 +153,7 @@
func (p legacyExtensionTypes) Register(t pref.ExtensionType) {
d := t.Descriptor()
- if p.mi.PBType.Descriptor().FullName() != d.Extendee().FullName() {
+ if p.mi.PBType.Descriptor().FullName() != d.ContainingMessage().FullName() {
panic("extended type mismatch")
}
if !p.mi.PBType.Descriptor().ExtensionRanges().Has(d.Number()) {
@@ -164,7 +164,7 @@
panic("extension descriptor already registered")
}
x.Desc = extensionDescFromType(t)
- if d.Cardinality() == pref.Repeated {
+ if d.IsList() {
// If the field is repeated, initialize the entry with an empty list
// so that future Get operations can return a mutable and concrete list.
x.Value = t.InterfaceOf(t.New())
@@ -178,7 +178,7 @@
return
}
x := p.x.Get(d.Number())
- if d.Cardinality() == pref.Repeated {
+ if d.IsList() {
// Treat an empty repeated field as unpopulated.
v := reflect.ValueOf(x.Value)
if x.Value == nil || v.IsNil() || v.Elem().Len() == 0 {