internal/impl: store extension values as Values
Change the storage type of ExtensionField from interface{} to
protoreflect.Value.
Replace the codec functions operating on interface{}s with ones
operating on Values.
Values are potentially more efficient, since they can represent
non-pointer types without allocation. This also reduces the number of
types used to represent field values.
Additionally, this change lays groundwork for changing the
user-visible representation of repeated extension fields from
*[]T to []T. The storage type for extension fields must support mutation
(thus *[]T currently); changing the storage type to a Value permits this
without the need to introduce yet another view on field values.
Change-Id: Ida336be14112bb940f655236eb58df21bf312525
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/192218
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
diff --git a/internal/impl/message_reflect.go b/internal/impl/message_reflect.go
index 8ed7cc5..469b255 100644
--- a/internal/impl/message_reflect.go
+++ b/internal/impl/message_reflect.go
@@ -119,7 +119,7 @@
if m != nil {
for _, x := range *m {
xt := x.GetType()
- if !f(xt.TypeDescriptor(), xt.ValueOf(x.GetValue())) {
+ if !f(xt.TypeDescriptor(), x.Value()) {
return
}
}
@@ -138,18 +138,20 @@
xd := xt.TypeDescriptor()
if m != nil {
if x, ok := (*m)[int32(xd.Number())]; ok {
- return xt.ValueOf(x.GetValue())
+ return x.Value()
}
}
return xt.Zero()
}
func (m *extensionMap) Set(xt pref.ExtensionType, v pref.Value) {
+ if !xt.IsValidValue(v) {
+ panic(fmt.Errorf("%v: assigning invalid value", xt.TypeDescriptor().FullName()))
+ }
if *m == nil {
*m = make(map[int32]ExtensionField)
}
var x ExtensionField
- x.SetType(xt)
- x.SetEagerValue(xt.InterfaceOf(v))
+ x.Set(xt, v)
(*m)[int32(xt.TypeDescriptor().Number())] = x
}
func (m *extensionMap) Mutable(xt pref.ExtensionType) pref.Value {
@@ -158,7 +160,7 @@
panic("invalid Mutable on field with non-composite type")
}
if x, ok := (*m)[int32(xd.Number())]; ok {
- return xt.ValueOf(x.GetValue())
+ return x.Value()
}
v := xt.New()
m.Set(xt, v)