2018-10-18 11:06:29 -07: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.
|
|
|
|
|
2018-11-26 22:32:06 -08:00
|
|
|
package impl_test
|
2018-10-18 11:06:29 -07:00
|
|
|
|
|
|
|
import (
|
2019-06-06 13:01:53 -07:00
|
|
|
"fmt"
|
2018-10-18 11:06:29 -07:00
|
|
|
"reflect"
|
2019-05-22 13:42:54 -04:00
|
|
|
"sync"
|
2018-10-18 11:06:29 -07:00
|
|
|
"testing"
|
|
|
|
|
2019-04-25 23:48:08 -07:00
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
"github.com/google/go-cmp/cmp/cmpopts"
|
2020-02-25 12:51:10 -08:00
|
|
|
|
2019-06-06 13:01:53 -07:00
|
|
|
"google.golang.org/protobuf/encoding/prototext"
|
2019-05-13 23:55:40 -07:00
|
|
|
pimpl "google.golang.org/protobuf/internal/impl"
|
2019-06-06 13:01:53 -07:00
|
|
|
"google.golang.org/protobuf/internal/pragma"
|
|
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
pdesc "google.golang.org/protobuf/reflect/protodesc"
|
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"
|
|
|
|
|
2020-01-11 00:25:01 -08:00
|
|
|
proto2_20180125 "google.golang.org/protobuf/internal/testprotos/legacy/proto2_20180125_92554152"
|
2019-06-06 13:01:53 -07:00
|
|
|
"google.golang.org/protobuf/types/descriptorpb"
|
2018-10-18 11:06:29 -07:00
|
|
|
)
|
|
|
|
|
2019-06-06 13:01:53 -07:00
|
|
|
type LegacyTestMessage struct {
|
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
|
|
|
XXX_unrecognized []byte
|
2019-06-04 16:20:00 -07:00
|
|
|
XXX_InternalExtensions map[int32]pimpl.ExtensionField
|
2018-10-31 18:23:42 -07:00
|
|
|
}
|
|
|
|
|
2019-06-06 13:01:53 -07:00
|
|
|
func (*LegacyTestMessage) Reset() {}
|
|
|
|
func (*LegacyTestMessage) String() string { return "" }
|
|
|
|
func (*LegacyTestMessage) ProtoMessage() {}
|
|
|
|
func (*LegacyTestMessage) ExtensionRangeArray() []piface.ExtensionRangeV1 {
|
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 []piface.ExtensionRangeV1{{Start: 10, End: 20}, {Start: 40, End: 80}, {Start: 10000, End: 20000}}
|
2018-10-31 18:23:42 -07:00
|
|
|
}
|
2019-06-06 13:01:53 -07:00
|
|
|
func (*LegacyTestMessage) Descriptor() ([]byte, []int) { return legacyFD, []int{0} }
|
|
|
|
|
|
|
|
var legacyFD = func() []byte {
|
|
|
|
b, _ := proto.Marshal(pdesc.ToFileDescriptorProto(mustMakeFileDesc(`
|
|
|
|
name: "legacy.proto"
|
|
|
|
syntax: "proto2"
|
|
|
|
message_type: [{
|
|
|
|
name: "LegacyTestMessage"
|
|
|
|
extension_range: [{start:10 end:20}, {start:40 end:80}, {start:10000 end:20000}]
|
|
|
|
}]
|
|
|
|
`, nil)))
|
|
|
|
return pimpl.Export{}.CompressGZIP(b)
|
|
|
|
}()
|
2018-10-31 18:23:42 -07:00
|
|
|
|
2019-05-01 12:29:25 -07:00
|
|
|
func init() {
|
2019-06-06 13:01:53 -07:00
|
|
|
mt := pimpl.Export{}.MessageTypeOf((*LegacyTestMessage)(nil))
|
2019-10-08 13:28:53 -07:00
|
|
|
preg.GlobalFiles.RegisterFile(mt.Descriptor().ParentFile())
|
|
|
|
preg.GlobalTypes.RegisterMessage(mt)
|
2019-05-01 12:29:25 -07:00
|
|
|
}
|
|
|
|
|
2019-07-17 16:52:10 -07:00
|
|
|
func mustMakeExtensionType(fileDesc, extDesc string, t reflect.Type, r pdesc.Resolver) pref.ExtensionType {
|
2019-06-06 13:01:53 -07:00
|
|
|
s := fmt.Sprintf(`name:"test.proto" syntax:"proto2" %s extension:[{%s}]`, fileDesc, extDesc)
|
|
|
|
xd := mustMakeFileDesc(s, r).Extensions().Get(0)
|
2019-08-08 15:45:59 -07:00
|
|
|
xi := &pimpl.ExtensionInfo{}
|
|
|
|
pimpl.InitExtensionInfo(xi, xd, t)
|
|
|
|
return xi
|
2019-06-06 13:01:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func mustMakeFileDesc(s string, r pdesc.Resolver) pref.FileDescriptor {
|
|
|
|
pb := new(descriptorpb.FileDescriptorProto)
|
|
|
|
if err := prototext.Unmarshal([]byte(s), pb); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
fd, err := pdesc.NewFile(pb, r)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return fd
|
|
|
|
}
|
|
|
|
|
2018-11-26 22:32:06 -08:00
|
|
|
var (
|
2019-06-06 13:01:53 -07:00
|
|
|
testParentDesc = pimpl.Export{}.MessageDescriptorOf((*LegacyTestMessage)(nil))
|
2019-05-22 13:42:54 -04:00
|
|
|
testEnumV1Desc = pimpl.Export{}.EnumDescriptorOf(proto2_20180125.Message_ChildEnum(0))
|
|
|
|
testMessageV1Desc = pimpl.Export{}.MessageDescriptorOf((*proto2_20180125.Message_ChildMessage)(nil))
|
2019-08-06 15:43:25 -07:00
|
|
|
testMessageV2Desc = enumMessagesType.Desc
|
2018-11-26 22:32:06 -08:00
|
|
|
|
2019-10-08 13:28:53 -07:00
|
|
|
depReg = newFileRegistry(
|
2019-06-06 13:01:53 -07:00
|
|
|
testParentDesc.ParentFile(),
|
|
|
|
testEnumV1Desc.ParentFile(),
|
|
|
|
testMessageV1Desc.ParentFile(),
|
2019-08-06 15:43:25 -07:00
|
|
|
enumProto2Desc.ParentFile(),
|
2019-06-06 13:01:53 -07:00
|
|
|
testMessageV2Desc.ParentFile(),
|
|
|
|
)
|
2018-11-26 22:32:06 -08:00
|
|
|
extensionTypes = []pref.ExtensionType{
|
2019-06-06 13:01:53 -07:00
|
|
|
mustMakeExtensionType(
|
|
|
|
`package:"fizz.buzz" dependency:"legacy.proto"`,
|
|
|
|
`name:"optional_bool" number:10000 label:LABEL_OPTIONAL type:TYPE_BOOL default_value:"true" extendee:".LegacyTestMessage"`,
|
2019-07-17 16:52:10 -07:00
|
|
|
reflect.TypeOf(false), depReg,
|
2019-06-06 13:01:53 -07:00
|
|
|
),
|
|
|
|
mustMakeExtensionType(
|
|
|
|
`package:"fizz.buzz" dependency:"legacy.proto"`,
|
|
|
|
`name:"optional_int32" number:10001 label:LABEL_OPTIONAL type:TYPE_INT32 default_value:"-12345" extendee:".LegacyTestMessage"`,
|
2019-07-17 16:52:10 -07:00
|
|
|
reflect.TypeOf(int32(0)), depReg,
|
2019-06-06 13:01:53 -07:00
|
|
|
),
|
|
|
|
mustMakeExtensionType(
|
|
|
|
`package:"fizz.buzz" dependency:"legacy.proto"`,
|
|
|
|
`name:"optional_uint32" number:10002 label:LABEL_OPTIONAL type:TYPE_UINT32 default_value:"3200" extendee:".LegacyTestMessage"`,
|
2019-07-17 16:52:10 -07:00
|
|
|
reflect.TypeOf(uint32(0)), depReg,
|
2019-06-06 13:01:53 -07:00
|
|
|
),
|
|
|
|
mustMakeExtensionType(
|
|
|
|
`package:"fizz.buzz" dependency:"legacy.proto"`,
|
|
|
|
`name:"optional_float" number:10003 label:LABEL_OPTIONAL type:TYPE_FLOAT default_value:"3.14159" extendee:".LegacyTestMessage"`,
|
2019-07-17 16:52:10 -07:00
|
|
|
reflect.TypeOf(float32(0)), depReg,
|
2019-06-06 13:01:53 -07:00
|
|
|
),
|
|
|
|
mustMakeExtensionType(
|
|
|
|
`package:"fizz.buzz" dependency:"legacy.proto"`,
|
|
|
|
`name:"optional_string" number:10004 label:LABEL_OPTIONAL type:TYPE_STRING default_value:"hello, \"world!\"\n" extendee:".LegacyTestMessage"`,
|
2019-07-17 16:52:10 -07:00
|
|
|
reflect.TypeOf(""), depReg,
|
2019-06-06 13:01:53 -07:00
|
|
|
),
|
|
|
|
mustMakeExtensionType(
|
|
|
|
`package:"fizz.buzz" dependency:"legacy.proto"`,
|
|
|
|
`name:"optional_bytes" number:10005 label:LABEL_OPTIONAL type:TYPE_BYTES default_value:"dead\\336\\255\\276\\357beef" extendee:".LegacyTestMessage"`,
|
2019-07-17 16:52:10 -07:00
|
|
|
reflect.TypeOf(([]byte)(nil)), depReg,
|
2019-06-06 13:01:53 -07:00
|
|
|
),
|
|
|
|
mustMakeExtensionType(
|
2020-01-11 00:25:01 -08:00
|
|
|
`package:"fizz.buzz" dependency:["legacy.proto", "proto2_20180125_92554152/test.proto"]`,
|
2019-06-06 13:01:53 -07:00
|
|
|
`name:"optional_enum_v1" number:10006 label:LABEL_OPTIONAL type:TYPE_ENUM type_name:".google.golang.org.proto2_20180125.Message.ChildEnum" default_value:"ALPHA" extendee:".LegacyTestMessage"`,
|
2019-07-17 16:52:10 -07:00
|
|
|
reflect.TypeOf(proto2_20180125.Message_ChildEnum(0)), depReg,
|
2019-06-06 13:01:53 -07:00
|
|
|
),
|
|
|
|
mustMakeExtensionType(
|
2020-01-11 00:25:01 -08:00
|
|
|
`package:"fizz.buzz" dependency:["legacy.proto", "proto2_20180125_92554152/test.proto"]`,
|
2019-06-06 13:01:53 -07:00
|
|
|
`name:"optional_message_v1" number:10007 label:LABEL_OPTIONAL type:TYPE_MESSAGE type_name:".google.golang.org.proto2_20180125.Message.ChildMessage" extendee:".LegacyTestMessage"`,
|
2019-07-17 16:52:10 -07:00
|
|
|
reflect.TypeOf((*proto2_20180125.Message_ChildMessage)(nil)), depReg,
|
2019-06-06 13:01:53 -07:00
|
|
|
),
|
|
|
|
mustMakeExtensionType(
|
|
|
|
`package:"fizz.buzz" dependency:["legacy.proto", "enum2.proto"]`,
|
|
|
|
`name:"optional_enum_v2" number:10008 label:LABEL_OPTIONAL type:TYPE_ENUM type_name:".EnumProto2" default_value:"DEAD" extendee:".LegacyTestMessage"`,
|
2019-07-17 16:52:10 -07:00
|
|
|
reflect.TypeOf(EnumProto2(0)), depReg,
|
2019-06-06 13:01:53 -07:00
|
|
|
),
|
|
|
|
mustMakeExtensionType(
|
|
|
|
`package:"fizz.buzz" dependency:["legacy.proto", "enum-messages.proto"]`,
|
|
|
|
`name:"optional_message_v2" number:10009 label:LABEL_OPTIONAL type:TYPE_MESSAGE type_name:".EnumMessages" extendee:".LegacyTestMessage"`,
|
2019-07-17 16:52:10 -07:00
|
|
|
reflect.TypeOf((*EnumMessages)(nil)), depReg,
|
2019-06-06 13:01:53 -07:00
|
|
|
),
|
|
|
|
mustMakeExtensionType(
|
|
|
|
`package:"fizz.buzz" dependency:"legacy.proto"`,
|
|
|
|
`name:"repeated_bool" number:10010 label:LABEL_REPEATED type:TYPE_BOOL extendee:".LegacyTestMessage"`,
|
2019-09-16 13:30:15 -07:00
|
|
|
reflect.TypeOf([]bool(nil)), depReg,
|
2019-06-06 13:01:53 -07:00
|
|
|
),
|
|
|
|
mustMakeExtensionType(
|
|
|
|
`package:"fizz.buzz" dependency:"legacy.proto"`,
|
|
|
|
`name:"repeated_int32" number:10011 label:LABEL_REPEATED type:TYPE_INT32 extendee:".LegacyTestMessage"`,
|
2019-09-16 13:30:15 -07:00
|
|
|
reflect.TypeOf([]int32(nil)), depReg,
|
2019-06-06 13:01:53 -07:00
|
|
|
),
|
|
|
|
mustMakeExtensionType(
|
|
|
|
`package:"fizz.buzz" dependency:"legacy.proto"`,
|
|
|
|
`name:"repeated_uint32" number:10012 label:LABEL_REPEATED type:TYPE_UINT32 extendee:".LegacyTestMessage"`,
|
2019-09-16 13:30:15 -07:00
|
|
|
reflect.TypeOf([]uint32(nil)), depReg,
|
2019-06-06 13:01:53 -07:00
|
|
|
),
|
|
|
|
mustMakeExtensionType(
|
|
|
|
`package:"fizz.buzz" dependency:"legacy.proto"`,
|
|
|
|
`name:"repeated_float" number:10013 label:LABEL_REPEATED type:TYPE_FLOAT extendee:".LegacyTestMessage"`,
|
2019-09-16 13:30:15 -07:00
|
|
|
reflect.TypeOf([]float32(nil)), depReg,
|
2019-06-06 13:01:53 -07:00
|
|
|
),
|
|
|
|
mustMakeExtensionType(
|
|
|
|
`package:"fizz.buzz" dependency:"legacy.proto"`,
|
|
|
|
`name:"repeated_string" number:10014 label:LABEL_REPEATED type:TYPE_STRING extendee:".LegacyTestMessage"`,
|
2019-09-16 13:30:15 -07:00
|
|
|
reflect.TypeOf([]string(nil)), depReg,
|
2019-06-06 13:01:53 -07:00
|
|
|
),
|
|
|
|
mustMakeExtensionType(
|
|
|
|
`package:"fizz.buzz" dependency:"legacy.proto"`,
|
|
|
|
`name:"repeated_bytes" number:10015 label:LABEL_REPEATED type:TYPE_BYTES extendee:".LegacyTestMessage"`,
|
2019-09-16 13:30:15 -07:00
|
|
|
reflect.TypeOf([][]byte(nil)), depReg,
|
2019-06-06 13:01:53 -07:00
|
|
|
),
|
|
|
|
mustMakeExtensionType(
|
2020-01-11 00:25:01 -08:00
|
|
|
`package:"fizz.buzz" dependency:["legacy.proto", "proto2_20180125_92554152/test.proto"]`,
|
2019-06-06 13:01:53 -07:00
|
|
|
`name:"repeated_enum_v1" number:10016 label:LABEL_REPEATED type:TYPE_ENUM type_name:".google.golang.org.proto2_20180125.Message.ChildEnum" extendee:".LegacyTestMessage"`,
|
2019-09-16 13:30:15 -07:00
|
|
|
reflect.TypeOf([]proto2_20180125.Message_ChildEnum(nil)), depReg,
|
2019-06-06 13:01:53 -07:00
|
|
|
),
|
|
|
|
mustMakeExtensionType(
|
2020-01-11 00:25:01 -08:00
|
|
|
`package:"fizz.buzz" dependency:["legacy.proto", "proto2_20180125_92554152/test.proto"]`,
|
2019-06-06 13:01:53 -07:00
|
|
|
`name:"repeated_message_v1" number:10017 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".google.golang.org.proto2_20180125.Message.ChildMessage" extendee:".LegacyTestMessage"`,
|
2019-09-16 13:30:15 -07:00
|
|
|
reflect.TypeOf([]*proto2_20180125.Message_ChildMessage(nil)), depReg,
|
2019-06-06 13:01:53 -07:00
|
|
|
),
|
|
|
|
mustMakeExtensionType(
|
|
|
|
`package:"fizz.buzz" dependency:["legacy.proto", "enum2.proto"]`,
|
|
|
|
`name:"repeated_enum_v2" number:10018 label:LABEL_REPEATED type:TYPE_ENUM type_name:".EnumProto2" extendee:".LegacyTestMessage"`,
|
2019-09-16 13:30:15 -07:00
|
|
|
reflect.TypeOf([]EnumProto2(nil)), depReg,
|
2019-06-06 13:01:53 -07:00
|
|
|
),
|
|
|
|
mustMakeExtensionType(
|
|
|
|
`package:"fizz.buzz" dependency:["legacy.proto", "enum-messages.proto"]`,
|
|
|
|
`name:"repeated_message_v2" number:10019 label:LABEL_REPEATED type:TYPE_MESSAGE type_name:".EnumMessages" extendee:".LegacyTestMessage"`,
|
2019-09-16 13:30:15 -07:00
|
|
|
reflect.TypeOf([]*EnumMessages(nil)), depReg,
|
2019-06-06 13:01:53 -07:00
|
|
|
),
|
2018-11-26 22:32:06 -08:00
|
|
|
}
|
|
|
|
|
2019-08-08 15:45:59 -07:00
|
|
|
extensionDescs = []*pimpl.ExtensionInfo{{
|
2019-06-06 13:01:53 -07:00
|
|
|
ExtendedType: (*LegacyTestMessage)(nil),
|
2018-11-26 22:32:06 -08:00
|
|
|
ExtensionType: (*bool)(nil),
|
|
|
|
Field: 10000,
|
|
|
|
Name: "fizz.buzz.optional_bool",
|
|
|
|
Tag: "varint,10000,opt,name=optional_bool,def=1",
|
2019-06-06 13:01:53 -07:00
|
|
|
Filename: "test.proto",
|
2018-11-26 22:32:06 -08:00
|
|
|
}, {
|
2019-06-06 13:01:53 -07:00
|
|
|
ExtendedType: (*LegacyTestMessage)(nil),
|
2018-11-26 22:32:06 -08:00
|
|
|
ExtensionType: (*int32)(nil),
|
|
|
|
Field: 10001,
|
|
|
|
Name: "fizz.buzz.optional_int32",
|
|
|
|
Tag: "varint,10001,opt,name=optional_int32,def=-12345",
|
2019-06-06 13:01:53 -07:00
|
|
|
Filename: "test.proto",
|
2018-11-26 22:32:06 -08:00
|
|
|
}, {
|
2019-06-06 13:01:53 -07:00
|
|
|
ExtendedType: (*LegacyTestMessage)(nil),
|
2018-11-26 22:32:06 -08:00
|
|
|
ExtensionType: (*uint32)(nil),
|
|
|
|
Field: 10002,
|
|
|
|
Name: "fizz.buzz.optional_uint32",
|
|
|
|
Tag: "varint,10002,opt,name=optional_uint32,def=3200",
|
2019-06-06 13:01:53 -07:00
|
|
|
Filename: "test.proto",
|
2018-11-26 22:32:06 -08:00
|
|
|
}, {
|
2019-06-06 13:01:53 -07:00
|
|
|
ExtendedType: (*LegacyTestMessage)(nil),
|
2018-11-26 22:32:06 -08:00
|
|
|
ExtensionType: (*float32)(nil),
|
|
|
|
Field: 10003,
|
|
|
|
Name: "fizz.buzz.optional_float",
|
|
|
|
Tag: "fixed32,10003,opt,name=optional_float,def=3.14159",
|
2019-06-06 13:01:53 -07:00
|
|
|
Filename: "test.proto",
|
2018-11-26 22:32:06 -08:00
|
|
|
}, {
|
2019-06-06 13:01:53 -07:00
|
|
|
ExtendedType: (*LegacyTestMessage)(nil),
|
2018-11-26 22:32:06 -08:00
|
|
|
ExtensionType: (*string)(nil),
|
|
|
|
Field: 10004,
|
|
|
|
Name: "fizz.buzz.optional_string",
|
|
|
|
Tag: "bytes,10004,opt,name=optional_string,def=hello, \"world!\"\n",
|
2019-06-06 13:01:53 -07:00
|
|
|
Filename: "test.proto",
|
2018-11-26 22:32:06 -08:00
|
|
|
}, {
|
2019-06-06 13:01:53 -07:00
|
|
|
ExtendedType: (*LegacyTestMessage)(nil),
|
2018-11-26 22:32:06 -08:00
|
|
|
ExtensionType: ([]byte)(nil),
|
|
|
|
Field: 10005,
|
|
|
|
Name: "fizz.buzz.optional_bytes",
|
|
|
|
Tag: "bytes,10005,opt,name=optional_bytes,def=dead\\336\\255\\276\\357beef",
|
2019-06-06 13:01:53 -07:00
|
|
|
Filename: "test.proto",
|
2018-11-26 22:32:06 -08:00
|
|
|
}, {
|
2019-06-06 13:01:53 -07:00
|
|
|
ExtendedType: (*LegacyTestMessage)(nil),
|
2018-11-26 22:32:06 -08:00
|
|
|
ExtensionType: (*proto2_20180125.Message_ChildEnum)(nil),
|
|
|
|
Field: 10006,
|
|
|
|
Name: "fizz.buzz.optional_enum_v1",
|
|
|
|
Tag: "varint,10006,opt,name=optional_enum_v1,enum=google.golang.org.proto2_20180125.Message_ChildEnum,def=0",
|
2019-06-06 13:01:53 -07:00
|
|
|
Filename: "test.proto",
|
2018-11-26 22:32:06 -08:00
|
|
|
}, {
|
2019-06-06 13:01:53 -07:00
|
|
|
ExtendedType: (*LegacyTestMessage)(nil),
|
2018-11-26 22:32:06 -08:00
|
|
|
ExtensionType: (*proto2_20180125.Message_ChildMessage)(nil),
|
|
|
|
Field: 10007,
|
|
|
|
Name: "fizz.buzz.optional_message_v1",
|
|
|
|
Tag: "bytes,10007,opt,name=optional_message_v1",
|
2019-06-06 13:01:53 -07:00
|
|
|
Filename: "test.proto",
|
2018-11-26 22:32:06 -08:00
|
|
|
}, {
|
2019-06-06 13:01:53 -07:00
|
|
|
ExtendedType: (*LegacyTestMessage)(nil),
|
2018-11-26 22:32:06 -08:00
|
|
|
ExtensionType: (*EnumProto2)(nil),
|
|
|
|
Field: 10008,
|
|
|
|
Name: "fizz.buzz.optional_enum_v2",
|
|
|
|
Tag: "varint,10008,opt,name=optional_enum_v2,enum=EnumProto2,def=57005",
|
2019-06-06 13:01:53 -07:00
|
|
|
Filename: "test.proto",
|
2018-11-26 22:32:06 -08:00
|
|
|
}, {
|
2019-06-06 13:01:53 -07:00
|
|
|
ExtendedType: (*LegacyTestMessage)(nil),
|
2018-11-26 22:32:06 -08:00
|
|
|
ExtensionType: (*EnumMessages)(nil),
|
|
|
|
Field: 10009,
|
|
|
|
Name: "fizz.buzz.optional_message_v2",
|
|
|
|
Tag: "bytes,10009,opt,name=optional_message_v2",
|
2019-06-06 13:01:53 -07:00
|
|
|
Filename: "test.proto",
|
2018-11-26 22:32:06 -08:00
|
|
|
}, {
|
2019-06-06 13:01:53 -07:00
|
|
|
ExtendedType: (*LegacyTestMessage)(nil),
|
2018-11-26 22:32:06 -08:00
|
|
|
ExtensionType: ([]bool)(nil),
|
|
|
|
Field: 10010,
|
|
|
|
Name: "fizz.buzz.repeated_bool",
|
|
|
|
Tag: "varint,10010,rep,name=repeated_bool",
|
2019-06-06 13:01:53 -07:00
|
|
|
Filename: "test.proto",
|
2018-11-26 22:32:06 -08:00
|
|
|
}, {
|
2019-06-06 13:01:53 -07:00
|
|
|
ExtendedType: (*LegacyTestMessage)(nil),
|
2018-11-26 22:32:06 -08:00
|
|
|
ExtensionType: ([]int32)(nil),
|
|
|
|
Field: 10011,
|
|
|
|
Name: "fizz.buzz.repeated_int32",
|
|
|
|
Tag: "varint,10011,rep,name=repeated_int32",
|
2019-06-06 13:01:53 -07:00
|
|
|
Filename: "test.proto",
|
2018-11-26 22:32:06 -08:00
|
|
|
}, {
|
2019-06-06 13:01:53 -07:00
|
|
|
ExtendedType: (*LegacyTestMessage)(nil),
|
2018-11-26 22:32:06 -08:00
|
|
|
ExtensionType: ([]uint32)(nil),
|
|
|
|
Field: 10012,
|
|
|
|
Name: "fizz.buzz.repeated_uint32",
|
|
|
|
Tag: "varint,10012,rep,name=repeated_uint32",
|
2019-06-06 13:01:53 -07:00
|
|
|
Filename: "test.proto",
|
2018-11-26 22:32:06 -08:00
|
|
|
}, {
|
2019-06-06 13:01:53 -07:00
|
|
|
ExtendedType: (*LegacyTestMessage)(nil),
|
2018-11-26 22:32:06 -08:00
|
|
|
ExtensionType: ([]float32)(nil),
|
|
|
|
Field: 10013,
|
|
|
|
Name: "fizz.buzz.repeated_float",
|
|
|
|
Tag: "fixed32,10013,rep,name=repeated_float",
|
2019-06-06 13:01:53 -07:00
|
|
|
Filename: "test.proto",
|
2018-11-26 22:32:06 -08:00
|
|
|
}, {
|
2019-06-06 13:01:53 -07:00
|
|
|
ExtendedType: (*LegacyTestMessage)(nil),
|
2018-11-26 22:32:06 -08:00
|
|
|
ExtensionType: ([]string)(nil),
|
|
|
|
Field: 10014,
|
|
|
|
Name: "fizz.buzz.repeated_string",
|
|
|
|
Tag: "bytes,10014,rep,name=repeated_string",
|
2019-06-06 13:01:53 -07:00
|
|
|
Filename: "test.proto",
|
2018-11-26 22:32:06 -08:00
|
|
|
}, {
|
2019-06-06 13:01:53 -07:00
|
|
|
ExtendedType: (*LegacyTestMessage)(nil),
|
2018-11-26 22:32:06 -08:00
|
|
|
ExtensionType: ([][]byte)(nil),
|
|
|
|
Field: 10015,
|
|
|
|
Name: "fizz.buzz.repeated_bytes",
|
|
|
|
Tag: "bytes,10015,rep,name=repeated_bytes",
|
2019-06-06 13:01:53 -07:00
|
|
|
Filename: "test.proto",
|
2018-11-26 22:32:06 -08:00
|
|
|
}, {
|
2019-06-06 13:01:53 -07:00
|
|
|
ExtendedType: (*LegacyTestMessage)(nil),
|
2018-11-26 22:32:06 -08:00
|
|
|
ExtensionType: ([]proto2_20180125.Message_ChildEnum)(nil),
|
|
|
|
Field: 10016,
|
|
|
|
Name: "fizz.buzz.repeated_enum_v1",
|
|
|
|
Tag: "varint,10016,rep,name=repeated_enum_v1,enum=google.golang.org.proto2_20180125.Message_ChildEnum",
|
2019-06-06 13:01:53 -07:00
|
|
|
Filename: "test.proto",
|
2018-11-26 22:32:06 -08:00
|
|
|
}, {
|
2019-06-06 13:01:53 -07:00
|
|
|
ExtendedType: (*LegacyTestMessage)(nil),
|
2018-11-26 22:32:06 -08:00
|
|
|
ExtensionType: ([]*proto2_20180125.Message_ChildMessage)(nil),
|
|
|
|
Field: 10017,
|
|
|
|
Name: "fizz.buzz.repeated_message_v1",
|
|
|
|
Tag: "bytes,10017,rep,name=repeated_message_v1",
|
2019-06-06 13:01:53 -07:00
|
|
|
Filename: "test.proto",
|
2018-11-26 22:32:06 -08:00
|
|
|
}, {
|
2019-06-06 13:01:53 -07:00
|
|
|
ExtendedType: (*LegacyTestMessage)(nil),
|
2018-11-26 22:32:06 -08:00
|
|
|
ExtensionType: ([]EnumProto2)(nil),
|
|
|
|
Field: 10018,
|
|
|
|
Name: "fizz.buzz.repeated_enum_v2",
|
|
|
|
Tag: "varint,10018,rep,name=repeated_enum_v2,enum=EnumProto2",
|
2019-06-06 13:01:53 -07:00
|
|
|
Filename: "test.proto",
|
2018-11-26 22:32:06 -08:00
|
|
|
}, {
|
2019-06-06 13:01:53 -07:00
|
|
|
ExtendedType: (*LegacyTestMessage)(nil),
|
2018-11-26 22:32:06 -08:00
|
|
|
ExtensionType: ([]*EnumMessages)(nil),
|
|
|
|
Field: 10019,
|
|
|
|
Name: "fizz.buzz.repeated_message_v2",
|
|
|
|
Tag: "bytes,10019,rep,name=repeated_message_v2",
|
2019-06-06 13:01:53 -07:00
|
|
|
Filename: "test.proto",
|
2018-11-26 22:32:06 -08:00
|
|
|
}}
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestLegacyExtensions(t *testing.T) {
|
internal/impl: support legacy extension fields
Implement support for extension fields for messages that use the v1
data structures for extensions. The legacyExtensionFields type wraps a
v1 map to implement the v2 protoreflect.KnownFields interface.
Working on this change revealed a bug in the dynamic construction of
message types for protobuf messages that had cyclic dependencies (e.g.,
message Foo has a sub-field of message Bar, and Bar has a sub-field of Foo).
In such a situation, a deadlock occurs because initialization code depends on
the very initialization code that is currently running. To break these cycles,
we make some systematic changes listed in the following paragraphs.
Generally speaking, we separate the logic for construction and wrapping,
where constuction does not recursively rely on dependencies,
while wrapping may recursively inspect dependencies.
Promote the MessageType.MessageOf method as a standalone MessageOf function
that dynamically finds the proper *MessageType to use. We make it such that
MessageType only supports two forms of messages types:
* Those that fully implement the v2 API.
* Those that do not implement the v2 API at all.
This removes support for the hybrid form that was exploited by message_test.go
In impl/message_test.go, switch each message to look more like how future
generated messages will look like. This is done in reaction to the fact that
MessageType.MessageOf no longer exists.
In value/{map,vector}.go, fix Unwrap to return a pointer since the underlying
reflect.Value is addressable reference value, not a pointer value.
In value/convert.go, split the logic apart so that obtaining a v2 type and
wrapping a type as v2 are distinct operations. Wrapping requires further
initialization than simply creating the initial message type, and calling it
during initial construction would lead to a deadlock.
In protoreflect/go_type.go, we switch back to a lazy initialization of GoType
to avoid a deadlock since the user-provided fn may rely on the fact that
prototype.GoMessage returned.
Change-Id: I5dea00e36fe1a9899bd2ac0aed2c8e51d5d87420
Reviewed-on: https://go-review.googlesource.com/c/148826
Reviewed-by: Herbie Ong <herbie@google.com>
2018-11-06 13:05:20 -08:00
|
|
|
opts := cmp.Options{cmp.Comparer(func(x, y *proto2_20180125.Message_ChildMessage) bool {
|
|
|
|
return x == y // pointer compare messages for object identity
|
|
|
|
})}
|
|
|
|
|
2019-06-06 13:01:53 -07:00
|
|
|
m := pimpl.Export{}.MessageOf(new(LegacyTestMessage))
|
internal/impl: support legacy extension fields
Implement support for extension fields for messages that use the v1
data structures for extensions. The legacyExtensionFields type wraps a
v1 map to implement the v2 protoreflect.KnownFields interface.
Working on this change revealed a bug in the dynamic construction of
message types for protobuf messages that had cyclic dependencies (e.g.,
message Foo has a sub-field of message Bar, and Bar has a sub-field of Foo).
In such a situation, a deadlock occurs because initialization code depends on
the very initialization code that is currently running. To break these cycles,
we make some systematic changes listed in the following paragraphs.
Generally speaking, we separate the logic for construction and wrapping,
where constuction does not recursively rely on dependencies,
while wrapping may recursively inspect dependencies.
Promote the MessageType.MessageOf method as a standalone MessageOf function
that dynamically finds the proper *MessageType to use. We make it such that
MessageType only supports two forms of messages types:
* Those that fully implement the v2 API.
* Those that do not implement the v2 API at all.
This removes support for the hybrid form that was exploited by message_test.go
In impl/message_test.go, switch each message to look more like how future
generated messages will look like. This is done in reaction to the fact that
MessageType.MessageOf no longer exists.
In value/{map,vector}.go, fix Unwrap to return a pointer since the underlying
reflect.Value is addressable reference value, not a pointer value.
In value/convert.go, split the logic apart so that obtaining a v2 type and
wrapping a type as v2 are distinct operations. Wrapping requires further
initialization than simply creating the initial message type, and calling it
during initial construction would lead to a deadlock.
In protoreflect/go_type.go, we switch back to a lazy initialization of GoType
to avoid a deadlock since the user-provided fn may rely on the fact that
prototype.GoMessage returned.
Change-Id: I5dea00e36fe1a9899bd2ac0aed2c8e51d5d87420
Reviewed-on: https://go-review.googlesource.com/c/148826
Reviewed-by: Herbie Ong <herbie@google.com>
2018-11-06 13:05:20 -08:00
|
|
|
|
|
|
|
// Check that getting the zero value returns the default value for scalars,
|
all: rename Vector as List
The terminology Vector does not occur in protobuf documentation at all,
so we should rename the Go use of the term to something more recognizable.
As such, all instances that match the regexp "[Vv]ect(or)?" were replaced.
The C++ documentation uses the term "Repeated", which is a reasonable name.
However, the term became overloaded in 2014, when maps were added as a feature
and implementated under the hood as repeated fields. This is confusing as it
means "repeated" could either refer to repeated fields proper (i.e., explicitly
marked with the "repeated" label in the proto file) or map fields. In the case
of the C++ reflective API, this is not a problem since repeated fields proper
and map fields are interacted with through the same RepeatedField type.
In Go, we do not use a single type to handle both types of repeated fields:
1) We are coming up with the Go protobuf reflection API for the first time
and so do not need to piggy-back on the repeated fields API to remain backwards
compatible since no former usages of Go protobuf reflection exists.
2) Map fields are commonly represented in Go as the Go map type, which do not
preserve ordering information. As such it is fundamentally impossible to present
an unordered map as a consistently ordered list. Thus, Go needs two different
interfaces for lists and maps.
Given the above situation, "Repeated" is not a great term to use since it
refers to two different things (when we only want one of the meanings).
To distinguish between the two, we'll use the terms "List" and "Map" instead.
There is some precedence for the term "List" in the protobuf codebase
(e.g., "getRepeatedInt32List").
Change-Id: Iddcdb6b78e1e60c14fa4ca213c15f45e214b967b
Reviewed-on: https://go-review.googlesource.com/c/149657
Reviewed-by: Damien Neil <dneil@google.com>
2018-11-14 14:05:19 -08:00
|
|
|
// nil for singular messages, and an empty list for repeated fields.
|
2019-04-25 23:48:08 -07:00
|
|
|
defaultValues := map[int]interface{}{
|
|
|
|
0: bool(true),
|
|
|
|
1: int32(-12345),
|
|
|
|
2: uint32(3200),
|
|
|
|
3: float32(3.14159),
|
|
|
|
4: string("hello, \"world!\"\n"),
|
|
|
|
5: []byte("dead\xde\xad\xbe\xefbeef"),
|
|
|
|
6: proto2_20180125.Message_ALPHA,
|
|
|
|
7: nil,
|
|
|
|
8: EnumProto2(0xdead),
|
|
|
|
9: nil,
|
internal/impl: support legacy extension fields
Implement support for extension fields for messages that use the v1
data structures for extensions. The legacyExtensionFields type wraps a
v1 map to implement the v2 protoreflect.KnownFields interface.
Working on this change revealed a bug in the dynamic construction of
message types for protobuf messages that had cyclic dependencies (e.g.,
message Foo has a sub-field of message Bar, and Bar has a sub-field of Foo).
In such a situation, a deadlock occurs because initialization code depends on
the very initialization code that is currently running. To break these cycles,
we make some systematic changes listed in the following paragraphs.
Generally speaking, we separate the logic for construction and wrapping,
where constuction does not recursively rely on dependencies,
while wrapping may recursively inspect dependencies.
Promote the MessageType.MessageOf method as a standalone MessageOf function
that dynamically finds the proper *MessageType to use. We make it such that
MessageType only supports two forms of messages types:
* Those that fully implement the v2 API.
* Those that do not implement the v2 API at all.
This removes support for the hybrid form that was exploited by message_test.go
In impl/message_test.go, switch each message to look more like how future
generated messages will look like. This is done in reaction to the fact that
MessageType.MessageOf no longer exists.
In value/{map,vector}.go, fix Unwrap to return a pointer since the underlying
reflect.Value is addressable reference value, not a pointer value.
In value/convert.go, split the logic apart so that obtaining a v2 type and
wrapping a type as v2 are distinct operations. Wrapping requires further
initialization than simply creating the initial message type, and calling it
during initial construction would lead to a deadlock.
In protoreflect/go_type.go, we switch back to a lazy initialization of GoType
to avoid a deadlock since the user-provided fn may rely on the fact that
prototype.GoMessage returned.
Change-Id: I5dea00e36fe1a9899bd2ac0aed2c8e51d5d87420
Reviewed-on: https://go-review.googlesource.com/c/148826
Reviewed-by: Herbie Ong <herbie@google.com>
2018-11-06 13:05:20 -08:00
|
|
|
}
|
2018-11-26 22:32:06 -08:00
|
|
|
for i, xt := range extensionTypes {
|
internal/impl: support legacy extension fields
Implement support for extension fields for messages that use the v1
data structures for extensions. The legacyExtensionFields type wraps a
v1 map to implement the v2 protoreflect.KnownFields interface.
Working on this change revealed a bug in the dynamic construction of
message types for protobuf messages that had cyclic dependencies (e.g.,
message Foo has a sub-field of message Bar, and Bar has a sub-field of Foo).
In such a situation, a deadlock occurs because initialization code depends on
the very initialization code that is currently running. To break these cycles,
we make some systematic changes listed in the following paragraphs.
Generally speaking, we separate the logic for construction and wrapping,
where constuction does not recursively rely on dependencies,
while wrapping may recursively inspect dependencies.
Promote the MessageType.MessageOf method as a standalone MessageOf function
that dynamically finds the proper *MessageType to use. We make it such that
MessageType only supports two forms of messages types:
* Those that fully implement the v2 API.
* Those that do not implement the v2 API at all.
This removes support for the hybrid form that was exploited by message_test.go
In impl/message_test.go, switch each message to look more like how future
generated messages will look like. This is done in reaction to the fact that
MessageType.MessageOf no longer exists.
In value/{map,vector}.go, fix Unwrap to return a pointer since the underlying
reflect.Value is addressable reference value, not a pointer value.
In value/convert.go, split the logic apart so that obtaining a v2 type and
wrapping a type as v2 are distinct operations. Wrapping requires further
initialization than simply creating the initial message type, and calling it
during initial construction would lead to a deadlock.
In protoreflect/go_type.go, we switch back to a lazy initialization of GoType
to avoid a deadlock since the user-provided fn may rely on the fact that
prototype.GoMessage returned.
Change-Id: I5dea00e36fe1a9899bd2ac0aed2c8e51d5d87420
Reviewed-on: https://go-review.googlesource.com/c/148826
Reviewed-by: Herbie Ong <herbie@google.com>
2018-11-06 13:05:20 -08:00
|
|
|
var got interface{}
|
2019-08-28 11:08:22 -07:00
|
|
|
xd := xt.TypeDescriptor()
|
2019-08-02 16:58:08 -07:00
|
|
|
if !(xd.IsList() || xd.IsMap() || xd.Message() != nil) {
|
|
|
|
got = xt.InterfaceOf(m.Get(xd))
|
internal/impl: support legacy extension fields
Implement support for extension fields for messages that use the v1
data structures for extensions. The legacyExtensionFields type wraps a
v1 map to implement the v2 protoreflect.KnownFields interface.
Working on this change revealed a bug in the dynamic construction of
message types for protobuf messages that had cyclic dependencies (e.g.,
message Foo has a sub-field of message Bar, and Bar has a sub-field of Foo).
In such a situation, a deadlock occurs because initialization code depends on
the very initialization code that is currently running. To break these cycles,
we make some systematic changes listed in the following paragraphs.
Generally speaking, we separate the logic for construction and wrapping,
where constuction does not recursively rely on dependencies,
while wrapping may recursively inspect dependencies.
Promote the MessageType.MessageOf method as a standalone MessageOf function
that dynamically finds the proper *MessageType to use. We make it such that
MessageType only supports two forms of messages types:
* Those that fully implement the v2 API.
* Those that do not implement the v2 API at all.
This removes support for the hybrid form that was exploited by message_test.go
In impl/message_test.go, switch each message to look more like how future
generated messages will look like. This is done in reaction to the fact that
MessageType.MessageOf no longer exists.
In value/{map,vector}.go, fix Unwrap to return a pointer since the underlying
reflect.Value is addressable reference value, not a pointer value.
In value/convert.go, split the logic apart so that obtaining a v2 type and
wrapping a type as v2 are distinct operations. Wrapping requires further
initialization than simply creating the initial message type, and calling it
during initial construction would lead to a deadlock.
In protoreflect/go_type.go, we switch back to a lazy initialization of GoType
to avoid a deadlock since the user-provided fn may rely on the fact that
prototype.GoMessage returned.
Change-Id: I5dea00e36fe1a9899bd2ac0aed2c8e51d5d87420
Reviewed-on: https://go-review.googlesource.com/c/148826
Reviewed-by: Herbie Ong <herbie@google.com>
2018-11-06 13:05:20 -08:00
|
|
|
}
|
|
|
|
want := defaultValues[i]
|
|
|
|
if diff := cmp.Diff(want, got, opts); diff != "" {
|
2019-08-02 16:58:08 -07:00
|
|
|
t.Errorf("Message.Get(%d) mismatch (-want +got):\n%v", xd.Number(), diff)
|
internal/impl: support legacy extension fields
Implement support for extension fields for messages that use the v1
data structures for extensions. The legacyExtensionFields type wraps a
v1 map to implement the v2 protoreflect.KnownFields interface.
Working on this change revealed a bug in the dynamic construction of
message types for protobuf messages that had cyclic dependencies (e.g.,
message Foo has a sub-field of message Bar, and Bar has a sub-field of Foo).
In such a situation, a deadlock occurs because initialization code depends on
the very initialization code that is currently running. To break these cycles,
we make some systematic changes listed in the following paragraphs.
Generally speaking, we separate the logic for construction and wrapping,
where constuction does not recursively rely on dependencies,
while wrapping may recursively inspect dependencies.
Promote the MessageType.MessageOf method as a standalone MessageOf function
that dynamically finds the proper *MessageType to use. We make it such that
MessageType only supports two forms of messages types:
* Those that fully implement the v2 API.
* Those that do not implement the v2 API at all.
This removes support for the hybrid form that was exploited by message_test.go
In impl/message_test.go, switch each message to look more like how future
generated messages will look like. This is done in reaction to the fact that
MessageType.MessageOf no longer exists.
In value/{map,vector}.go, fix Unwrap to return a pointer since the underlying
reflect.Value is addressable reference value, not a pointer value.
In value/convert.go, split the logic apart so that obtaining a v2 type and
wrapping a type as v2 are distinct operations. Wrapping requires further
initialization than simply creating the initial message type, and calling it
during initial construction would lead to a deadlock.
In protoreflect/go_type.go, we switch back to a lazy initialization of GoType
to avoid a deadlock since the user-provided fn may rely on the fact that
prototype.GoMessage returned.
Change-Id: I5dea00e36fe1a9899bd2ac0aed2c8e51d5d87420
Reviewed-on: https://go-review.googlesource.com/c/148826
Reviewed-by: Herbie Ong <herbie@google.com>
2018-11-06 13:05:20 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// All fields should be unpopulated.
|
2018-11-26 22:32:06 -08:00
|
|
|
for _, xt := range extensionTypes {
|
2019-08-28 11:08:22 -07:00
|
|
|
xd := xt.TypeDescriptor()
|
2019-08-02 16:58:08 -07:00
|
|
|
if m.Has(xd) {
|
|
|
|
t.Errorf("Message.Has(%d) = true, want false", xd.Number())
|
internal/impl: support legacy extension fields
Implement support for extension fields for messages that use the v1
data structures for extensions. The legacyExtensionFields type wraps a
v1 map to implement the v2 protoreflect.KnownFields interface.
Working on this change revealed a bug in the dynamic construction of
message types for protobuf messages that had cyclic dependencies (e.g.,
message Foo has a sub-field of message Bar, and Bar has a sub-field of Foo).
In such a situation, a deadlock occurs because initialization code depends on
the very initialization code that is currently running. To break these cycles,
we make some systematic changes listed in the following paragraphs.
Generally speaking, we separate the logic for construction and wrapping,
where constuction does not recursively rely on dependencies,
while wrapping may recursively inspect dependencies.
Promote the MessageType.MessageOf method as a standalone MessageOf function
that dynamically finds the proper *MessageType to use. We make it such that
MessageType only supports two forms of messages types:
* Those that fully implement the v2 API.
* Those that do not implement the v2 API at all.
This removes support for the hybrid form that was exploited by message_test.go
In impl/message_test.go, switch each message to look more like how future
generated messages will look like. This is done in reaction to the fact that
MessageType.MessageOf no longer exists.
In value/{map,vector}.go, fix Unwrap to return a pointer since the underlying
reflect.Value is addressable reference value, not a pointer value.
In value/convert.go, split the logic apart so that obtaining a v2 type and
wrapping a type as v2 are distinct operations. Wrapping requires further
initialization than simply creating the initial message type, and calling it
during initial construction would lead to a deadlock.
In protoreflect/go_type.go, we switch back to a lazy initialization of GoType
to avoid a deadlock since the user-provided fn may rely on the fact that
prototype.GoMessage returned.
Change-Id: I5dea00e36fe1a9899bd2ac0aed2c8e51d5d87420
Reviewed-on: https://go-review.googlesource.com/c/148826
Reviewed-by: Herbie Ong <herbie@google.com>
2018-11-06 13:05:20 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
all: rename Vector as List
The terminology Vector does not occur in protobuf documentation at all,
so we should rename the Go use of the term to something more recognizable.
As such, all instances that match the regexp "[Vv]ect(or)?" were replaced.
The C++ documentation uses the term "Repeated", which is a reasonable name.
However, the term became overloaded in 2014, when maps were added as a feature
and implementated under the hood as repeated fields. This is confusing as it
means "repeated" could either refer to repeated fields proper (i.e., explicitly
marked with the "repeated" label in the proto file) or map fields. In the case
of the C++ reflective API, this is not a problem since repeated fields proper
and map fields are interacted with through the same RepeatedField type.
In Go, we do not use a single type to handle both types of repeated fields:
1) We are coming up with the Go protobuf reflection API for the first time
and so do not need to piggy-back on the repeated fields API to remain backwards
compatible since no former usages of Go protobuf reflection exists.
2) Map fields are commonly represented in Go as the Go map type, which do not
preserve ordering information. As such it is fundamentally impossible to present
an unordered map as a consistently ordered list. Thus, Go needs two different
interfaces for lists and maps.
Given the above situation, "Repeated" is not a great term to use since it
refers to two different things (when we only want one of the meanings).
To distinguish between the two, we'll use the terms "List" and "Map" instead.
There is some precedence for the term "List" in the protobuf codebase
(e.g., "getRepeatedInt32List").
Change-Id: Iddcdb6b78e1e60c14fa4ca213c15f45e214b967b
Reviewed-on: https://go-review.googlesource.com/c/149657
Reviewed-by: Damien Neil <dneil@google.com>
2018-11-14 14:05:19 -08:00
|
|
|
// Set some values and append to values to the lists.
|
2019-07-10 16:17:16 -07:00
|
|
|
m1a := &proto2_20180125.Message_ChildMessage{F1: proto.String("m1a")}
|
|
|
|
m1b := &proto2_20180125.Message_ChildMessage{F1: proto.String("m2b")}
|
2018-11-14 21:59:49 -08:00
|
|
|
m2a := &EnumMessages{EnumP2: EnumProto2(0x1b).Enum()}
|
|
|
|
m2b := &EnumMessages{EnumP2: EnumProto2(0x2b).Enum()}
|
2019-04-25 23:48:08 -07:00
|
|
|
setValues := map[int]interface{}{
|
|
|
|
0: bool(false),
|
|
|
|
1: int32(-54321),
|
|
|
|
2: uint32(6400),
|
|
|
|
3: float32(2.71828),
|
|
|
|
4: string("goodbye, \"world!\"\n"),
|
|
|
|
5: []byte("live\xde\xad\xbe\xefchicken"),
|
|
|
|
6: proto2_20180125.Message_CHARLIE,
|
|
|
|
7: m1a,
|
|
|
|
8: EnumProto2(0xbeef),
|
|
|
|
9: m2a,
|
2019-08-29 11:42:57 -07:00
|
|
|
10: []bool{true},
|
|
|
|
11: []int32{-1000},
|
|
|
|
12: []uint32{1280},
|
|
|
|
13: []float32{1.6180},
|
|
|
|
14: []string{"zero"},
|
|
|
|
15: [][]byte{[]byte("zero")},
|
|
|
|
16: []proto2_20180125.Message_ChildEnum{proto2_20180125.Message_BRAVO},
|
|
|
|
17: []*proto2_20180125.Message_ChildMessage{m1b},
|
|
|
|
18: []EnumProto2{0xdead},
|
|
|
|
19: []*EnumMessages{m2b},
|
internal/impl: support legacy extension fields
Implement support for extension fields for messages that use the v1
data structures for extensions. The legacyExtensionFields type wraps a
v1 map to implement the v2 protoreflect.KnownFields interface.
Working on this change revealed a bug in the dynamic construction of
message types for protobuf messages that had cyclic dependencies (e.g.,
message Foo has a sub-field of message Bar, and Bar has a sub-field of Foo).
In such a situation, a deadlock occurs because initialization code depends on
the very initialization code that is currently running. To break these cycles,
we make some systematic changes listed in the following paragraphs.
Generally speaking, we separate the logic for construction and wrapping,
where constuction does not recursively rely on dependencies,
while wrapping may recursively inspect dependencies.
Promote the MessageType.MessageOf method as a standalone MessageOf function
that dynamically finds the proper *MessageType to use. We make it such that
MessageType only supports two forms of messages types:
* Those that fully implement the v2 API.
* Those that do not implement the v2 API at all.
This removes support for the hybrid form that was exploited by message_test.go
In impl/message_test.go, switch each message to look more like how future
generated messages will look like. This is done in reaction to the fact that
MessageType.MessageOf no longer exists.
In value/{map,vector}.go, fix Unwrap to return a pointer since the underlying
reflect.Value is addressable reference value, not a pointer value.
In value/convert.go, split the logic apart so that obtaining a v2 type and
wrapping a type as v2 are distinct operations. Wrapping requires further
initialization than simply creating the initial message type, and calling it
during initial construction would lead to a deadlock.
In protoreflect/go_type.go, we switch back to a lazy initialization of GoType
to avoid a deadlock since the user-provided fn may rely on the fact that
prototype.GoMessage returned.
Change-Id: I5dea00e36fe1a9899bd2ac0aed2c8e51d5d87420
Reviewed-on: https://go-review.googlesource.com/c/148826
Reviewed-by: Herbie Ong <herbie@google.com>
2018-11-06 13:05:20 -08:00
|
|
|
}
|
2018-11-26 22:32:06 -08:00
|
|
|
for i, xt := range extensionTypes {
|
2019-08-28 11:08:22 -07:00
|
|
|
m.Set(xt.TypeDescriptor(), xt.ValueOf(setValues[i]))
|
internal/impl: support legacy extension fields
Implement support for extension fields for messages that use the v1
data structures for extensions. The legacyExtensionFields type wraps a
v1 map to implement the v2 protoreflect.KnownFields interface.
Working on this change revealed a bug in the dynamic construction of
message types for protobuf messages that had cyclic dependencies (e.g.,
message Foo has a sub-field of message Bar, and Bar has a sub-field of Foo).
In such a situation, a deadlock occurs because initialization code depends on
the very initialization code that is currently running. To break these cycles,
we make some systematic changes listed in the following paragraphs.
Generally speaking, we separate the logic for construction and wrapping,
where constuction does not recursively rely on dependencies,
while wrapping may recursively inspect dependencies.
Promote the MessageType.MessageOf method as a standalone MessageOf function
that dynamically finds the proper *MessageType to use. We make it such that
MessageType only supports two forms of messages types:
* Those that fully implement the v2 API.
* Those that do not implement the v2 API at all.
This removes support for the hybrid form that was exploited by message_test.go
In impl/message_test.go, switch each message to look more like how future
generated messages will look like. This is done in reaction to the fact that
MessageType.MessageOf no longer exists.
In value/{map,vector}.go, fix Unwrap to return a pointer since the underlying
reflect.Value is addressable reference value, not a pointer value.
In value/convert.go, split the logic apart so that obtaining a v2 type and
wrapping a type as v2 are distinct operations. Wrapping requires further
initialization than simply creating the initial message type, and calling it
during initial construction would lead to a deadlock.
In protoreflect/go_type.go, we switch back to a lazy initialization of GoType
to avoid a deadlock since the user-provided fn may rely on the fact that
prototype.GoMessage returned.
Change-Id: I5dea00e36fe1a9899bd2ac0aed2c8e51d5d87420
Reviewed-on: https://go-review.googlesource.com/c/148826
Reviewed-by: Herbie Ong <herbie@google.com>
2018-11-06 13:05:20 -08:00
|
|
|
}
|
2018-11-26 22:32:06 -08:00
|
|
|
for i, xt := range extensionTypes[len(extensionTypes)/2:] {
|
|
|
|
v := extensionTypes[i].ValueOf(setValues[i])
|
2019-08-28 11:08:22 -07:00
|
|
|
m.Get(xt.TypeDescriptor()).List().Append(v)
|
internal/impl: support legacy extension fields
Implement support for extension fields for messages that use the v1
data structures for extensions. The legacyExtensionFields type wraps a
v1 map to implement the v2 protoreflect.KnownFields interface.
Working on this change revealed a bug in the dynamic construction of
message types for protobuf messages that had cyclic dependencies (e.g.,
message Foo has a sub-field of message Bar, and Bar has a sub-field of Foo).
In such a situation, a deadlock occurs because initialization code depends on
the very initialization code that is currently running. To break these cycles,
we make some systematic changes listed in the following paragraphs.
Generally speaking, we separate the logic for construction and wrapping,
where constuction does not recursively rely on dependencies,
while wrapping may recursively inspect dependencies.
Promote the MessageType.MessageOf method as a standalone MessageOf function
that dynamically finds the proper *MessageType to use. We make it such that
MessageType only supports two forms of messages types:
* Those that fully implement the v2 API.
* Those that do not implement the v2 API at all.
This removes support for the hybrid form that was exploited by message_test.go
In impl/message_test.go, switch each message to look more like how future
generated messages will look like. This is done in reaction to the fact that
MessageType.MessageOf no longer exists.
In value/{map,vector}.go, fix Unwrap to return a pointer since the underlying
reflect.Value is addressable reference value, not a pointer value.
In value/convert.go, split the logic apart so that obtaining a v2 type and
wrapping a type as v2 are distinct operations. Wrapping requires further
initialization than simply creating the initial message type, and calling it
during initial construction would lead to a deadlock.
In protoreflect/go_type.go, we switch back to a lazy initialization of GoType
to avoid a deadlock since the user-provided fn may rely on the fact that
prototype.GoMessage returned.
Change-Id: I5dea00e36fe1a9899bd2ac0aed2c8e51d5d87420
Reviewed-on: https://go-review.googlesource.com/c/148826
Reviewed-by: Herbie Ong <herbie@google.com>
2018-11-06 13:05:20 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the values and check for equality.
|
2019-04-25 23:48:08 -07:00
|
|
|
getValues := map[int]interface{}{
|
|
|
|
0: bool(false),
|
|
|
|
1: int32(-54321),
|
|
|
|
2: uint32(6400),
|
|
|
|
3: float32(2.71828),
|
|
|
|
4: string("goodbye, \"world!\"\n"),
|
|
|
|
5: []byte("live\xde\xad\xbe\xefchicken"),
|
|
|
|
6: proto2_20180125.Message_ChildEnum(proto2_20180125.Message_CHARLIE),
|
|
|
|
7: m1a,
|
|
|
|
8: EnumProto2(0xbeef),
|
|
|
|
9: m2a,
|
2019-08-29 11:42:57 -07:00
|
|
|
10: []bool{true, false},
|
|
|
|
11: []int32{-1000, -54321},
|
|
|
|
12: []uint32{1280, 6400},
|
|
|
|
13: []float32{1.6180, 2.71828},
|
|
|
|
14: []string{"zero", "goodbye, \"world!\"\n"},
|
|
|
|
15: [][]byte{[]byte("zero"), []byte("live\xde\xad\xbe\xefchicken")},
|
|
|
|
16: []proto2_20180125.Message_ChildEnum{proto2_20180125.Message_BRAVO, proto2_20180125.Message_CHARLIE},
|
|
|
|
17: []*proto2_20180125.Message_ChildMessage{m1b, m1a},
|
|
|
|
18: []EnumProto2{0xdead, 0xbeef},
|
|
|
|
19: []*EnumMessages{m2b, m2a},
|
internal/impl: support legacy extension fields
Implement support for extension fields for messages that use the v1
data structures for extensions. The legacyExtensionFields type wraps a
v1 map to implement the v2 protoreflect.KnownFields interface.
Working on this change revealed a bug in the dynamic construction of
message types for protobuf messages that had cyclic dependencies (e.g.,
message Foo has a sub-field of message Bar, and Bar has a sub-field of Foo).
In such a situation, a deadlock occurs because initialization code depends on
the very initialization code that is currently running. To break these cycles,
we make some systematic changes listed in the following paragraphs.
Generally speaking, we separate the logic for construction and wrapping,
where constuction does not recursively rely on dependencies,
while wrapping may recursively inspect dependencies.
Promote the MessageType.MessageOf method as a standalone MessageOf function
that dynamically finds the proper *MessageType to use. We make it such that
MessageType only supports two forms of messages types:
* Those that fully implement the v2 API.
* Those that do not implement the v2 API at all.
This removes support for the hybrid form that was exploited by message_test.go
In impl/message_test.go, switch each message to look more like how future
generated messages will look like. This is done in reaction to the fact that
MessageType.MessageOf no longer exists.
In value/{map,vector}.go, fix Unwrap to return a pointer since the underlying
reflect.Value is addressable reference value, not a pointer value.
In value/convert.go, split the logic apart so that obtaining a v2 type and
wrapping a type as v2 are distinct operations. Wrapping requires further
initialization than simply creating the initial message type, and calling it
during initial construction would lead to a deadlock.
In protoreflect/go_type.go, we switch back to a lazy initialization of GoType
to avoid a deadlock since the user-provided fn may rely on the fact that
prototype.GoMessage returned.
Change-Id: I5dea00e36fe1a9899bd2ac0aed2c8e51d5d87420
Reviewed-on: https://go-review.googlesource.com/c/148826
Reviewed-by: Herbie Ong <herbie@google.com>
2018-11-06 13:05:20 -08:00
|
|
|
}
|
2018-11-26 22:32:06 -08:00
|
|
|
for i, xt := range extensionTypes {
|
2019-08-28 11:08:22 -07:00
|
|
|
xd := xt.TypeDescriptor()
|
2019-08-02 16:58:08 -07:00
|
|
|
got := xt.InterfaceOf(m.Get(xd))
|
internal/impl: support legacy extension fields
Implement support for extension fields for messages that use the v1
data structures for extensions. The legacyExtensionFields type wraps a
v1 map to implement the v2 protoreflect.KnownFields interface.
Working on this change revealed a bug in the dynamic construction of
message types for protobuf messages that had cyclic dependencies (e.g.,
message Foo has a sub-field of message Bar, and Bar has a sub-field of Foo).
In such a situation, a deadlock occurs because initialization code depends on
the very initialization code that is currently running. To break these cycles,
we make some systematic changes listed in the following paragraphs.
Generally speaking, we separate the logic for construction and wrapping,
where constuction does not recursively rely on dependencies,
while wrapping may recursively inspect dependencies.
Promote the MessageType.MessageOf method as a standalone MessageOf function
that dynamically finds the proper *MessageType to use. We make it such that
MessageType only supports two forms of messages types:
* Those that fully implement the v2 API.
* Those that do not implement the v2 API at all.
This removes support for the hybrid form that was exploited by message_test.go
In impl/message_test.go, switch each message to look more like how future
generated messages will look like. This is done in reaction to the fact that
MessageType.MessageOf no longer exists.
In value/{map,vector}.go, fix Unwrap to return a pointer since the underlying
reflect.Value is addressable reference value, not a pointer value.
In value/convert.go, split the logic apart so that obtaining a v2 type and
wrapping a type as v2 are distinct operations. Wrapping requires further
initialization than simply creating the initial message type, and calling it
during initial construction would lead to a deadlock.
In protoreflect/go_type.go, we switch back to a lazy initialization of GoType
to avoid a deadlock since the user-provided fn may rely on the fact that
prototype.GoMessage returned.
Change-Id: I5dea00e36fe1a9899bd2ac0aed2c8e51d5d87420
Reviewed-on: https://go-review.googlesource.com/c/148826
Reviewed-by: Herbie Ong <herbie@google.com>
2018-11-06 13:05:20 -08:00
|
|
|
want := getValues[i]
|
|
|
|
if diff := cmp.Diff(want, got, opts); diff != "" {
|
2019-08-02 16:58:08 -07:00
|
|
|
t.Errorf("Message.Get(%d) mismatch (-want +got):\n%v", xd.Number(), diff)
|
internal/impl: support legacy extension fields
Implement support for extension fields for messages that use the v1
data structures for extensions. The legacyExtensionFields type wraps a
v1 map to implement the v2 protoreflect.KnownFields interface.
Working on this change revealed a bug in the dynamic construction of
message types for protobuf messages that had cyclic dependencies (e.g.,
message Foo has a sub-field of message Bar, and Bar has a sub-field of Foo).
In such a situation, a deadlock occurs because initialization code depends on
the very initialization code that is currently running. To break these cycles,
we make some systematic changes listed in the following paragraphs.
Generally speaking, we separate the logic for construction and wrapping,
where constuction does not recursively rely on dependencies,
while wrapping may recursively inspect dependencies.
Promote the MessageType.MessageOf method as a standalone MessageOf function
that dynamically finds the proper *MessageType to use. We make it such that
MessageType only supports two forms of messages types:
* Those that fully implement the v2 API.
* Those that do not implement the v2 API at all.
This removes support for the hybrid form that was exploited by message_test.go
In impl/message_test.go, switch each message to look more like how future
generated messages will look like. This is done in reaction to the fact that
MessageType.MessageOf no longer exists.
In value/{map,vector}.go, fix Unwrap to return a pointer since the underlying
reflect.Value is addressable reference value, not a pointer value.
In value/convert.go, split the logic apart so that obtaining a v2 type and
wrapping a type as v2 are distinct operations. Wrapping requires further
initialization than simply creating the initial message type, and calling it
during initial construction would lead to a deadlock.
In protoreflect/go_type.go, we switch back to a lazy initialization of GoType
to avoid a deadlock since the user-provided fn may rely on the fact that
prototype.GoMessage returned.
Change-Id: I5dea00e36fe1a9899bd2ac0aed2c8e51d5d87420
Reviewed-on: https://go-review.googlesource.com/c/148826
Reviewed-by: Herbie Ong <herbie@google.com>
2018-11-06 13:05:20 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-25 23:48:08 -07:00
|
|
|
// Clear all singular fields and truncate all repeated fields.
|
2018-11-26 22:32:06 -08:00
|
|
|
for _, xt := range extensionTypes[:len(extensionTypes)/2] {
|
2019-08-28 11:08:22 -07:00
|
|
|
m.Clear(xt.TypeDescriptor())
|
internal/impl: support legacy extension fields
Implement support for extension fields for messages that use the v1
data structures for extensions. The legacyExtensionFields type wraps a
v1 map to implement the v2 protoreflect.KnownFields interface.
Working on this change revealed a bug in the dynamic construction of
message types for protobuf messages that had cyclic dependencies (e.g.,
message Foo has a sub-field of message Bar, and Bar has a sub-field of Foo).
In such a situation, a deadlock occurs because initialization code depends on
the very initialization code that is currently running. To break these cycles,
we make some systematic changes listed in the following paragraphs.
Generally speaking, we separate the logic for construction and wrapping,
where constuction does not recursively rely on dependencies,
while wrapping may recursively inspect dependencies.
Promote the MessageType.MessageOf method as a standalone MessageOf function
that dynamically finds the proper *MessageType to use. We make it such that
MessageType only supports two forms of messages types:
* Those that fully implement the v2 API.
* Those that do not implement the v2 API at all.
This removes support for the hybrid form that was exploited by message_test.go
In impl/message_test.go, switch each message to look more like how future
generated messages will look like. This is done in reaction to the fact that
MessageType.MessageOf no longer exists.
In value/{map,vector}.go, fix Unwrap to return a pointer since the underlying
reflect.Value is addressable reference value, not a pointer value.
In value/convert.go, split the logic apart so that obtaining a v2 type and
wrapping a type as v2 are distinct operations. Wrapping requires further
initialization than simply creating the initial message type, and calling it
during initial construction would lead to a deadlock.
In protoreflect/go_type.go, we switch back to a lazy initialization of GoType
to avoid a deadlock since the user-provided fn may rely on the fact that
prototype.GoMessage returned.
Change-Id: I5dea00e36fe1a9899bd2ac0aed2c8e51d5d87420
Reviewed-on: https://go-review.googlesource.com/c/148826
Reviewed-by: Herbie Ong <herbie@google.com>
2018-11-06 13:05:20 -08:00
|
|
|
}
|
2019-04-25 23:48:08 -07:00
|
|
|
for _, xt := range extensionTypes[len(extensionTypes)/2:] {
|
2019-08-28 11:08:22 -07:00
|
|
|
m.Get(xt.TypeDescriptor()).List().Truncate(0)
|
internal/impl: support legacy extension fields
Implement support for extension fields for messages that use the v1
data structures for extensions. The legacyExtensionFields type wraps a
v1 map to implement the v2 protoreflect.KnownFields interface.
Working on this change revealed a bug in the dynamic construction of
message types for protobuf messages that had cyclic dependencies (e.g.,
message Foo has a sub-field of message Bar, and Bar has a sub-field of Foo).
In such a situation, a deadlock occurs because initialization code depends on
the very initialization code that is currently running. To break these cycles,
we make some systematic changes listed in the following paragraphs.
Generally speaking, we separate the logic for construction and wrapping,
where constuction does not recursively rely on dependencies,
while wrapping may recursively inspect dependencies.
Promote the MessageType.MessageOf method as a standalone MessageOf function
that dynamically finds the proper *MessageType to use. We make it such that
MessageType only supports two forms of messages types:
* Those that fully implement the v2 API.
* Those that do not implement the v2 API at all.
This removes support for the hybrid form that was exploited by message_test.go
In impl/message_test.go, switch each message to look more like how future
generated messages will look like. This is done in reaction to the fact that
MessageType.MessageOf no longer exists.
In value/{map,vector}.go, fix Unwrap to return a pointer since the underlying
reflect.Value is addressable reference value, not a pointer value.
In value/convert.go, split the logic apart so that obtaining a v2 type and
wrapping a type as v2 are distinct operations. Wrapping requires further
initialization than simply creating the initial message type, and calling it
during initial construction would lead to a deadlock.
In protoreflect/go_type.go, we switch back to a lazy initialization of GoType
to avoid a deadlock since the user-provided fn may rely on the fact that
prototype.GoMessage returned.
Change-Id: I5dea00e36fe1a9899bd2ac0aed2c8e51d5d87420
Reviewed-on: https://go-review.googlesource.com/c/148826
Reviewed-by: Herbie Ong <herbie@google.com>
2018-11-06 13:05:20 -08:00
|
|
|
}
|
|
|
|
|
2019-04-25 23:48:08 -07:00
|
|
|
// Clear all repeated fields.
|
|
|
|
for _, xt := range extensionTypes[len(extensionTypes)/2:] {
|
2019-08-28 11:08:22 -07:00
|
|
|
m.Clear(xt.TypeDescriptor())
|
internal/impl: support legacy extension fields
Implement support for extension fields for messages that use the v1
data structures for extensions. The legacyExtensionFields type wraps a
v1 map to implement the v2 protoreflect.KnownFields interface.
Working on this change revealed a bug in the dynamic construction of
message types for protobuf messages that had cyclic dependencies (e.g.,
message Foo has a sub-field of message Bar, and Bar has a sub-field of Foo).
In such a situation, a deadlock occurs because initialization code depends on
the very initialization code that is currently running. To break these cycles,
we make some systematic changes listed in the following paragraphs.
Generally speaking, we separate the logic for construction and wrapping,
where constuction does not recursively rely on dependencies,
while wrapping may recursively inspect dependencies.
Promote the MessageType.MessageOf method as a standalone MessageOf function
that dynamically finds the proper *MessageType to use. We make it such that
MessageType only supports two forms of messages types:
* Those that fully implement the v2 API.
* Those that do not implement the v2 API at all.
This removes support for the hybrid form that was exploited by message_test.go
In impl/message_test.go, switch each message to look more like how future
generated messages will look like. This is done in reaction to the fact that
MessageType.MessageOf no longer exists.
In value/{map,vector}.go, fix Unwrap to return a pointer since the underlying
reflect.Value is addressable reference value, not a pointer value.
In value/convert.go, split the logic apart so that obtaining a v2 type and
wrapping a type as v2 are distinct operations. Wrapping requires further
initialization than simply creating the initial message type, and calling it
during initial construction would lead to a deadlock.
In protoreflect/go_type.go, we switch back to a lazy initialization of GoType
to avoid a deadlock since the user-provided fn may rely on the fact that
prototype.GoMessage returned.
Change-Id: I5dea00e36fe1a9899bd2ac0aed2c8e51d5d87420
Reviewed-on: https://go-review.googlesource.com/c/148826
Reviewed-by: Herbie Ong <herbie@google.com>
2018-11-06 13:05:20 -08:00
|
|
|
}
|
2018-11-26 22:32:06 -08:00
|
|
|
}
|
internal/impl: support legacy extension fields
Implement support for extension fields for messages that use the v1
data structures for extensions. The legacyExtensionFields type wraps a
v1 map to implement the v2 protoreflect.KnownFields interface.
Working on this change revealed a bug in the dynamic construction of
message types for protobuf messages that had cyclic dependencies (e.g.,
message Foo has a sub-field of message Bar, and Bar has a sub-field of Foo).
In such a situation, a deadlock occurs because initialization code depends on
the very initialization code that is currently running. To break these cycles,
we make some systematic changes listed in the following paragraphs.
Generally speaking, we separate the logic for construction and wrapping,
where constuction does not recursively rely on dependencies,
while wrapping may recursively inspect dependencies.
Promote the MessageType.MessageOf method as a standalone MessageOf function
that dynamically finds the proper *MessageType to use. We make it such that
MessageType only supports two forms of messages types:
* Those that fully implement the v2 API.
* Those that do not implement the v2 API at all.
This removes support for the hybrid form that was exploited by message_test.go
In impl/message_test.go, switch each message to look more like how future
generated messages will look like. This is done in reaction to the fact that
MessageType.MessageOf no longer exists.
In value/{map,vector}.go, fix Unwrap to return a pointer since the underlying
reflect.Value is addressable reference value, not a pointer value.
In value/convert.go, split the logic apart so that obtaining a v2 type and
wrapping a type as v2 are distinct operations. Wrapping requires further
initialization than simply creating the initial message type, and calling it
during initial construction would lead to a deadlock.
In protoreflect/go_type.go, we switch back to a lazy initialization of GoType
to avoid a deadlock since the user-provided fn may rely on the fact that
prototype.GoMessage returned.
Change-Id: I5dea00e36fe1a9899bd2ac0aed2c8e51d5d87420
Reviewed-on: https://go-review.googlesource.com/c/148826
Reviewed-by: Herbie Ong <herbie@google.com>
2018-11-06 13:05:20 -08:00
|
|
|
|
2019-09-16 13:30:15 -07:00
|
|
|
func TestLegacyExtensionConvert(t *testing.T) {
|
2018-11-26 22:32:06 -08:00
|
|
|
for i := range extensionTypes {
|
|
|
|
i := i
|
|
|
|
t.Run("", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
wantType := extensionTypes[i]
|
|
|
|
wantDesc := extensionDescs[i]
|
2019-08-08 15:45:59 -07:00
|
|
|
gotType := (pref.ExtensionType)(wantDesc)
|
2019-09-16 13:30:15 -07:00
|
|
|
gotDesc := wantType.(*pimpl.ExtensionInfo)
|
|
|
|
|
|
|
|
// Concurrently call accessors to trigger possible races.
|
|
|
|
for _, xt := range []pref.ExtensionType{wantType, wantDesc} {
|
|
|
|
xt := xt
|
|
|
|
go func() { xt.New() }()
|
|
|
|
go func() { xt.Zero() }()
|
|
|
|
go func() { xt.TypeDescriptor() }()
|
|
|
|
}
|
2018-11-26 22:32:06 -08:00
|
|
|
|
|
|
|
// TODO: We need a test package to compare descriptors.
|
|
|
|
type list interface {
|
|
|
|
Len() int
|
|
|
|
pragma.DoNotImplement
|
|
|
|
}
|
|
|
|
opts := cmp.Options{
|
|
|
|
cmp.Comparer(func(x, y reflect.Type) bool {
|
|
|
|
return x == y
|
|
|
|
}),
|
|
|
|
cmp.Transformer("", func(x list) []interface{} {
|
|
|
|
out := make([]interface{}, x.Len())
|
|
|
|
v := reflect.ValueOf(x)
|
|
|
|
for i := 0; i < x.Len(); i++ {
|
|
|
|
m := v.MethodByName("Get")
|
|
|
|
out[i] = m.Call([]reflect.Value{reflect.ValueOf(i)})[0].Interface()
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}),
|
|
|
|
cmp.Transformer("", func(x pref.Descriptor) map[string]interface{} {
|
|
|
|
out := make(map[string]interface{})
|
|
|
|
v := reflect.ValueOf(x)
|
|
|
|
for i := 0; i < v.NumMethod(); i++ {
|
|
|
|
name := v.Type().Method(i).Name
|
|
|
|
if m := v.Method(i); m.Type().NumIn() == 0 && m.Type().NumOut() == 1 {
|
|
|
|
switch name {
|
2019-05-08 18:50:41 -07:00
|
|
|
case "ParentFile", "Parent":
|
|
|
|
// Ignore parents to avoid recursive cycle.
|
2018-11-26 22:32:06 -08:00
|
|
|
case "Options":
|
|
|
|
// Ignore descriptor options since protos are not cmperable.
|
2019-05-13 14:32:56 -07:00
|
|
|
case "ContainingOneof", "ContainingMessage", "Enum", "Message":
|
2018-11-26 22:32:06 -08:00
|
|
|
// Avoid descending into a dependency to avoid a cycle.
|
|
|
|
// Just record the full name if available.
|
|
|
|
//
|
|
|
|
// TODO: Cycle support in cmp would be useful here.
|
|
|
|
v := m.Call(nil)[0]
|
|
|
|
if !v.IsNil() {
|
|
|
|
out[name] = v.Interface().(pref.Descriptor).FullName()
|
|
|
|
}
|
2019-08-02 16:58:08 -07:00
|
|
|
case "Type":
|
|
|
|
// Ignore ExtensionTypeDescriptor.Type method to avoid cycle.
|
2018-11-26 22:32:06 -08:00
|
|
|
default:
|
|
|
|
out[name] = m.Call(nil)[0].Interface()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}),
|
2019-08-02 16:58:08 -07:00
|
|
|
cmp.Transformer("", func(xt pref.ExtensionType) map[string]interface{} {
|
|
|
|
return map[string]interface{}{
|
2019-08-28 11:08:22 -07:00
|
|
|
"Descriptor": xt.TypeDescriptor(),
|
2019-08-02 16:58:08 -07:00
|
|
|
}
|
|
|
|
}),
|
2018-11-26 22:32:06 -08:00
|
|
|
cmp.Transformer("", func(v pref.Value) interface{} {
|
|
|
|
return v.Interface()
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
if diff := cmp.Diff(&wantType, &gotType, opts); diff != "" {
|
|
|
|
t.Errorf("ExtensionType mismatch (-want, +got):\n%v", diff)
|
|
|
|
}
|
|
|
|
|
|
|
|
opts = cmp.Options{
|
2019-08-08 15:45:59 -07:00
|
|
|
cmpopts.IgnoreFields(pimpl.ExtensionInfo{}, "ExtensionType"),
|
|
|
|
cmpopts.IgnoreUnexported(pimpl.ExtensionInfo{}),
|
2018-11-26 22:32:06 -08:00
|
|
|
}
|
|
|
|
if diff := cmp.Diff(wantDesc, gotDesc, opts); diff != "" {
|
|
|
|
t.Errorf("ExtensionDesc mismatch (-want, +got):\n%v", diff)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
internal/impl: support legacy extension fields
Implement support for extension fields for messages that use the v1
data structures for extensions. The legacyExtensionFields type wraps a
v1 map to implement the v2 protoreflect.KnownFields interface.
Working on this change revealed a bug in the dynamic construction of
message types for protobuf messages that had cyclic dependencies (e.g.,
message Foo has a sub-field of message Bar, and Bar has a sub-field of Foo).
In such a situation, a deadlock occurs because initialization code depends on
the very initialization code that is currently running. To break these cycles,
we make some systematic changes listed in the following paragraphs.
Generally speaking, we separate the logic for construction and wrapping,
where constuction does not recursively rely on dependencies,
while wrapping may recursively inspect dependencies.
Promote the MessageType.MessageOf method as a standalone MessageOf function
that dynamically finds the proper *MessageType to use. We make it such that
MessageType only supports two forms of messages types:
* Those that fully implement the v2 API.
* Those that do not implement the v2 API at all.
This removes support for the hybrid form that was exploited by message_test.go
In impl/message_test.go, switch each message to look more like how future
generated messages will look like. This is done in reaction to the fact that
MessageType.MessageOf no longer exists.
In value/{map,vector}.go, fix Unwrap to return a pointer since the underlying
reflect.Value is addressable reference value, not a pointer value.
In value/convert.go, split the logic apart so that obtaining a v2 type and
wrapping a type as v2 are distinct operations. Wrapping requires further
initialization than simply creating the initial message type, and calling it
during initial construction would lead to a deadlock.
In protoreflect/go_type.go, we switch back to a lazy initialization of GoType
to avoid a deadlock since the user-provided fn may rely on the fact that
prototype.GoMessage returned.
Change-Id: I5dea00e36fe1a9899bd2ac0aed2c8e51d5d87420
Reviewed-on: https://go-review.googlesource.com/c/148826
Reviewed-by: Herbie Ong <herbie@google.com>
2018-11-06 13:05:20 -08:00
|
|
|
}
|
2019-05-22 13:42:54 -04:00
|
|
|
|
|
|
|
type (
|
|
|
|
MessageA struct {
|
|
|
|
A1 *MessageA `protobuf:"bytes,1,req,name=a1"`
|
|
|
|
A2 *MessageB `protobuf:"bytes,2,req,name=a2"`
|
|
|
|
A3 Enum `protobuf:"varint,3,opt,name=a3,enum=legacy.Enum"`
|
|
|
|
}
|
|
|
|
MessageB struct {
|
|
|
|
B1 *MessageA `protobuf:"bytes,1,req,name=b1"`
|
|
|
|
B2 *MessageB `protobuf:"bytes,2,req,name=b2"`
|
|
|
|
B3 Enum `protobuf:"varint,3,opt,name=b3,enum=legacy.Enum"`
|
|
|
|
}
|
|
|
|
Enum int32
|
|
|
|
)
|
|
|
|
|
2019-09-03 15:33:00 -07:00
|
|
|
func (*MessageA) Reset() { panic("not implemented") }
|
|
|
|
func (*MessageA) String() string { panic("not implemented") }
|
|
|
|
func (*MessageA) ProtoMessage() { panic("not implemented") }
|
2019-06-06 13:01:53 -07:00
|
|
|
func (*MessageA) Descriptor() ([]byte, []int) { return concurrentFD, []int{0} }
|
2019-09-03 15:33:00 -07:00
|
|
|
|
|
|
|
func (*MessageB) Reset() { panic("not implemented") }
|
|
|
|
func (*MessageB) String() string { panic("not implemented") }
|
|
|
|
func (*MessageB) ProtoMessage() { panic("not implemented") }
|
2019-06-06 13:01:53 -07:00
|
|
|
func (*MessageB) Descriptor() ([]byte, []int) { return concurrentFD, []int{1} }
|
2019-09-03 15:33:00 -07:00
|
|
|
|
|
|
|
func (Enum) EnumDescriptor() ([]byte, []int) { return concurrentFD, []int{0} }
|
2019-06-06 13:01:53 -07:00
|
|
|
|
|
|
|
var concurrentFD = func() []byte {
|
|
|
|
b, _ := proto.Marshal(pdesc.ToFileDescriptorProto(mustMakeFileDesc(`
|
|
|
|
name: "concurrent.proto"
|
|
|
|
syntax: "proto2"
|
|
|
|
package: "legacy"
|
|
|
|
message_type: [{
|
|
|
|
name: "MessageA"
|
|
|
|
field: [
|
|
|
|
{name:"a1" number:1 label:LABEL_REQUIRED type:TYPE_MESSAGE type_name:".legacy.MessageA"},
|
|
|
|
{name:"a2" number:2 label:LABEL_REQUIRED type:TYPE_MESSAGE type_name:".legacy.MessageB"},
|
|
|
|
{name:"a3" number:3 label:LABEL_OPTIONAL type:TYPE_ENUM type_name:".legacy.Enum"}
|
|
|
|
]
|
|
|
|
}, {
|
|
|
|
name: "MessageB"
|
|
|
|
field: [
|
|
|
|
{name:"a1" number:1 label:LABEL_REQUIRED type:TYPE_MESSAGE type_name:".legacy.MessageA"},
|
|
|
|
{name:"a2" number:2 label:LABEL_REQUIRED type:TYPE_MESSAGE type_name:".legacy.MessageB"},
|
|
|
|
{name:"a3" number:3 label:LABEL_OPTIONAL type:TYPE_ENUM type_name:".legacy.Enum"}
|
|
|
|
]
|
|
|
|
}]
|
|
|
|
enum_type: [{
|
|
|
|
name: "Enum"
|
|
|
|
value: [{name:"FOO" number:500}]
|
|
|
|
}]
|
|
|
|
`, nil)))
|
|
|
|
return pimpl.Export{}.CompressGZIP(b)
|
|
|
|
}()
|
|
|
|
|
2019-09-16 13:30:15 -07:00
|
|
|
// TestLegacyConcurrentInit tests that concurrent wrapping of multiple legacy types
|
2019-05-22 13:42:54 -04:00
|
|
|
// results in the exact same descriptor being created.
|
2019-09-16 13:30:15 -07:00
|
|
|
func TestLegacyConcurrentInit(t *testing.T) {
|
2019-05-22 13:42:54 -04:00
|
|
|
const numParallel = 5
|
|
|
|
var messageATypes [numParallel]pref.MessageType
|
|
|
|
var messageBTypes [numParallel]pref.MessageType
|
|
|
|
var enumDescs [numParallel]pref.EnumDescriptor
|
|
|
|
|
|
|
|
// Concurrently load message and enum types.
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
for i := 0; i < numParallel; i++ {
|
|
|
|
i := i
|
|
|
|
wg.Add(3)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
messageATypes[i] = pimpl.Export{}.MessageTypeOf((*MessageA)(nil))
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
messageBTypes[i] = pimpl.Export{}.MessageTypeOf((*MessageB)(nil))
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
enumDescs[i] = pimpl.Export{}.EnumDescriptorOf(Enum(0))
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
var (
|
|
|
|
wantMTA = messageATypes[0]
|
2019-08-02 16:58:08 -07:00
|
|
|
wantMDA = messageATypes[0].Descriptor().Fields().ByNumber(1).Message()
|
2019-05-22 13:42:54 -04:00
|
|
|
wantMTB = messageBTypes[0]
|
2019-08-02 16:58:08 -07:00
|
|
|
wantMDB = messageBTypes[0].Descriptor().Fields().ByNumber(2).Message()
|
|
|
|
wantED = messageATypes[0].Descriptor().Fields().ByNumber(3).Enum()
|
2019-05-22 13:42:54 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
for _, gotMT := range messageATypes[1:] {
|
|
|
|
if gotMT != wantMTA {
|
|
|
|
t.Error("MessageType(MessageA) mismatch")
|
|
|
|
}
|
2019-08-02 16:58:08 -07:00
|
|
|
if gotMDA := gotMT.Descriptor().Fields().ByNumber(1).Message(); gotMDA != wantMDA {
|
2019-05-22 13:42:54 -04:00
|
|
|
t.Error("MessageDescriptor(MessageA) mismatch")
|
|
|
|
}
|
2019-08-02 16:58:08 -07:00
|
|
|
if gotMDB := gotMT.Descriptor().Fields().ByNumber(2).Message(); gotMDB != wantMDB {
|
2019-05-22 13:42:54 -04:00
|
|
|
t.Error("MessageDescriptor(MessageB) mismatch")
|
|
|
|
}
|
2019-08-02 16:58:08 -07:00
|
|
|
if gotED := gotMT.Descriptor().Fields().ByNumber(3).Enum(); gotED != wantED {
|
2019-05-22 13:42:54 -04:00
|
|
|
t.Error("EnumDescriptor(Enum) mismatch")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, gotMT := range messageBTypes[1:] {
|
|
|
|
if gotMT != wantMTB {
|
|
|
|
t.Error("MessageType(MessageB) mismatch")
|
|
|
|
}
|
2019-08-02 16:58:08 -07:00
|
|
|
if gotMDA := gotMT.Descriptor().Fields().ByNumber(1).Message(); gotMDA != wantMDA {
|
2019-05-22 13:42:54 -04:00
|
|
|
t.Error("MessageDescriptor(MessageA) mismatch")
|
|
|
|
}
|
2019-08-02 16:58:08 -07:00
|
|
|
if gotMDB := gotMT.Descriptor().Fields().ByNumber(2).Message(); gotMDB != wantMDB {
|
2019-05-22 13:42:54 -04:00
|
|
|
t.Error("MessageDescriptor(MessageB) mismatch")
|
|
|
|
}
|
2019-08-02 16:58:08 -07:00
|
|
|
if gotED := gotMT.Descriptor().Fields().ByNumber(3).Enum(); gotED != wantED {
|
2019-05-22 13:42:54 -04:00
|
|
|
t.Error("EnumDescriptor(Enum) mismatch")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, gotED := range enumDescs[1:] {
|
|
|
|
if gotED != wantED {
|
|
|
|
t.Error("EnumType(Enum) mismatch")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-04 22:41:40 -07:00
|
|
|
|
|
|
|
type LegacyTestMessageName1 struct{}
|
|
|
|
|
|
|
|
func (*LegacyTestMessageName1) Reset() { panic("not implemented") }
|
|
|
|
func (*LegacyTestMessageName1) String() string { panic("not implemented") }
|
|
|
|
func (*LegacyTestMessageName1) ProtoMessage() { panic("not implemented") }
|
|
|
|
|
|
|
|
type LegacyTestMessageName2 struct{}
|
|
|
|
|
|
|
|
func (*LegacyTestMessageName2) Reset() { panic("not implemented") }
|
|
|
|
func (*LegacyTestMessageName2) String() string { panic("not implemented") }
|
|
|
|
func (*LegacyTestMessageName2) ProtoMessage() { panic("not implemented") }
|
|
|
|
func (*LegacyTestMessageName2) XXX_MessageName() string {
|
|
|
|
return "google.golang.org.LegacyTestMessageName2"
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLegacyMessageName(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
in piface.MessageV1
|
|
|
|
suggestName pref.FullName
|
|
|
|
wantName pref.FullName
|
|
|
|
}{
|
|
|
|
{new(LegacyTestMessageName1), "google.golang.org.LegacyTestMessageName1", "google.golang.org.LegacyTestMessageName1"},
|
|
|
|
{new(LegacyTestMessageName2), "", "google.golang.org.LegacyTestMessageName2"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
mt := pimpl.Export{}.LegacyMessageTypeOf(tt.in, tt.suggestName)
|
|
|
|
if got := mt.Descriptor().FullName(); got != tt.wantName {
|
|
|
|
t.Errorf("type: %T, name mismatch: got %v, want %v", tt.in, got, tt.wantName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|