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"
|
|
|
|
|
2019-09-05 17:25:15 -07:00
|
|
|
"google.golang.org/protobuf/internal/descopts"
|
2019-07-20 14:57:56 -07:00
|
|
|
"google.golang.org/protobuf/internal/encoding/messageset"
|
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-09-05 17:25:15 -07:00
|
|
|
"google.golang.org/protobuf/internal/pragma"
|
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-08-08 15:45:59 -07:00
|
|
|
func (xi *ExtensionInfo) initToLegacy() {
|
|
|
|
xd := xi.desc
|
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-09-03 16:30:39 -07:00
|
|
|
if mv, ok := mv.(unwrapper); ok {
|
|
|
|
t = reflect.TypeOf(mv.protoUnwrap())
|
2018-11-26 22:32:06 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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-08-08 15:45:59 -07:00
|
|
|
extType := xi.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
|
|
|
|
}
|
|
|
|
|
2019-08-23 12:18:57 -07:00
|
|
|
// Reconstruct the legacy enum full name.
|
2018-11-26 22:32:06 -08:00
|
|
|
var enumName string
|
2019-08-02 16:58:08 -07:00
|
|
|
if xd.Kind() == pref.EnumKind {
|
2019-08-23 12:18:57 -07:00
|
|
|
enumName = legacyEnumName(xd.Enum())
|
2018-11-26 22:32:06 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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()
|
|
|
|
}
|
|
|
|
|
2019-07-20 14:57:56 -07:00
|
|
|
// For MessageSet extensions, the name used is the parent message.
|
|
|
|
name := xd.FullName()
|
|
|
|
if messageset.IsMessageSetExtension(xd) {
|
|
|
|
name = name.Parent()
|
|
|
|
}
|
|
|
|
|
2019-08-08 15:45:59 -07:00
|
|
|
xi.ExtendedType = parent
|
|
|
|
xi.ExtensionType = reflect.Zero(extType).Interface()
|
|
|
|
xi.Field = int32(xd.Number())
|
2019-07-20 14:57:56 -07:00
|
|
|
xi.Name = string(name)
|
2019-08-08 15:45:59 -07:00
|
|
|
xi.Tag = ptag.Marshal(xd, enumName)
|
|
|
|
xi.Filename = filename
|
2018-11-26 22:32:06 -08:00
|
|
|
}
|
|
|
|
|
2019-08-08 15:45:59 -07:00
|
|
|
// initFromLegacy initializes an ExtensionInfo from
|
|
|
|
// the contents of the deprecated exported fields of the type.
|
|
|
|
func (xi *ExtensionInfo) initFromLegacy() {
|
2019-09-05 17:25:15 -07:00
|
|
|
// The v1 API returns "type incomplete" descriptors where only the
|
|
|
|
// field number is specified. In such a case, use a placeholder.
|
|
|
|
if xi.ExtendedType == nil || xi.ExtensionType == nil {
|
|
|
|
xd := placeholderExtension{
|
|
|
|
name: pref.FullName(xi.Name),
|
|
|
|
number: pref.FieldNumber(xi.Field),
|
|
|
|
}
|
|
|
|
xi.desc = extensionTypeDescriptor{xd, xi}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-06 13:01:53 -07:00
|
|
|
// Resolve enum or message dependencies.
|
|
|
|
var ed pref.EnumDescriptor
|
|
|
|
var md pref.MessageDescriptor
|
2019-08-08 15:45:59 -07:00
|
|
|
t := reflect.TypeOf(xi.ExtensionType)
|
2018-11-26 22:32:06 -08:00
|
|
|
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-08-08 15:45:59 -07:00
|
|
|
fd := ptag.Unmarshal(xi.Tag, t, evs).(*filedesc.Field)
|
2019-06-06 13:01:53 -07:00
|
|
|
|
|
|
|
// Construct a v2 ExtensionType.
|
|
|
|
xd := &filedesc.Extension{L2: new(filedesc.ExtensionL2)}
|
|
|
|
xd.L0.ParentFile = filedesc.SurrogateProto2
|
2019-08-08 15:45:59 -07:00
|
|
|
xd.L0.FullName = pref.FullName(xi.Name)
|
|
|
|
xd.L1.Number = pref.FieldNumber(xi.Field)
|
2019-09-16 13:30:15 -07:00
|
|
|
xd.L1.Cardinality = fd.L1.Cardinality
|
2019-06-06 13:01:53 -07:00
|
|
|
xd.L1.Kind = fd.L1.Kind
|
|
|
|
xd.L2.IsPacked = fd.L1.IsPacked
|
|
|
|
xd.L2.Default = fd.L1.Default
|
2019-08-08 15:45:59 -07:00
|
|
|
xd.L1.Extendee = Export{}.MessageDescriptorOf(xi.ExtendedType)
|
2019-06-06 13:01:53 -07:00
|
|
|
xd.L2.Enum = ed
|
|
|
|
xd.L2.Message = md
|
2019-07-20 14:57:56 -07:00
|
|
|
|
|
|
|
// Derive real extension field name for MessageSets.
|
|
|
|
if messageset.IsMessageSet(xd.L1.Extendee) && md.FullName() == xd.L0.FullName {
|
|
|
|
xd.L0.FullName = xd.L0.FullName.Append(messageset.ExtensionName)
|
|
|
|
}
|
|
|
|
|
2019-08-08 15:45:59 -07:00
|
|
|
tt := reflect.TypeOf(xi.ExtensionType)
|
2019-07-17 16:52:10 -07:00
|
|
|
if isOptional {
|
|
|
|
tt = tt.Elem()
|
|
|
|
}
|
2019-08-08 15:45:59 -07:00
|
|
|
xi.goType = tt
|
2019-09-16 13:30:15 -07:00
|
|
|
xi.desc = extensionTypeDescriptor{xd, xi}
|
2018-11-26 22:32:06 -08:00
|
|
|
}
|
2019-09-05 17:25:15 -07:00
|
|
|
|
|
|
|
type placeholderExtension struct {
|
|
|
|
name pref.FullName
|
|
|
|
number pref.FieldNumber
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x placeholderExtension) ParentFile() pref.FileDescriptor { return nil }
|
|
|
|
func (x placeholderExtension) Parent() pref.Descriptor { return nil }
|
|
|
|
func (x placeholderExtension) Index() int { return 0 }
|
|
|
|
func (x placeholderExtension) Syntax() pref.Syntax { return 0 }
|
|
|
|
func (x placeholderExtension) Name() pref.Name { return x.name.Name() }
|
|
|
|
func (x placeholderExtension) FullName() pref.FullName { return x.name }
|
|
|
|
func (x placeholderExtension) IsPlaceholder() bool { return true }
|
|
|
|
func (x placeholderExtension) Options() pref.ProtoMessage { return descopts.Field }
|
|
|
|
func (x placeholderExtension) Number() pref.FieldNumber { return x.number }
|
|
|
|
func (x placeholderExtension) Cardinality() pref.Cardinality { return 0 }
|
|
|
|
func (x placeholderExtension) Kind() pref.Kind { return 0 }
|
|
|
|
func (x placeholderExtension) HasJSONName() bool { return false }
|
2020-06-24 16:47:32 -07:00
|
|
|
func (x placeholderExtension) JSONName() string { return "[" + string(x.name) + "]" }
|
|
|
|
func (x placeholderExtension) TextName() string { return "[" + string(x.name) + "]" }
|
2020-04-28 14:44:38 -07:00
|
|
|
func (x placeholderExtension) HasPresence() bool { return false }
|
|
|
|
func (x placeholderExtension) HasOptionalKeyword() bool { return false }
|
2019-09-05 17:25:15 -07:00
|
|
|
func (x placeholderExtension) IsExtension() bool { return true }
|
|
|
|
func (x placeholderExtension) IsWeak() bool { return false }
|
|
|
|
func (x placeholderExtension) IsPacked() bool { return false }
|
|
|
|
func (x placeholderExtension) IsList() bool { return false }
|
|
|
|
func (x placeholderExtension) IsMap() bool { return false }
|
|
|
|
func (x placeholderExtension) MapKey() pref.FieldDescriptor { return nil }
|
|
|
|
func (x placeholderExtension) MapValue() pref.FieldDescriptor { return nil }
|
|
|
|
func (x placeholderExtension) HasDefault() bool { return false }
|
|
|
|
func (x placeholderExtension) Default() pref.Value { return pref.Value{} }
|
|
|
|
func (x placeholderExtension) DefaultEnumValue() pref.EnumValueDescriptor { return nil }
|
|
|
|
func (x placeholderExtension) ContainingOneof() pref.OneofDescriptor { return nil }
|
|
|
|
func (x placeholderExtension) ContainingMessage() pref.MessageDescriptor { return nil }
|
|
|
|
func (x placeholderExtension) Enum() pref.EnumDescriptor { return nil }
|
|
|
|
func (x placeholderExtension) Message() pref.MessageDescriptor { return nil }
|
|
|
|
func (x placeholderExtension) ProtoType(pref.FieldDescriptor) { return }
|
|
|
|
func (x placeholderExtension) ProtoInternal(pragma.DoNotImplement) { return }
|