Damien Neil | c37adef | 2019-04-01 13:49:56 -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 ( |
Joe Tsai | 89d4963 | 2019-06-04 16:20:00 -0700 | [diff] [blame] | 8 | "sync" |
| 9 | "sync/atomic" |
| 10 | |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 11 | "google.golang.org/protobuf/internal/encoding/wire" |
Joe Tsai | 89d4963 | 2019-06-04 16:20:00 -0700 | [diff] [blame] | 12 | pref "google.golang.org/protobuf/reflect/protoreflect" |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 13 | ) |
| 14 | |
| 15 | type extensionFieldInfo struct { |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 16 | wiretag uint64 |
| 17 | tagsize int |
| 18 | unmarshalNeedsValue bool |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame] | 19 | funcs valueCoderFuncs |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 20 | } |
| 21 | |
Damien Neil | 79571e9 | 2019-12-09 10:24:36 -0800 | [diff] [blame^] | 22 | var legacyExtensionFieldInfoCache sync.Map // map[protoreflect.ExtensionType]*extensionFieldInfo |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 23 | |
Damien Neil | 79571e9 | 2019-12-09 10:24:36 -0800 | [diff] [blame^] | 24 | func getExtensionFieldInfo(xt pref.ExtensionType) *extensionFieldInfo { |
| 25 | if xi, ok := xt.(*ExtensionInfo); ok { |
| 26 | xi.lazyInit() |
| 27 | return xi.info |
| 28 | } |
| 29 | return legacyLoadExtensionFieldInfo(xt) |
| 30 | } |
| 31 | |
| 32 | // legacyLoadExtensionFieldInfo dynamically loads a *ExtensionInfo for xt. |
| 33 | func legacyLoadExtensionFieldInfo(xt pref.ExtensionType) *extensionFieldInfo { |
| 34 | if xi, ok := legacyExtensionFieldInfoCache.Load(xt); ok { |
| 35 | return xi.(*extensionFieldInfo) |
| 36 | } |
| 37 | e := makeExtensionFieldInfo(xt.TypeDescriptor()) |
| 38 | if e, ok := legacyMessageTypeCache.LoadOrStore(xt, e); ok { |
| 39 | return e.(*extensionFieldInfo) |
| 40 | } |
| 41 | return e |
| 42 | } |
| 43 | |
| 44 | func makeExtensionFieldInfo(xd pref.ExtensionDescriptor) *extensionFieldInfo { |
Damien Neil | 7492a09 | 2019-07-10 15:23:29 -0700 | [diff] [blame] | 45 | var wiretag uint64 |
Damien Neil | 92f7618 | 2019-08-02 16:58:08 -0700 | [diff] [blame] | 46 | if !xd.IsPacked() { |
| 47 | wiretag = wire.EncodeTag(xd.Number(), wireTypes[xd.Kind()]) |
Damien Neil | 7492a09 | 2019-07-10 15:23:29 -0700 | [diff] [blame] | 48 | } else { |
Damien Neil | 92f7618 | 2019-08-02 16:58:08 -0700 | [diff] [blame] | 49 | wiretag = wire.EncodeTag(xd.Number(), wire.BytesType) |
Damien Neil | 7492a09 | 2019-07-10 15:23:29 -0700 | [diff] [blame] | 50 | } |
Damien Neil | 79571e9 | 2019-12-09 10:24:36 -0800 | [diff] [blame^] | 51 | e := &extensionFieldInfo{ |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 52 | wiretag: wiretag, |
| 53 | tagsize: wire.SizeVarint(wiretag), |
Damien Neil | 4b3a82f | 2019-09-04 19:07:00 -0700 | [diff] [blame] | 54 | funcs: encoderFuncsForValue(xd), |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 55 | } |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 56 | // Does the unmarshal function need a value passed to it? |
| 57 | // This is true for composite types, where we pass in a message, list, or map to fill in, |
| 58 | // and for enums, where we pass in a prototype value to specify the concrete enum type. |
Damien Neil | 92f7618 | 2019-08-02 16:58:08 -0700 | [diff] [blame] | 59 | switch xd.Kind() { |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 60 | case pref.MessageKind, pref.GroupKind, pref.EnumKind: |
| 61 | e.unmarshalNeedsValue = true |
| 62 | default: |
Damien Neil | 92f7618 | 2019-08-02 16:58:08 -0700 | [diff] [blame] | 63 | if xd.Cardinality() == pref.Repeated { |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 64 | e.unmarshalNeedsValue = true |
| 65 | } |
| 66 | } |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 67 | return e |
| 68 | } |
Joe Tsai | 89d4963 | 2019-06-04 16:20:00 -0700 | [diff] [blame] | 69 | |
| 70 | type ExtensionField struct { |
Joe Tsai | 05e11b8 | 2019-06-04 17:05:04 -0700 | [diff] [blame] | 71 | typ pref.ExtensionType |
Joe Tsai | 89d4963 | 2019-06-04 16:20:00 -0700 | [diff] [blame] | 72 | |
| 73 | // value is either the value of GetValue, |
| 74 | // or a *lazyExtensionValue that then returns the value of GetValue. |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame] | 75 | value pref.Value |
| 76 | lazy *lazyExtensionValue |
Joe Tsai | 89d4963 | 2019-06-04 16:20:00 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame] | 79 | // Set sets the type and value of the extension field. |
| 80 | // This must not be called concurrently. |
| 81 | func (f *ExtensionField) Set(t pref.ExtensionType, v pref.Value) { |
Joe Tsai | 05e11b8 | 2019-06-04 17:05:04 -0700 | [diff] [blame] | 82 | f.typ = t |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame] | 83 | f.value = v |
Joe Tsai | 89d4963 | 2019-06-04 16:20:00 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame] | 86 | // SetLazy sets the type and a value that is to be lazily evaluated upon first use. |
| 87 | // This must not be called concurrently. |
| 88 | func (f *ExtensionField) SetLazy(t pref.ExtensionType, fn func() pref.Value) { |
| 89 | f.typ = t |
| 90 | f.lazy = &lazyExtensionValue{value: fn} |
Joe Tsai | 89d4963 | 2019-06-04 16:20:00 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame] | 93 | // Value returns the value of the extension field. |
Joe Tsai | 89d4963 | 2019-06-04 16:20:00 -0700 | [diff] [blame] | 94 | // This may be called concurrently. |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame] | 95 | func (f *ExtensionField) Value() pref.Value { |
| 96 | if f.lazy != nil { |
| 97 | return f.lazy.GetValue() |
Joe Tsai | 89d4963 | 2019-06-04 16:20:00 -0700 | [diff] [blame] | 98 | } |
| 99 | return f.value |
| 100 | } |
| 101 | |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame] | 102 | // Type returns the type of the extension field. |
| 103 | // This may be called concurrently. |
| 104 | func (f ExtensionField) Type() pref.ExtensionType { |
| 105 | return f.typ |
Joe Tsai | 89d4963 | 2019-06-04 16:20:00 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame] | 108 | // IsSet returns whether the extension field is set. |
| 109 | // This may be called concurrently. |
| 110 | func (f ExtensionField) IsSet() bool { |
| 111 | return f.typ != nil |
| 112 | } |
| 113 | |
| 114 | // Deprecated: Do not use. |
| 115 | func (f ExtensionField) HasType() bool { |
| 116 | return f.typ != nil |
| 117 | } |
| 118 | |
| 119 | // Deprecated: Do not use. |
| 120 | func (f ExtensionField) GetType() pref.ExtensionType { |
| 121 | return f.typ |
| 122 | } |
| 123 | |
| 124 | // Deprecated: Do not use. |
| 125 | func (f *ExtensionField) SetType(t pref.ExtensionType) { |
| 126 | f.typ = t |
| 127 | } |
| 128 | |
| 129 | // Deprecated: Do not use. |
| 130 | func (f ExtensionField) HasValue() bool { |
| 131 | return f.value.IsValid() || f.lazy != nil |
| 132 | } |
| 133 | |
| 134 | // Deprecated: Do not use. |
| 135 | func (f ExtensionField) GetValue() interface{} { |
| 136 | return f.typ.InterfaceOf(f.Value()) |
| 137 | } |
| 138 | |
| 139 | // Deprecated: Do not use. |
| 140 | func (f *ExtensionField) SetEagerValue(ival interface{}) { |
| 141 | f.value = f.typ.ValueOf(ival) |
| 142 | } |
| 143 | |
| 144 | // Deprecated: Do not use. |
| 145 | func (f *ExtensionField) SetLazyValue(fn func() interface{}) { |
Joe Tsai | 8ee364e | 2019-09-06 00:19:33 -0700 | [diff] [blame] | 146 | f.lazy = &lazyExtensionValue{value: func() pref.Value { |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame] | 147 | return f.typ.ValueOf(fn()) |
| 148 | }} |
Joe Tsai | 89d4963 | 2019-06-04 16:20:00 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | type lazyExtensionValue struct { |
| 152 | once uint32 // atomically set if value is valid |
| 153 | mu sync.Mutex // protects value |
Joe Tsai | 8ee364e | 2019-09-06 00:19:33 -0700 | [diff] [blame] | 154 | value interface{} // either a pref.Value itself or a func() pref.ValueOf |
Joe Tsai | 89d4963 | 2019-06-04 16:20:00 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame] | 157 | func (v *lazyExtensionValue) GetValue() pref.Value { |
Joe Tsai | 89d4963 | 2019-06-04 16:20:00 -0700 | [diff] [blame] | 158 | if atomic.LoadUint32(&v.once) == 0 { |
| 159 | v.mu.Lock() |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame] | 160 | if f, ok := v.value.(func() pref.Value); ok { |
Joe Tsai | 89d4963 | 2019-06-04 16:20:00 -0700 | [diff] [blame] | 161 | v.value = f() |
| 162 | } |
| 163 | atomic.StoreUint32(&v.once, 1) |
| 164 | v.mu.Unlock() |
| 165 | } |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame] | 166 | return v.value.(pref.Value) |
Joe Tsai | 89d4963 | 2019-06-04 16:20:00 -0700 | [diff] [blame] | 167 | } |