codec_extension.go 3.4 KB

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