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 { |
| 16 | wiretag uint64 |
| 17 | tagsize int |
| 18 | funcs ifaceCoderFuncs |
| 19 | } |
| 20 | |
Joe Tsai | 89d4963 | 2019-06-04 16:20:00 -0700 | [diff] [blame] | 21 | func (mi *MessageInfo) extensionFieldInfo(xt pref.ExtensionType) *extensionFieldInfo { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 22 | // As of this time (Go 1.12, linux/amd64), an RWMutex benchmarks as faster |
| 23 | // than a sync.Map. |
| 24 | mi.extensionFieldInfosMu.RLock() |
Joe Tsai | 89d4963 | 2019-06-04 16:20:00 -0700 | [diff] [blame] | 25 | e, ok := mi.extensionFieldInfos[xt] |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 26 | mi.extensionFieldInfosMu.RUnlock() |
| 27 | if ok { |
| 28 | return e |
| 29 | } |
| 30 | |
Joe Tsai | 89d4963 | 2019-06-04 16:20:00 -0700 | [diff] [blame] | 31 | wiretag := wire.EncodeTag(xt.Number(), wireTypes[xt.Kind()]) |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 32 | e = &extensionFieldInfo{ |
| 33 | wiretag: wiretag, |
| 34 | tagsize: wire.SizeVarint(wiretag), |
Joe Tsai | 89d4963 | 2019-06-04 16:20:00 -0700 | [diff] [blame] | 35 | funcs: encoderFuncsForValue(xt, xt.GoType()), |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | mi.extensionFieldInfosMu.Lock() |
| 39 | if mi.extensionFieldInfos == nil { |
Joe Tsai | 89d4963 | 2019-06-04 16:20:00 -0700 | [diff] [blame] | 40 | mi.extensionFieldInfos = make(map[pref.ExtensionType]*extensionFieldInfo) |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 41 | } |
Joe Tsai | 89d4963 | 2019-06-04 16:20:00 -0700 | [diff] [blame] | 42 | mi.extensionFieldInfos[xt] = e |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 43 | mi.extensionFieldInfosMu.Unlock() |
| 44 | return e |
| 45 | } |
Joe Tsai | 89d4963 | 2019-06-04 16:20:00 -0700 | [diff] [blame] | 46 | |
| 47 | type ExtensionField struct { |
Joe Tsai | 05e11b8 | 2019-06-04 17:05:04 -0700 | [diff] [blame^] | 48 | typ pref.ExtensionType |
Joe Tsai | 89d4963 | 2019-06-04 16:20:00 -0700 | [diff] [blame] | 49 | |
| 50 | // value is either the value of GetValue, |
| 51 | // or a *lazyExtensionValue that then returns the value of GetValue. |
| 52 | value interface{} // TODO: switch to protoreflect.Value |
| 53 | } |
| 54 | |
| 55 | func (f ExtensionField) HasType() bool { |
Joe Tsai | 05e11b8 | 2019-06-04 17:05:04 -0700 | [diff] [blame^] | 56 | return f.typ != nil |
Joe Tsai | 89d4963 | 2019-06-04 16:20:00 -0700 | [diff] [blame] | 57 | } |
| 58 | func (f ExtensionField) GetType() pref.ExtensionType { |
Joe Tsai | 05e11b8 | 2019-06-04 17:05:04 -0700 | [diff] [blame^] | 59 | return f.typ |
Joe Tsai | 89d4963 | 2019-06-04 16:20:00 -0700 | [diff] [blame] | 60 | } |
| 61 | func (f *ExtensionField) SetType(t pref.ExtensionType) { |
Joe Tsai | 05e11b8 | 2019-06-04 17:05:04 -0700 | [diff] [blame^] | 62 | f.typ = t |
Joe Tsai | 89d4963 | 2019-06-04 16:20:00 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | // HasValue reports whether a value is set for the extension field. |
| 66 | // This may be called concurrently. |
| 67 | func (f ExtensionField) HasValue() bool { |
| 68 | return f.value != nil |
| 69 | } |
| 70 | |
| 71 | // GetValue returns the concrete value for the extension field. |
| 72 | // Let the type of Desc.ExtensionType be the "API type" and |
| 73 | // the type of GetValue be the "storage type". |
| 74 | // The API type and storage type are the same except: |
| 75 | // * for scalars (except []byte), where the API type uses *T, |
| 76 | // while the storage type uses T. |
| 77 | // * for repeated fields, where the API type uses []T, |
| 78 | // while the storage type uses *[]T. |
| 79 | // |
| 80 | // The reason for the divergence is so that the storage type more naturally |
| 81 | // matches what is expected of when retrieving the values through the |
| 82 | // protobuf reflection APIs. |
| 83 | // |
| 84 | // GetValue is only populated if Desc is also populated. |
| 85 | // This may be called concurrently. |
| 86 | // |
| 87 | // TODO: switch interface{} to protoreflect.Value |
| 88 | func (f ExtensionField) GetValue() interface{} { |
| 89 | if f, ok := f.value.(*lazyExtensionValue); ok { |
| 90 | return f.GetValue() |
| 91 | } |
| 92 | return f.value |
| 93 | } |
| 94 | |
| 95 | // SetEagerValue sets the current value of the extension. |
| 96 | // This must not be called concurrently. |
| 97 | func (f *ExtensionField) SetEagerValue(v interface{}) { |
| 98 | f.value = v |
| 99 | } |
| 100 | |
| 101 | // SetLazyValue sets a value that is to be lazily evaluated upon first use. |
| 102 | // The returned value must not be nil. |
| 103 | // This must not be called concurrently. |
| 104 | func (f *ExtensionField) SetLazyValue(v func() interface{}) { |
| 105 | f.value = &lazyExtensionValue{value: v} |
| 106 | } |
| 107 | |
| 108 | type lazyExtensionValue struct { |
| 109 | once uint32 // atomically set if value is valid |
| 110 | mu sync.Mutex // protects value |
| 111 | value interface{} // either the value itself or a func() interface{} |
| 112 | } |
| 113 | |
| 114 | func (v *lazyExtensionValue) GetValue() interface{} { |
| 115 | if atomic.LoadUint32(&v.once) == 0 { |
| 116 | v.mu.Lock() |
| 117 | if f, ok := v.value.(func() interface{}); ok { |
| 118 | v.value = f() |
| 119 | } |
| 120 | atomic.StoreUint32(&v.once, 1) |
| 121 | v.mu.Unlock() |
| 122 | } |
| 123 | return v.value |
| 124 | } |