Joe Tsai | 62517cc | 2018-12-04 14:00:01 -0800 | [diff] [blame] | 1 | // Copyright 2018 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package legacy_test |
| 6 | |
| 7 | import ( |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 8 | "reflect" |
Joe Tsai | 62517cc | 2018-12-04 14:00:01 -0800 | [diff] [blame] | 9 | "testing" |
| 10 | |
Damien Neil | e89e624 | 2019-05-13 23:55:40 -0700 | [diff] [blame^] | 11 | pimpl "google.golang.org/protobuf/internal/impl" |
| 12 | plegacy "google.golang.org/protobuf/internal/legacy" |
| 13 | ptype "google.golang.org/protobuf/internal/prototype" |
| 14 | pref "google.golang.org/protobuf/reflect/protoreflect" |
| 15 | piface "google.golang.org/protobuf/runtime/protoiface" |
Joe Tsai | 62517cc | 2018-12-04 14:00:01 -0800 | [diff] [blame] | 16 | |
Damien Neil | e89e624 | 2019-05-13 23:55:40 -0700 | [diff] [blame^] | 17 | proto2_20180125 "google.golang.org/protobuf/internal/testprotos/legacy/proto2.v1.0.0-20180125-92554152" |
Joe Tsai | 62517cc | 2018-12-04 14:00:01 -0800 | [diff] [blame] | 18 | ) |
| 19 | |
| 20 | type legacyTestMessage struct { |
Joe Tsai | 4fddeba | 2019-03-20 18:29:32 -0700 | [diff] [blame] | 21 | XXX_unrecognized []byte |
Joe Tsai | 00e50dc | 2019-04-16 12:39:41 -0700 | [diff] [blame] | 22 | XXX_InternalExtensions map[int32]pimpl.ExtensionFieldV1 |
Joe Tsai | 62517cc | 2018-12-04 14:00:01 -0800 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | func (*legacyTestMessage) Reset() {} |
| 26 | func (*legacyTestMessage) String() string { return "" } |
| 27 | func (*legacyTestMessage) ProtoMessage() {} |
Joe Tsai | 4fddeba | 2019-03-20 18:29:32 -0700 | [diff] [blame] | 28 | func (*legacyTestMessage) ExtensionRangeArray() []piface.ExtensionRangeV1 { |
| 29 | return []piface.ExtensionRangeV1{{Start: 10000, End: 20000}} |
Joe Tsai | 62517cc | 2018-12-04 14:00:01 -0800 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | func mustMakeExtensionType(x *ptype.StandaloneExtension, v interface{}) pref.ExtensionType { |
| 33 | xd, err := ptype.NewExtension(x) |
| 34 | if err != nil { |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 35 | panic(err) |
Joe Tsai | 62517cc | 2018-12-04 14:00:01 -0800 | [diff] [blame] | 36 | } |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 37 | return plegacy.ExtensionTypeOf(xd, reflect.TypeOf(v)) |
Joe Tsai | 62517cc | 2018-12-04 14:00:01 -0800 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | var ( |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 41 | parentDesc = pimpl.Export{}.MessageDescriptorOf((*legacyTestMessage)(nil)) |
| 42 | messageV1Desc = pimpl.Export{}.MessageDescriptorOf((*proto2_20180125.Message_ChildMessage)(nil)) |
Joe Tsai | 62517cc | 2018-12-04 14:00:01 -0800 | [diff] [blame] | 43 | |
| 44 | wantType = mustMakeExtensionType(&ptype.StandaloneExtension{ |
| 45 | FullName: "fizz.buzz.optional_message_v1", |
| 46 | Number: 10007, |
| 47 | Cardinality: pref.Optional, |
| 48 | Kind: pref.MessageKind, |
Joe Tsai | 0fc49f8 | 2019-05-01 12:29:25 -0700 | [diff] [blame] | 49 | MessageType: messageV1Desc, |
| 50 | ExtendedType: parentDesc, |
Joe Tsai | 62517cc | 2018-12-04 14:00:01 -0800 | [diff] [blame] | 51 | }, (*proto2_20180125.Message_ChildMessage)(nil)) |
Joe Tsai | 4fddeba | 2019-03-20 18:29:32 -0700 | [diff] [blame] | 52 | wantDesc = &piface.ExtensionDescV1{ |
Joe Tsai | 62517cc | 2018-12-04 14:00:01 -0800 | [diff] [blame] | 53 | ExtendedType: (*legacyTestMessage)(nil), |
| 54 | ExtensionType: (*proto2_20180125.Message_ChildMessage)(nil), |
| 55 | Field: 10007, |
| 56 | Name: "fizz.buzz.optional_message_v1", |
| 57 | Tag: "bytes,10007,opt,name=optional_message_v1", |
| 58 | } |
| 59 | ) |
| 60 | |
| 61 | func BenchmarkConvert(b *testing.B) { |
| 62 | b.ReportAllocs() |
| 63 | for i := 0; i < b.N; i++ { |
| 64 | xd := plegacy.Export{}.ExtensionDescFromType(wantType) |
| 65 | gotType := plegacy.Export{}.ExtensionTypeFromDesc(xd) |
| 66 | if gotType != wantType { |
| 67 | b.Fatalf("ExtensionType mismatch: got %p, want %p", gotType, wantType) |
| 68 | } |
| 69 | |
| 70 | xt := plegacy.Export{}.ExtensionTypeFromDesc(wantDesc) |
| 71 | gotDesc := plegacy.Export{}.ExtensionDescFromType(xt) |
| 72 | if gotDesc != wantDesc { |
| 73 | b.Fatalf("ExtensionDesc mismatch: got %p, want %p", gotDesc, wantDesc) |
| 74 | } |
| 75 | } |
| 76 | } |