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