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