internal/impl: remove hack in legacy_extension_hack.go
This hack has now been upstreamed in the v1 codebase;
there is no longer any need to maintain the code in v2.
See https://github.com/golang/protobuf/pull/746
Change-Id: I2c59d11303f5465e65190d893c422c088c647691
Reviewed-on: https://go-review.googlesource.com/c/149659
Reviewed-by: Herbie Ong <herbie@google.com>
diff --git a/internal/impl/legacy_extension.go b/internal/impl/legacy_extension.go
index ba3a614..d54da34 100644
--- a/internal/impl/legacy_extension.go
+++ b/internal/impl/legacy_extension.go
@@ -47,7 +47,7 @@
}
t := legacyExtensionTypeOf(x.desc)
if t.Cardinality() == pref.Repeated {
- return legacyExtensionValueOf(x.val, t).List().Len() > 0
+ return t.ValueOf(x.val).List().Len() > 0
}
return true
}
@@ -63,7 +63,7 @@
// TODO: What is the zero value for Lists?
// TODO: This logic is racy.
v := t.ValueOf(t.New())
- x.val = legacyExtensionInterfaceOf(v, t)
+ x.val = t.InterfaceOf(v)
p.x.Set(n, x)
return v
}
@@ -73,7 +73,7 @@
}
return t.Default()
}
- return legacyExtensionValueOf(x.val, t)
+ return t.ValueOf(x.val)
}
func (p legacyExtensionFields) Set(n pref.FieldNumber, v pref.Value) {
@@ -82,7 +82,7 @@
panic("no extension descriptor registered")
}
t := legacyExtensionTypeOf(x.desc)
- x.val = legacyExtensionInterfaceOf(v, t)
+ x.val = t.InterfaceOf(v)
p.x.Set(n, x)
}
@@ -103,10 +103,10 @@
t := legacyExtensionTypeOf(x.desc)
if x.val == nil {
v := t.ValueOf(t.New())
- x.val = legacyExtensionInterfaceOf(v, t)
+ x.val = t.InterfaceOf(v)
p.x.Set(n, x)
}
- return legacyExtensionValueOf(x.val, t).Interface().(pref.Mutable)
+ return t.ValueOf(x.val).Interface().(pref.Mutable)
}
func (p legacyExtensionFields) Range(f func(pref.FieldNumber, pref.Value) bool) {
diff --git a/internal/impl/legacy_extension_hack.go b/internal/impl/legacy_extension_hack.go
index 295798f..1ee56cc 100644
--- a/internal/impl/legacy_extension_hack.go
+++ b/internal/impl/legacy_extension_hack.go
@@ -141,76 +141,3 @@
val interface{}
raw []byte
}
-
-// TODO: The legacyExtensionInterfaceOf and legacyExtensionValueOf converters
-// exist since the current storage representation in the v1 data structures use
-// *T for scalars and []T for repeated fields, but the v2 API operates on
-// T for scalars and *[]T for repeated fields.
-//
-// Instead of maintaining this technical debt in the v2 repository,
-// we can offload this into the v1 implementation such that it uses a
-// storage representation that is appropriate for v2, and uses the these
-// functions to present the illusion that that the underlying storage
-// is still *T and []T.
-//
-// See https://github.com/golang/protobuf/pull/746
-const hasPR746 = true
-
-// legacyExtensionInterfaceOf converts a protoreflect.Value to the
-// storage representation used in v1 extension data structures.
-//
-// In particular, it represents scalars (except []byte) a pointer to the value,
-// and repeated fields as the a slice value itself.
-func legacyExtensionInterfaceOf(pv pref.Value, t pref.ExtensionType) interface{} {
- v := t.InterfaceOf(pv)
- if !hasPR746 {
- switch rv := reflect.ValueOf(v); rv.Kind() {
- case reflect.Bool, reflect.Int32, reflect.Int64, reflect.Uint32, reflect.Uint64, reflect.Float32, reflect.Float64, reflect.String:
- // Represent primitive types as a pointer to the value.
- rv2 := reflect.New(rv.Type())
- rv2.Elem().Set(rv)
- v = rv2.Interface()
- case reflect.Ptr:
- // Represent pointer to slice types as the value itself.
- switch rv.Type().Elem().Kind() {
- case reflect.Slice:
- if rv.IsNil() {
- v = reflect.Zero(rv.Type().Elem()).Interface()
- } else {
- v = rv.Elem().Interface()
- }
- }
- }
- }
- return v
-}
-
-// legacyExtensionValueOf converts the storage representation of a value in
-// the v1 extension data structures to a protoreflect.Value.
-//
-// In particular, it represents scalars as the value itself,
-// and repeated fields as a pointer to the slice value.
-func legacyExtensionValueOf(v interface{}, t pref.ExtensionType) pref.Value {
- if !hasPR746 {
- switch rv := reflect.ValueOf(v); rv.Kind() {
- case reflect.Ptr:
- // Represent slice types as the value itself.
- switch rv.Type().Elem().Kind() {
- case reflect.Bool, reflect.Int32, reflect.Int64, reflect.Uint32, reflect.Uint64, reflect.Float32, reflect.Float64, reflect.String:
- if rv.IsNil() {
- v = reflect.Zero(rv.Type().Elem()).Interface()
- } else {
- v = rv.Elem().Interface()
- }
- }
- case reflect.Slice:
- // Represent slice types (except []byte) as a pointer to the value.
- if rv.Type().Elem().Kind() != reflect.Uint8 {
- rv2 := reflect.New(rv.Type())
- rv2.Elem().Set(rv)
- v = rv2.Interface()
- }
- }
- }
- return t.ValueOf(v)
-}