blob: 541f4a7998deb0dd35120881f426495895a912a4 [file] [log] [blame]
Damien Neilf1e905b2019-08-08 15:45:59 -07001// Copyright 2019 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
5package impl
6
7import (
8 "reflect"
9 "sync"
10 "sync/atomic"
11
12 pref "google.golang.org/protobuf/reflect/protoreflect"
13 piface "google.golang.org/protobuf/runtime/protoiface"
14)
15
16// ExtensionInfo implements ExtensionType.
17//
18// This type contains a number of exported fields for legacy compatibility.
19// The only non-deprecated use of this type is through the methods of the
20// ExtensionType interface.
21type ExtensionInfo struct {
22 // An ExtensionInfo may exist in several stages of initialization.
23 //
24 // extensionInfoUninitialized: Some or all of the legacy exported
25 // fields may be set, but none of the unexported fields have been
26 // initialized. This is the starting state for an ExtensionInfo
27 // in legacy generated code.
28 //
Joe Tsaifd4c6052019-09-16 13:30:15 -070029 // extensionInfoDescInit: The desc field is set, but other unexported fields
30 // may not be initialized. Legacy exported fields may or may not be set.
31 // This is the starting state for an ExtensionInfo in newly generated code.
Damien Neilf1e905b2019-08-08 15:45:59 -070032 //
33 // extensionInfoFullInit: The ExtensionInfo is fully initialized.
34 // This state is only entered after lazy initialization is complete.
35 init uint32
36 mu sync.Mutex
37
Damien Neilf1e905b2019-08-08 15:45:59 -070038 goType reflect.Type
Joe Tsaifd4c6052019-09-16 13:30:15 -070039 desc extensionTypeDescriptor
Damien Neilf1e905b2019-08-08 15:45:59 -070040 conv Converter
41
Damien Neilf1e905b2019-08-08 15:45:59 -070042 // ExtendedType is a typed nil-pointer to the parent message type that
43 // is being extended. It is possible for this to be unpopulated in v2
44 // since the message may no longer implement the MessageV1 interface.
45 //
46 // Deprecated: Use the ExtendedType method instead.
47 ExtendedType piface.MessageV1
48
Damien Neil3b659922019-10-03 16:38:33 -070049 // ExtensionType is the zero value of the extension type.
Damien Neilf1e905b2019-08-08 15:45:59 -070050 //
Damien Neil3b659922019-10-03 16:38:33 -070051 // For historical reasons, reflect.TypeOf(ExtensionType) and the
52 // type returned by InterfaceOf may not be identical.
Damien Neilf1e905b2019-08-08 15:45:59 -070053 //
Damien Neil3b659922019-10-03 16:38:33 -070054 // Deprecated: Use InterfaceOf(xt.Zero()) instead.
Damien Neilf1e905b2019-08-08 15:45:59 -070055 ExtensionType interface{}
56
57 // Field is the field number of the extension.
58 //
59 // Deprecated: Use the Descriptor().Number method instead.
60 Field int32
61
62 // Name is the fully qualified name of extension.
63 //
64 // Deprecated: Use the Descriptor().FullName method instead.
65 Name string
66
67 // Tag is the protobuf struct tag used in the v1 API.
68 //
69 // Deprecated: Do not use.
70 Tag string
71
72 // Filename is the proto filename in which the extension is defined.
73 //
74 // Deprecated: Use Descriptor().ParentFile().Path() instead.
75 Filename string
76}
77
78// Stages of initialization: See the ExtensionInfo.init field.
79const (
80 extensionInfoUninitialized = 0
81 extensionInfoDescInit = 1
82 extensionInfoFullInit = 2
83)
84
85func InitExtensionInfo(xi *ExtensionInfo, xd pref.ExtensionDescriptor, goType reflect.Type) {
Damien Neilf1e905b2019-08-08 15:45:59 -070086 xi.goType = goType
Joe Tsaifd4c6052019-09-16 13:30:15 -070087 xi.desc = extensionTypeDescriptor{xd, xi}
Damien Neilf1e905b2019-08-08 15:45:59 -070088 xi.init = extensionInfoDescInit
89}
90
91func (xi *ExtensionInfo) New() pref.Value {
92 return xi.lazyInit().New()
93}
94func (xi *ExtensionInfo) Zero() pref.Value {
95 return xi.lazyInit().Zero()
96}
97func (xi *ExtensionInfo) ValueOf(v interface{}) pref.Value {
98 return xi.lazyInit().PBValueOf(reflect.ValueOf(v))
99}
100func (xi *ExtensionInfo) InterfaceOf(v pref.Value) interface{} {
101 return xi.lazyInit().GoValueOf(v).Interface()
102}
Damien Neil835b2712019-08-29 14:08:28 -0700103func (xi *ExtensionInfo) IsValidValue(v pref.Value) bool {
104 return xi.lazyInit().IsValidPB(v)
105}
106func (xi *ExtensionInfo) IsValidInterface(v interface{}) bool {
107 return xi.lazyInit().IsValidGo(reflect.ValueOf(v))
108}
Damien Neil79bfdbe2019-08-28 11:08:22 -0700109func (xi *ExtensionInfo) TypeDescriptor() pref.ExtensionTypeDescriptor {
Joe Tsaifd4c6052019-09-16 13:30:15 -0700110 if atomic.LoadUint32(&xi.init) < extensionInfoDescInit {
Damien Neilf1e905b2019-08-08 15:45:59 -0700111 xi.lazyInitSlow()
112 }
Joe Tsaifd4c6052019-09-16 13:30:15 -0700113 return &xi.desc
Damien Neilf1e905b2019-08-08 15:45:59 -0700114}
115
116func (xi *ExtensionInfo) lazyInit() Converter {
Joe Tsaifd4c6052019-09-16 13:30:15 -0700117 if atomic.LoadUint32(&xi.init) < extensionInfoFullInit {
Damien Neilf1e905b2019-08-08 15:45:59 -0700118 xi.lazyInitSlow()
119 }
120 return xi.conv
121}
122
123func (xi *ExtensionInfo) lazyInitSlow() {
124 xi.mu.Lock()
125 defer xi.mu.Unlock()
126
127 if xi.init == extensionInfoFullInit {
128 return
129 }
Joe Tsai2df47c92019-09-09 16:52:28 -0700130 defer atomic.StoreUint32(&xi.init, extensionInfoFullInit)
Damien Neilf1e905b2019-08-08 15:45:59 -0700131
Joe Tsaifd4c6052019-09-16 13:30:15 -0700132 if xi.desc.ExtensionDescriptor == nil {
Damien Neilf1e905b2019-08-08 15:45:59 -0700133 xi.initFromLegacy()
Damien Neilf1e905b2019-08-08 15:45:59 -0700134 }
Joe Tsaibee62522019-09-05 17:25:15 -0700135 if !xi.desc.ExtensionDescriptor.IsPlaceholder() {
136 if xi.ExtensionType == nil {
137 xi.initToLegacy()
138 }
139 xi.conv = NewConverter(xi.goType, xi.desc)
Damien Neilf1e905b2019-08-08 15:45:59 -0700140 }
Damien Neilf1e905b2019-08-08 15:45:59 -0700141}
142
143type extensionTypeDescriptor struct {
144 pref.ExtensionDescriptor
145 xi *ExtensionInfo
146}
147
148func (xtd *extensionTypeDescriptor) Type() pref.ExtensionType {
149 return xtd.xi
150}
151func (xtd *extensionTypeDescriptor) Descriptor() pref.ExtensionDescriptor {
152 return xtd.ExtensionDescriptor
153}