Damien Neil | f1e905b | 2019-08-08 15:45:59 -0700 | [diff] [blame] | 1 | // 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 | |
| 5 | package impl |
| 6 | |
| 7 | import ( |
| 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. |
| 21 | type 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 Tsai | fd4c605 | 2019-09-16 13:30:15 -0700 | [diff] [blame] | 29 | // 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 Neil | f1e905b | 2019-08-08 15:45:59 -0700 | [diff] [blame] | 32 | // |
| 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 Neil | f1e905b | 2019-08-08 15:45:59 -0700 | [diff] [blame] | 38 | goType reflect.Type |
Joe Tsai | fd4c605 | 2019-09-16 13:30:15 -0700 | [diff] [blame] | 39 | desc extensionTypeDescriptor |
Damien Neil | f1e905b | 2019-08-08 15:45:59 -0700 | [diff] [blame] | 40 | conv Converter |
| 41 | |
Damien Neil | f1e905b | 2019-08-08 15:45:59 -0700 | [diff] [blame] | 42 | // 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 Neil | 3b65992 | 2019-10-03 16:38:33 -0700 | [diff] [blame^] | 49 | // ExtensionType is the zero value of the extension type. |
Damien Neil | f1e905b | 2019-08-08 15:45:59 -0700 | [diff] [blame] | 50 | // |
Damien Neil | 3b65992 | 2019-10-03 16:38:33 -0700 | [diff] [blame^] | 51 | // For historical reasons, reflect.TypeOf(ExtensionType) and the |
| 52 | // type returned by InterfaceOf may not be identical. |
Damien Neil | f1e905b | 2019-08-08 15:45:59 -0700 | [diff] [blame] | 53 | // |
Damien Neil | 3b65992 | 2019-10-03 16:38:33 -0700 | [diff] [blame^] | 54 | // Deprecated: Use InterfaceOf(xt.Zero()) instead. |
Damien Neil | f1e905b | 2019-08-08 15:45:59 -0700 | [diff] [blame] | 55 | 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. |
| 79 | const ( |
| 80 | extensionInfoUninitialized = 0 |
| 81 | extensionInfoDescInit = 1 |
| 82 | extensionInfoFullInit = 2 |
| 83 | ) |
| 84 | |
| 85 | func InitExtensionInfo(xi *ExtensionInfo, xd pref.ExtensionDescriptor, goType reflect.Type) { |
Damien Neil | f1e905b | 2019-08-08 15:45:59 -0700 | [diff] [blame] | 86 | xi.goType = goType |
Joe Tsai | fd4c605 | 2019-09-16 13:30:15 -0700 | [diff] [blame] | 87 | xi.desc = extensionTypeDescriptor{xd, xi} |
Damien Neil | f1e905b | 2019-08-08 15:45:59 -0700 | [diff] [blame] | 88 | xi.init = extensionInfoDescInit |
| 89 | } |
| 90 | |
| 91 | func (xi *ExtensionInfo) New() pref.Value { |
| 92 | return xi.lazyInit().New() |
| 93 | } |
| 94 | func (xi *ExtensionInfo) Zero() pref.Value { |
| 95 | return xi.lazyInit().Zero() |
| 96 | } |
| 97 | func (xi *ExtensionInfo) ValueOf(v interface{}) pref.Value { |
| 98 | return xi.lazyInit().PBValueOf(reflect.ValueOf(v)) |
| 99 | } |
| 100 | func (xi *ExtensionInfo) InterfaceOf(v pref.Value) interface{} { |
| 101 | return xi.lazyInit().GoValueOf(v).Interface() |
| 102 | } |
Damien Neil | 835b271 | 2019-08-29 14:08:28 -0700 | [diff] [blame] | 103 | func (xi *ExtensionInfo) IsValidValue(v pref.Value) bool { |
| 104 | return xi.lazyInit().IsValidPB(v) |
| 105 | } |
| 106 | func (xi *ExtensionInfo) IsValidInterface(v interface{}) bool { |
| 107 | return xi.lazyInit().IsValidGo(reflect.ValueOf(v)) |
| 108 | } |
Damien Neil | 79bfdbe | 2019-08-28 11:08:22 -0700 | [diff] [blame] | 109 | func (xi *ExtensionInfo) TypeDescriptor() pref.ExtensionTypeDescriptor { |
Joe Tsai | fd4c605 | 2019-09-16 13:30:15 -0700 | [diff] [blame] | 110 | if atomic.LoadUint32(&xi.init) < extensionInfoDescInit { |
Damien Neil | f1e905b | 2019-08-08 15:45:59 -0700 | [diff] [blame] | 111 | xi.lazyInitSlow() |
| 112 | } |
Joe Tsai | fd4c605 | 2019-09-16 13:30:15 -0700 | [diff] [blame] | 113 | return &xi.desc |
Damien Neil | f1e905b | 2019-08-08 15:45:59 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | func (xi *ExtensionInfo) lazyInit() Converter { |
Joe Tsai | fd4c605 | 2019-09-16 13:30:15 -0700 | [diff] [blame] | 117 | if atomic.LoadUint32(&xi.init) < extensionInfoFullInit { |
Damien Neil | f1e905b | 2019-08-08 15:45:59 -0700 | [diff] [blame] | 118 | xi.lazyInitSlow() |
| 119 | } |
| 120 | return xi.conv |
| 121 | } |
| 122 | |
| 123 | func (xi *ExtensionInfo) lazyInitSlow() { |
| 124 | xi.mu.Lock() |
| 125 | defer xi.mu.Unlock() |
| 126 | |
| 127 | if xi.init == extensionInfoFullInit { |
| 128 | return |
| 129 | } |
Joe Tsai | 2df47c9 | 2019-09-09 16:52:28 -0700 | [diff] [blame] | 130 | defer atomic.StoreUint32(&xi.init, extensionInfoFullInit) |
Damien Neil | f1e905b | 2019-08-08 15:45:59 -0700 | [diff] [blame] | 131 | |
Joe Tsai | fd4c605 | 2019-09-16 13:30:15 -0700 | [diff] [blame] | 132 | if xi.desc.ExtensionDescriptor == nil { |
Damien Neil | f1e905b | 2019-08-08 15:45:59 -0700 | [diff] [blame] | 133 | xi.initFromLegacy() |
Damien Neil | f1e905b | 2019-08-08 15:45:59 -0700 | [diff] [blame] | 134 | } |
Joe Tsai | bee6252 | 2019-09-05 17:25:15 -0700 | [diff] [blame] | 135 | if !xi.desc.ExtensionDescriptor.IsPlaceholder() { |
| 136 | if xi.ExtensionType == nil { |
| 137 | xi.initToLegacy() |
| 138 | } |
| 139 | xi.conv = NewConverter(xi.goType, xi.desc) |
Damien Neil | f1e905b | 2019-08-08 15:45:59 -0700 | [diff] [blame] | 140 | } |
Damien Neil | f1e905b | 2019-08-08 15:45:59 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | type extensionTypeDescriptor struct { |
| 144 | pref.ExtensionDescriptor |
| 145 | xi *ExtensionInfo |
| 146 | } |
| 147 | |
| 148 | func (xtd *extensionTypeDescriptor) Type() pref.ExtensionType { |
| 149 | return xtd.xi |
| 150 | } |
| 151 | func (xtd *extensionTypeDescriptor) Descriptor() pref.ExtensionDescriptor { |
| 152 | return xtd.ExtensionDescriptor |
| 153 | } |