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/decode.go b/internal/impl/decode.go
index 3bc25e0..f71d19d 100644
--- a/internal/impl/decode.go
+++ b/internal/impl/decode.go
@@ -145,24 +145,23 @@
}
return 0, err
}
- x.SetType(xt)
}
xi := mi.extensionFieldInfo(xt)
if xi.funcs.unmarshal == nil {
return 0, errUnknown
}
- ival := x.GetValue()
- if ival == nil && xi.unmarshalNeedsValue {
+ ival := x.Value()
+ if !ival.IsValid() && xi.unmarshalNeedsValue {
// Create a new message, list, or map value to fill in.
// For enums, create a prototype value to let the unmarshal func know the
// concrete type.
- ival = xt.InterfaceOf(xt.New())
+ ival = xt.New()
}
v, n, err := xi.funcs.unmarshal(b, ival, num, wtyp, opts)
if err != nil {
return 0, err
}
- x.SetEagerValue(v)
+ x.Set(xt, v)
exts[int32(num)] = x
return n, nil
}