2018-11-26 22:32:06 -08:00
|
|
|
// Copyright 2018 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2019-05-22 13:42:54 -04:00
|
|
|
package impl
|
2018-11-26 22:32:06 -08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
2018-12-04 14:00:01 -08:00
|
|
|
"sync"
|
2018-11-26 22:32:06 -08:00
|
|
|
|
2019-05-13 23:55:40 -07:00
|
|
|
ptag "google.golang.org/protobuf/internal/encoding/tag"
|
2019-06-06 13:01:53 -07:00
|
|
|
"google.golang.org/protobuf/internal/filedesc"
|
2019-05-13 23:55:40 -07:00
|
|
|
pref "google.golang.org/protobuf/reflect/protoreflect"
|
|
|
|
preg "google.golang.org/protobuf/reflect/protoregistry"
|
|
|
|
piface "google.golang.org/protobuf/runtime/protoiface"
|
2018-11-26 22:32:06 -08:00
|
|
|
)
|
|
|
|
|
2019-05-22 13:42:54 -04:00
|
|
|
// legacyExtensionDescKey is a comparable version of protoiface.ExtensionDescV1
|
2018-12-04 14:00:01 -08:00
|
|
|
// suitable for use as a key in a map.
|
2019-05-22 13:42:54 -04:00
|
|
|
type legacyExtensionDescKey struct {
|
2018-12-04 14:00:01 -08:00
|
|
|
typeV2 pref.ExtensionType
|
|
|
|
extendedType reflect.Type
|
|
|
|
extensionType reflect.Type
|
|
|
|
field int32
|
|
|
|
name string
|
|
|
|
tag string
|
|
|
|
filename string
|
|
|
|
}
|
|
|
|
|
2019-05-22 13:42:54 -04:00
|
|
|
func legacyExtensionDescKeyOf(d *piface.ExtensionDescV1) legacyExtensionDescKey {
|
|
|
|
return legacyExtensionDescKey{
|
2018-12-04 14:00:01 -08:00
|
|
|
d.Type,
|
|
|
|
reflect.TypeOf(d.ExtendedType),
|
|
|
|
reflect.TypeOf(d.ExtensionType),
|
|
|
|
d.Field, d.Name, d.Tag, d.Filename,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2019-05-22 13:42:54 -04:00
|
|
|
legacyExtensionTypeCache sync.Map // map[legacyExtensionDescKey]protoreflect.ExtensionType
|
|
|
|
legacyExtensionDescCache sync.Map // map[protoreflect.ExtensionType]*protoiface.ExtensionDescV1
|
2018-12-04 14:00:01 -08:00
|
|
|
)
|
|
|
|
|
2019-05-22 13:42:54 -04:00
|
|
|
// legacyExtensionDescFromType converts a v2 protoreflect.ExtensionType to a
|
all: move v1 types over to the v2 repository
As a goal, v2 should not depend on v1. As another step towards that end,
we move all the types that used to be in the v1 protoapi package over to v2.
For now, we place MessageV1, ExtensionRangeV1, and ExtensionDescV1
in runtime/protoiface since these are types that generated messages will
probably have to reference forever. An alternative location could be
reflect/protoreflect, but it seems unfortunate to have to dirty the
namespace of that package with these types.
We move ExtensionFieldV1, ExtensionFieldsV1, and ExtensionFieldsOf
to internal/impl, since these are related to the implementation of a
generated message.
Since moving these types from v1 to v2 implies that the v1 protoapi
package is useless, we update all usages of v1 protoapi in the v2
repository to point to the relevant v2 type or functionality.
CL/168538 is the corresponding change to alter v1.
There will be a temporary build failure as it is not possible
to submit CL/168519 and CL/168538 atomically.
Change-Id: Ide4025c1b6af5b7f0696f4b65b988b4d10a50f0b
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/168519
Reviewed-by: Herbie Ong <herbie@google.com>
2019-03-20 18:29:32 -07:00
|
|
|
// protoiface.ExtensionDescV1. The returned ExtensionDesc must not be mutated.
|
2019-05-22 13:42:54 -04:00
|
|
|
func legacyExtensionDescFromType(xt pref.ExtensionType) *piface.ExtensionDescV1 {
|
2019-03-14 16:08:22 -07:00
|
|
|
// Fast-path: check whether an extension desc is already nested within.
|
2019-05-01 12:29:25 -07:00
|
|
|
if xt, ok := xt.(interface {
|
all: move v1 types over to the v2 repository
As a goal, v2 should not depend on v1. As another step towards that end,
we move all the types that used to be in the v1 protoapi package over to v2.
For now, we place MessageV1, ExtensionRangeV1, and ExtensionDescV1
in runtime/protoiface since these are types that generated messages will
probably have to reference forever. An alternative location could be
reflect/protoreflect, but it seems unfortunate to have to dirty the
namespace of that package with these types.
We move ExtensionFieldV1, ExtensionFieldsV1, and ExtensionFieldsOf
to internal/impl, since these are related to the implementation of a
generated message.
Since moving these types from v1 to v2 implies that the v1 protoapi
package is useless, we update all usages of v1 protoapi in the v2
repository to point to the relevant v2 type or functionality.
CL/168538 is the corresponding change to alter v1.
There will be a temporary build failure as it is not possible
to submit CL/168519 and CL/168538 atomically.
Change-Id: Ide4025c1b6af5b7f0696f4b65b988b4d10a50f0b
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/168519
Reviewed-by: Herbie Ong <herbie@google.com>
2019-03-20 18:29:32 -07:00
|
|
|
ProtoLegacyExtensionDesc() *piface.ExtensionDescV1
|
|
|
|
}); ok {
|
2019-05-01 12:29:25 -07:00
|
|
|
if d := xt.ProtoLegacyExtensionDesc(); d != nil {
|
2019-03-14 16:08:22 -07:00
|
|
|
return d
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-04 14:00:01 -08:00
|
|
|
// Fast-path: check the cache for whether this ExtensionType has already
|
|
|
|
// been converted to a legacy descriptor.
|
2019-05-22 13:42:54 -04:00
|
|
|
if d, ok := legacyExtensionDescCache.Load(xt); ok {
|
all: move v1 types over to the v2 repository
As a goal, v2 should not depend on v1. As another step towards that end,
we move all the types that used to be in the v1 protoapi package over to v2.
For now, we place MessageV1, ExtensionRangeV1, and ExtensionDescV1
in runtime/protoiface since these are types that generated messages will
probably have to reference forever. An alternative location could be
reflect/protoreflect, but it seems unfortunate to have to dirty the
namespace of that package with these types.
We move ExtensionFieldV1, ExtensionFieldsV1, and ExtensionFieldsOf
to internal/impl, since these are related to the implementation of a
generated message.
Since moving these types from v1 to v2 implies that the v1 protoapi
package is useless, we update all usages of v1 protoapi in the v2
repository to point to the relevant v2 type or functionality.
CL/168538 is the corresponding change to alter v1.
There will be a temporary build failure as it is not possible
to submit CL/168519 and CL/168538 atomically.
Change-Id: Ide4025c1b6af5b7f0696f4b65b988b4d10a50f0b
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/168519
Reviewed-by: Herbie Ong <herbie@google.com>
2019-03-20 18:29:32 -07:00
|
|
|
return d.(*piface.ExtensionDescV1)
|
2018-11-26 22:32:06 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Determine the parent type if possible.
|
2019-08-02 16:58:08 -07:00
|
|
|
xd := xt.Descriptor()
|
all: move v1 types over to the v2 repository
As a goal, v2 should not depend on v1. As another step towards that end,
we move all the types that used to be in the v1 protoapi package over to v2.
For now, we place MessageV1, ExtensionRangeV1, and ExtensionDescV1
in runtime/protoiface since these are types that generated messages will
probably have to reference forever. An alternative location could be
reflect/protoreflect, but it seems unfortunate to have to dirty the
namespace of that package with these types.
We move ExtensionFieldV1, ExtensionFieldsV1, and ExtensionFieldsOf
to internal/impl, since these are related to the implementation of a
generated message.
Since moving these types from v1 to v2 implies that the v1 protoapi
package is useless, we update all usages of v1 protoapi in the v2
repository to point to the relevant v2 type or functionality.
CL/168538 is the corresponding change to alter v1.
There will be a temporary build failure as it is not possible
to submit CL/168519 and CL/168538 atomically.
Change-Id: Ide4025c1b6af5b7f0696f4b65b988b4d10a50f0b
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/168519
Reviewed-by: Herbie Ong <herbie@google.com>
2019-03-20 18:29:32 -07:00
|
|
|
var parent piface.MessageV1
|
2019-08-02 16:58:08 -07:00
|
|
|
messageName := xd.ContainingMessage().FullName()
|
2019-05-13 14:32:56 -07:00
|
|
|
if mt, _ := preg.GlobalTypes.FindMessageByName(messageName); mt != nil {
|
2018-11-26 22:32:06 -08:00
|
|
|
// Create a new parent message and unwrap it if possible.
|
2019-01-09 02:57:13 -08:00
|
|
|
mv := mt.New().Interface()
|
2018-11-26 22:32:06 -08:00
|
|
|
t := reflect.TypeOf(mv)
|
2019-07-17 16:52:10 -07:00
|
|
|
if mv, ok := mv.(Unwrapper); ok {
|
2018-11-26 22:32:06 -08:00
|
|
|
t = reflect.TypeOf(mv.ProtoUnwrap())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check whether the message implements the legacy v1 Message interface.
|
|
|
|
mz := reflect.Zero(t).Interface()
|
all: move v1 types over to the v2 repository
As a goal, v2 should not depend on v1. As another step towards that end,
we move all the types that used to be in the v1 protoapi package over to v2.
For now, we place MessageV1, ExtensionRangeV1, and ExtensionDescV1
in runtime/protoiface since these are types that generated messages will
probably have to reference forever. An alternative location could be
reflect/protoreflect, but it seems unfortunate to have to dirty the
namespace of that package with these types.
We move ExtensionFieldV1, ExtensionFieldsV1, and ExtensionFieldsOf
to internal/impl, since these are related to the implementation of a
generated message.
Since moving these types from v1 to v2 implies that the v1 protoapi
package is useless, we update all usages of v1 protoapi in the v2
repository to point to the relevant v2 type or functionality.
CL/168538 is the corresponding change to alter v1.
There will be a temporary build failure as it is not possible
to submit CL/168519 and CL/168538 atomically.
Change-Id: Ide4025c1b6af5b7f0696f4b65b988b4d10a50f0b
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/168519
Reviewed-by: Herbie Ong <herbie@google.com>
2019-03-20 18:29:32 -07:00
|
|
|
if mz, ok := mz.(piface.MessageV1); ok {
|
2018-11-26 22:32:06 -08:00
|
|
|
parent = mz
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine the v1 extension type, which is unfortunately not the same as
|
|
|
|
// the v2 ExtensionType.GoType.
|
2019-05-01 12:29:25 -07:00
|
|
|
extType := xt.GoType()
|
2018-11-26 22:32:06 -08:00
|
|
|
switch extType.Kind() {
|
|
|
|
case reflect.Bool, reflect.Int32, reflect.Int64, reflect.Uint32, reflect.Uint64, reflect.Float32, reflect.Float64, reflect.String:
|
|
|
|
extType = reflect.PtrTo(extType) // T -> *T for singular scalar fields
|
|
|
|
case reflect.Ptr:
|
|
|
|
if extType.Elem().Kind() == reflect.Slice {
|
|
|
|
extType = extType.Elem() // *[]T -> []T for repeated fields
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reconstruct the legacy enum full name, which is an odd mixture of the
|
|
|
|
// proto package name with the Go type name.
|
|
|
|
var enumName string
|
2019-08-02 16:58:08 -07:00
|
|
|
if xd.Kind() == pref.EnumKind {
|
2018-11-26 22:32:06 -08:00
|
|
|
// Derive Go type name.
|
2019-05-01 12:29:25 -07:00
|
|
|
t := extType
|
|
|
|
if t.Kind() == reflect.Ptr || t.Kind() == reflect.Slice {
|
|
|
|
t = t.Elem()
|
2018-11-26 22:32:06 -08:00
|
|
|
}
|
2019-05-01 12:29:25 -07:00
|
|
|
enumName = t.Name()
|
2018-11-26 22:32:06 -08:00
|
|
|
|
|
|
|
// Derive the proto package name.
|
|
|
|
// For legacy enums, obtain the proto package from the raw descriptor.
|
|
|
|
var protoPkg string
|
2019-08-02 16:58:08 -07:00
|
|
|
if fd := xd.Enum().ParentFile(); fd != nil {
|
2018-11-26 22:32:06 -08:00
|
|
|
protoPkg = string(fd.Package())
|
|
|
|
}
|
2019-05-01 12:29:25 -07:00
|
|
|
if ed, ok := reflect.Zero(t).Interface().(enumV1); ok && protoPkg == "" {
|
2018-11-26 22:32:06 -08:00
|
|
|
b, _ := ed.EnumDescriptor()
|
2019-06-06 13:01:53 -07:00
|
|
|
protoPkg = string(legacyLoadFileDesc(b).Package())
|
2018-11-26 22:32:06 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if protoPkg != "" {
|
|
|
|
enumName = protoPkg + "." + enumName
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Derive the proto file that the extension was declared within.
|
|
|
|
var filename string
|
2019-08-02 16:58:08 -07:00
|
|
|
if fd := xd.ParentFile(); fd != nil {
|
2018-11-26 22:32:06 -08:00
|
|
|
filename = fd.Path()
|
|
|
|
}
|
|
|
|
|
all: move v1 types over to the v2 repository
As a goal, v2 should not depend on v1. As another step towards that end,
we move all the types that used to be in the v1 protoapi package over to v2.
For now, we place MessageV1, ExtensionRangeV1, and ExtensionDescV1
in runtime/protoiface since these are types that generated messages will
probably have to reference forever. An alternative location could be
reflect/protoreflect, but it seems unfortunate to have to dirty the
namespace of that package with these types.
We move ExtensionFieldV1, ExtensionFieldsV1, and ExtensionFieldsOf
to internal/impl, since these are related to the implementation of a
generated message.
Since moving these types from v1 to v2 implies that the v1 protoapi
package is useless, we update all usages of v1 protoapi in the v2
repository to point to the relevant v2 type or functionality.
CL/168538 is the corresponding change to alter v1.
There will be a temporary build failure as it is not possible
to submit CL/168519 and CL/168538 atomically.
Change-Id: Ide4025c1b6af5b7f0696f4b65b988b4d10a50f0b
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/168519
Reviewed-by: Herbie Ong <herbie@google.com>
2019-03-20 18:29:32 -07:00
|
|
|
// Construct and return a ExtensionDescV1.
|
|
|
|
d := &piface.ExtensionDescV1{
|
2019-05-01 12:29:25 -07:00
|
|
|
Type: xt,
|
2018-11-26 22:32:06 -08:00
|
|
|
ExtendedType: parent,
|
|
|
|
ExtensionType: reflect.Zero(extType).Interface(),
|
2019-08-02 16:58:08 -07:00
|
|
|
Field: int32(xd.Number()),
|
|
|
|
Name: string(xd.FullName()),
|
|
|
|
Tag: ptag.Marshal(xd, enumName),
|
2018-11-26 22:32:06 -08:00
|
|
|
Filename: filename,
|
|
|
|
}
|
2019-05-22 13:42:54 -04:00
|
|
|
if d, ok := legacyExtensionDescCache.LoadOrStore(xt, d); ok {
|
all: move v1 types over to the v2 repository
As a goal, v2 should not depend on v1. As another step towards that end,
we move all the types that used to be in the v1 protoapi package over to v2.
For now, we place MessageV1, ExtensionRangeV1, and ExtensionDescV1
in runtime/protoiface since these are types that generated messages will
probably have to reference forever. An alternative location could be
reflect/protoreflect, but it seems unfortunate to have to dirty the
namespace of that package with these types.
We move ExtensionFieldV1, ExtensionFieldsV1, and ExtensionFieldsOf
to internal/impl, since these are related to the implementation of a
generated message.
Since moving these types from v1 to v2 implies that the v1 protoapi
package is useless, we update all usages of v1 protoapi in the v2
repository to point to the relevant v2 type or functionality.
CL/168538 is the corresponding change to alter v1.
There will be a temporary build failure as it is not possible
to submit CL/168519 and CL/168538 atomically.
Change-Id: Ide4025c1b6af5b7f0696f4b65b988b4d10a50f0b
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/168519
Reviewed-by: Herbie Ong <herbie@google.com>
2019-03-20 18:29:32 -07:00
|
|
|
return d.(*piface.ExtensionDescV1)
|
2019-03-19 14:14:29 -07:00
|
|
|
}
|
2018-12-04 14:00:01 -08:00
|
|
|
return d
|
2018-11-26 22:32:06 -08:00
|
|
|
}
|
|
|
|
|
2019-05-22 13:42:54 -04:00
|
|
|
// legacyExtensionTypeFromDesc converts a protoiface.ExtensionDescV1 to a
|
2018-12-04 14:00:01 -08:00
|
|
|
// v2 protoreflect.ExtensionType. The returned descriptor type takes ownership
|
|
|
|
// of the input extension desc. The input must not be mutated so long as the
|
|
|
|
// returned type is still in use.
|
2019-05-22 13:42:54 -04:00
|
|
|
func legacyExtensionTypeFromDesc(d *piface.ExtensionDescV1) pref.ExtensionType {
|
2018-12-04 14:00:01 -08:00
|
|
|
// Fast-path: check whether an extension type is already nested within.
|
2018-11-26 22:32:06 -08:00
|
|
|
if d.Type != nil {
|
2018-12-04 14:00:01 -08:00
|
|
|
return d.Type
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fast-path: check the cache for whether this ExtensionType has already
|
|
|
|
// been converted from a legacy descriptor.
|
2019-05-22 13:42:54 -04:00
|
|
|
dk := legacyExtensionDescKeyOf(d)
|
|
|
|
if t, ok := legacyExtensionTypeCache.Load(dk); ok {
|
2018-12-04 14:00:01 -08:00
|
|
|
return t.(pref.ExtensionType)
|
2018-11-26 22:32:06 -08:00
|
|
|
}
|
|
|
|
|
2019-06-06 13:01:53 -07:00
|
|
|
// Resolve enum or message dependencies.
|
|
|
|
var ed pref.EnumDescriptor
|
|
|
|
var md pref.MessageDescriptor
|
2018-11-26 22:32:06 -08:00
|
|
|
t := reflect.TypeOf(d.ExtensionType)
|
|
|
|
isOptional := t.Kind() == reflect.Ptr && t.Elem().Kind() != reflect.Struct
|
|
|
|
isRepeated := t.Kind() == reflect.Slice && t.Elem().Kind() != reflect.Uint8
|
|
|
|
if isOptional || isRepeated {
|
|
|
|
t = t.Elem()
|
|
|
|
}
|
2019-06-06 13:01:53 -07:00
|
|
|
switch v := reflect.Zero(t).Interface().(type) {
|
|
|
|
case pref.Enum:
|
|
|
|
ed = v.Descriptor()
|
|
|
|
case enumV1:
|
|
|
|
ed = LegacyLoadEnumDesc(t)
|
|
|
|
case pref.ProtoMessage:
|
|
|
|
md = v.ProtoReflect().Descriptor()
|
|
|
|
case messageV1:
|
|
|
|
md = LegacyLoadMessageDesc(t)
|
2019-05-01 12:29:25 -07:00
|
|
|
}
|
2019-06-06 13:01:53 -07:00
|
|
|
|
|
|
|
// Derive basic field information from the struct tag.
|
|
|
|
var evs pref.EnumValueDescriptors
|
|
|
|
if ed != nil {
|
|
|
|
evs = ed.Values()
|
2018-11-26 22:32:06 -08:00
|
|
|
}
|
2019-06-06 13:01:53 -07:00
|
|
|
fd := ptag.Unmarshal(d.Tag, t, evs).(*filedesc.Field)
|
|
|
|
|
|
|
|
// Construct a v2 ExtensionType.
|
|
|
|
xd := &filedesc.Extension{L2: new(filedesc.ExtensionL2)}
|
|
|
|
xd.L0.ParentFile = filedesc.SurrogateProto2
|
|
|
|
xd.L0.FullName = pref.FullName(d.Name)
|
|
|
|
xd.L1.Number = pref.FieldNumber(d.Field)
|
|
|
|
xd.L2.Cardinality = fd.L1.Cardinality
|
|
|
|
xd.L1.Kind = fd.L1.Kind
|
|
|
|
xd.L2.IsPacked = fd.L1.IsPacked
|
|
|
|
xd.L2.Default = fd.L1.Default
|
|
|
|
xd.L1.Extendee = Export{}.MessageDescriptorOf(d.ExtendedType)
|
|
|
|
xd.L2.Enum = ed
|
|
|
|
xd.L2.Message = md
|
2019-07-17 16:52:10 -07:00
|
|
|
tt := reflect.TypeOf(d.ExtensionType)
|
|
|
|
if isOptional {
|
|
|
|
tt = tt.Elem()
|
|
|
|
} else if isRepeated {
|
|
|
|
tt = reflect.PtrTo(tt)
|
|
|
|
}
|
|
|
|
xt := LegacyExtensionTypeOf(xd, tt)
|
2018-11-26 22:32:06 -08:00
|
|
|
|
2018-12-04 14:00:01 -08:00
|
|
|
// Cache the conversion for both directions.
|
2019-05-22 13:42:54 -04:00
|
|
|
legacyExtensionDescCache.LoadOrStore(xt, d)
|
|
|
|
if xt, ok := legacyExtensionTypeCache.LoadOrStore(dk, xt); ok {
|
2019-03-19 14:14:29 -07:00
|
|
|
return xt.(pref.ExtensionType)
|
|
|
|
}
|
2018-12-04 14:00:01 -08:00
|
|
|
return xt
|
2018-11-26 22:32:06 -08:00
|
|
|
}
|
|
|
|
|
2019-05-22 13:42:54 -04:00
|
|
|
// LegacyExtensionTypeOf returns a protoreflect.ExtensionType where the
|
2019-07-17 16:52:10 -07:00
|
|
|
// element type of the field is t.
|
2018-11-26 22:32:06 -08:00
|
|
|
//
|
2019-05-01 12:29:25 -07:00
|
|
|
// This is exported for testing purposes.
|
2019-05-22 13:42:54 -04:00
|
|
|
func LegacyExtensionTypeOf(xd pref.ExtensionDescriptor, t reflect.Type) pref.ExtensionType {
|
2019-08-02 16:58:08 -07:00
|
|
|
xt := &legacyExtensionType{
|
|
|
|
typ: t,
|
|
|
|
conv: NewConverter(t, xd),
|
2018-11-26 22:32:06 -08:00
|
|
|
}
|
2019-08-02 16:58:08 -07:00
|
|
|
xt.desc = &extDesc{xd, xt}
|
|
|
|
return xt
|
2018-11-26 22:32:06 -08:00
|
|
|
}
|
|
|
|
|
2019-05-22 13:42:54 -04:00
|
|
|
type legacyExtensionType struct {
|
2019-08-02 16:58:08 -07:00
|
|
|
desc pref.ExtensionTypeDescriptor
|
2019-07-17 16:52:10 -07:00
|
|
|
typ reflect.Type
|
|
|
|
conv Converter
|
2018-11-26 22:32:06 -08:00
|
|
|
}
|
|
|
|
|
2019-07-17 16:52:10 -07:00
|
|
|
func (x *legacyExtensionType) GoType() reflect.Type { return x.typ }
|
|
|
|
func (x *legacyExtensionType) New() pref.Value { return x.conv.New() }
|
all: make handling of zero-value composites more consistent
We occasionally need to work with immutable, empty lists, maps, and
messages. Notably, Message.Get on an empty repeated field will return a
"frozen" empty value.
Move handling of these immutable, zero-length composites into Converter,
to unify the behavior of regular and extension fields.
Add a Zero method to Converter, MessageType, and ExtensionType, to
provide a consistent way to get an empty, frozen value of a composite
type. Adding this method to the public {Message,Extension}Type
interfaces does increase our API surface, but lets us (for example)
cleanly represent an empty map as a nil map rather than a non-nil
one wrapped in a frozenMap type.
Drop the frozen{List,Map,Message} types as no longer necessary.
(These types did have support for creating a read-only view of a
non-empty value, but we are not currently using that feature.)
Change-Id: Ia76f149d591da07b40ce75b7404a7ab8a60cb9d8
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/189339
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
2019-08-07 12:21:41 -07:00
|
|
|
func (x *legacyExtensionType) Zero() pref.Value { return x.conv.Zero() }
|
2019-07-17 16:52:10 -07:00
|
|
|
func (x *legacyExtensionType) ValueOf(v interface{}) pref.Value {
|
|
|
|
return x.conv.PBValueOf(reflect.ValueOf(v))
|
|
|
|
}
|
|
|
|
func (x *legacyExtensionType) InterfaceOf(v pref.Value) interface{} {
|
|
|
|
return x.conv.GoValueOf(v).Interface()
|
|
|
|
}
|
2019-08-02 16:58:08 -07:00
|
|
|
func (x *legacyExtensionType) Descriptor() pref.ExtensionTypeDescriptor { return x.desc }
|
|
|
|
|
|
|
|
type extDesc struct {
|
|
|
|
pref.ExtensionDescriptor
|
|
|
|
xt *legacyExtensionType
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *extDesc) Type() pref.ExtensionType { return t.xt }
|
|
|
|
func (t *extDesc) Descriptor() pref.ExtensionDescriptor { return t.ExtensionDescriptor }
|