codec_extension.go 4.0 KB

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