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