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 |
| 19 | funcs ifaceCoderFuncs |
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 | |
Joe Tsai | 89d4963 | 2019-06-04 16:20:00 -0700 | [diff] [blame] | 32 | wiretag := wire.EncodeTag(xt.Number(), wireTypes[xt.Kind()]) |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 33 | e = &extensionFieldInfo{ |
| 34 | wiretag: wiretag, |
| 35 | tagsize: wire.SizeVarint(wiretag), |
Joe Tsai | 89d4963 | 2019-06-04 16:20:00 -0700 | [diff] [blame] | 36 | funcs: encoderFuncsForValue(xt, xt.GoType()), |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 37 | } |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 38 | // Does the unmarshal function need a value passed to it? |
| 39 | // This is true for composite types, where we pass in a message, list, or map to fill in, |
| 40 | // and for enums, where we pass in a prototype value to specify the concrete enum type. |
| 41 | switch xt.Kind() { |
| 42 | case pref.MessageKind, pref.GroupKind, pref.EnumKind: |
| 43 | e.unmarshalNeedsValue = true |
| 44 | default: |
| 45 | if xt.Cardinality() == pref.Repeated { |
| 46 | e.unmarshalNeedsValue = true |
| 47 | } |
| 48 | } |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 49 | mi.extensionFieldInfosMu.Lock() |
| 50 | if mi.extensionFieldInfos == nil { |
Joe Tsai | 89d4963 | 2019-06-04 16:20:00 -0700 | [diff] [blame] | 51 | mi.extensionFieldInfos = make(map[pref.ExtensionType]*extensionFieldInfo) |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 52 | } |
Joe Tsai | 89d4963 | 2019-06-04 16:20:00 -0700 | [diff] [blame] | 53 | mi.extensionFieldInfos[xt] = e |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 54 | mi.extensionFieldInfosMu.Unlock() |
| 55 | return e |
| 56 | } |
Joe Tsai | 89d4963 | 2019-06-04 16:20:00 -0700 | [diff] [blame] | 57 | |
| 58 | type ExtensionField struct { |
Joe Tsai | 05e11b8 | 2019-06-04 17:05:04 -0700 | [diff] [blame] | 59 | typ pref.ExtensionType |
Joe Tsai | 89d4963 | 2019-06-04 16:20:00 -0700 | [diff] [blame] | 60 | |
| 61 | // value is either the value of GetValue, |
| 62 | // or a *lazyExtensionValue that then returns the value of GetValue. |
| 63 | value interface{} // TODO: switch to protoreflect.Value |
| 64 | } |
| 65 | |
| 66 | func (f ExtensionField) HasType() bool { |
Joe Tsai | 05e11b8 | 2019-06-04 17:05:04 -0700 | [diff] [blame] | 67 | return f.typ != nil |
Joe Tsai | 89d4963 | 2019-06-04 16:20:00 -0700 | [diff] [blame] | 68 | } |
| 69 | func (f ExtensionField) GetType() pref.ExtensionType { |
Joe Tsai | 05e11b8 | 2019-06-04 17:05:04 -0700 | [diff] [blame] | 70 | return f.typ |
Joe Tsai | 89d4963 | 2019-06-04 16:20:00 -0700 | [diff] [blame] | 71 | } |
| 72 | func (f *ExtensionField) SetType(t pref.ExtensionType) { |
Joe Tsai | 05e11b8 | 2019-06-04 17:05:04 -0700 | [diff] [blame] | 73 | f.typ = t |
Joe Tsai | 89d4963 | 2019-06-04 16:20:00 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | // HasValue reports whether a value is set for the extension field. |
| 77 | // This may be called concurrently. |
| 78 | func (f ExtensionField) HasValue() bool { |
| 79 | return f.value != nil |
| 80 | } |
| 81 | |
| 82 | // GetValue returns the concrete value for the extension field. |
| 83 | // Let the type of Desc.ExtensionType be the "API type" and |
| 84 | // the type of GetValue be the "storage type". |
| 85 | // The API type and storage type are the same except: |
| 86 | // * for scalars (except []byte), where the API type uses *T, |
| 87 | // while the storage type uses T. |
| 88 | // * for repeated fields, where the API type uses []T, |
| 89 | // while the storage type uses *[]T. |
| 90 | // |
| 91 | // The reason for the divergence is so that the storage type more naturally |
| 92 | // matches what is expected of when retrieving the values through the |
| 93 | // protobuf reflection APIs. |
| 94 | // |
| 95 | // GetValue is only populated if Desc is also populated. |
| 96 | // This may be called concurrently. |
| 97 | // |
| 98 | // TODO: switch interface{} to protoreflect.Value |
| 99 | func (f ExtensionField) GetValue() interface{} { |
| 100 | if f, ok := f.value.(*lazyExtensionValue); ok { |
| 101 | return f.GetValue() |
| 102 | } |
| 103 | return f.value |
| 104 | } |
| 105 | |
| 106 | // SetEagerValue sets the current value of the extension. |
| 107 | // This must not be called concurrently. |
| 108 | func (f *ExtensionField) SetEagerValue(v interface{}) { |
| 109 | f.value = v |
| 110 | } |
| 111 | |
| 112 | // SetLazyValue sets a value that is to be lazily evaluated upon first use. |
| 113 | // The returned value must not be nil. |
| 114 | // This must not be called concurrently. |
| 115 | func (f *ExtensionField) SetLazyValue(v func() interface{}) { |
| 116 | f.value = &lazyExtensionValue{value: v} |
| 117 | } |
| 118 | |
| 119 | type lazyExtensionValue struct { |
| 120 | once uint32 // atomically set if value is valid |
| 121 | mu sync.Mutex // protects value |
| 122 | value interface{} // either the value itself or a func() interface{} |
| 123 | } |
| 124 | |
| 125 | func (v *lazyExtensionValue) GetValue() interface{} { |
| 126 | if atomic.LoadUint32(&v.once) == 0 { |
| 127 | v.mu.Lock() |
| 128 | if f, ok := v.value.(func() interface{}); ok { |
| 129 | v.value = f() |
| 130 | } |
| 131 | atomic.StoreUint32(&v.once, 1) |
| 132 | v.mu.Unlock() |
| 133 | } |
| 134 | return v.value |
| 135 | } |