all: refactor extensions, add proto.GetExtension etc.
Change protoiface.ExtensionDescV1 to implement protoreflect.ExtensionType.
ExtensionDescV1's Name field conflicts with the Descriptor Name method,
so change the protoreflect.{Message,Enum,Extension}Type types to no
longer implement the corresponding Descriptor interface. This also leads
to a clearer distinction between the two types.
Introduce a protoreflect.ExtensionTypeDescriptor type which bridges
between ExtensionType and ExtensionDescriptor.
Add extension accessor functions to the proto package:
proto.{Has,Clear,Get,Set}Extension. These functions take a
protoreflect.ExtensionType parameter, which allows writing the
same function call using either the old or new API:
proto.GetExtension(message, somepb.E_ExtensionFoo)
Fixes golang/protobuf#908
Change-Id: Ibc65d12a46666297849114fd3aefbc4a597d9f08
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/189199
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
diff --git a/internal/impl/message_reflect.go b/internal/impl/message_reflect.go
index 699ac2c..b0f1778 100644
--- a/internal/impl/message_reflect.go
+++ b/internal/impl/message_reflect.go
@@ -121,7 +121,7 @@
if m != nil {
for _, x := range *m {
xt := x.GetType()
- if !f(xt, xt.ValueOf(x.GetValue())) {
+ if !f(xt.Descriptor(), xt.ValueOf(x.GetValue())) {
return
}
}
@@ -129,16 +129,17 @@
}
func (m *extensionMap) Has(xt pref.ExtensionType) (ok bool) {
if m != nil {
- _, ok = (*m)[int32(xt.Number())]
+ _, ok = (*m)[int32(xt.Descriptor().Number())]
}
return ok
}
func (m *extensionMap) Clear(xt pref.ExtensionType) {
- delete(*m, int32(xt.Number()))
+ delete(*m, int32(xt.Descriptor().Number()))
}
func (m *extensionMap) Get(xt pref.ExtensionType) pref.Value {
+ xd := xt.Descriptor()
if m != nil {
- if x, ok := (*m)[int32(xt.Number())]; ok {
+ if x, ok := (*m)[int32(xd.Number())]; ok {
return xt.ValueOf(x.GetValue())
}
}
@@ -151,13 +152,14 @@
var x ExtensionField
x.SetType(xt)
x.SetEagerValue(xt.InterfaceOf(v))
- (*m)[int32(xt.Number())] = x
+ (*m)[int32(xt.Descriptor().Number())] = x
}
func (m *extensionMap) Mutable(xt pref.ExtensionType) pref.Value {
- if !isComposite(xt) {
+ xd := xt.Descriptor()
+ if !isComposite(xd) {
panic("invalid Mutable on field with non-composite type")
}
- if x, ok := (*m)[int32(xt.Number())]; ok {
+ if x, ok := (*m)[int32(xd.Number())]; ok {
return xt.ValueOf(x.GetValue())
}
v := xt.New()
@@ -179,14 +181,18 @@
return fi, nil
}
if fd.IsExtension() {
- if fd.ContainingMessage().FullName() != mi.PBType.FullName() {
+ if fd.ContainingMessage().FullName() != mi.PBType.Descriptor().FullName() {
// TODO: Should this be exact containing message descriptor match?
panic("mismatching containing message")
}
- if !mi.PBType.ExtensionRanges().Has(fd.Number()) {
+ if !mi.PBType.Descriptor().ExtensionRanges().Has(fd.Number()) {
panic("invalid extension field")
}
- return nil, fd.(pref.ExtensionType)
+ xtd, ok := fd.(pref.ExtensionTypeDescriptor)
+ if !ok {
+ panic("extension descriptor does not implement ExtensionTypeDescriptor")
+ }
+ return nil, xtd.Type()
}
panic("invalid field descriptor")
}