attributes.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. *
  3. * Copyright 2019 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. // Package attributes defines a generic key/value store used in various gRPC
  19. // components.
  20. //
  21. // # Experimental
  22. //
  23. // Notice: This package is EXPERIMENTAL and may be changed or removed in a
  24. // later release.
  25. package attributes
  26. import (
  27. "fmt"
  28. "strings"
  29. )
  30. // Attributes is an immutable struct for storing and retrieving generic
  31. // key/value pairs. Keys must be hashable, and users should define their own
  32. // types for keys. Values should not be modified after they are added to an
  33. // Attributes or if they were received from one. If values implement 'Equal(o
  34. // interface{}) bool', it will be called by (*Attributes).Equal to determine
  35. // whether two values with the same key should be considered equal.
  36. type Attributes struct {
  37. m map[interface{}]interface{}
  38. }
  39. // New returns a new Attributes containing the key/value pair.
  40. func New(key, value interface{}) *Attributes {
  41. return &Attributes{m: map[interface{}]interface{}{key: value}}
  42. }
  43. // WithValue returns a new Attributes containing the previous keys and values
  44. // and the new key/value pair. If the same key appears multiple times, the
  45. // last value overwrites all previous values for that key. To remove an
  46. // existing key, use a nil value. value should not be modified later.
  47. func (a *Attributes) WithValue(key, value interface{}) *Attributes {
  48. if a == nil {
  49. return New(key, value)
  50. }
  51. n := &Attributes{m: make(map[interface{}]interface{}, len(a.m)+1)}
  52. for k, v := range a.m {
  53. n.m[k] = v
  54. }
  55. n.m[key] = value
  56. return n
  57. }
  58. // Value returns the value associated with these attributes for key, or nil if
  59. // no value is associated with key. The returned value should not be modified.
  60. func (a *Attributes) Value(key interface{}) interface{} {
  61. if a == nil {
  62. return nil
  63. }
  64. return a.m[key]
  65. }
  66. // Equal returns whether a and o are equivalent. If 'Equal(o interface{})
  67. // bool' is implemented for a value in the attributes, it is called to
  68. // determine if the value matches the one stored in the other attributes. If
  69. // Equal is not implemented, standard equality is used to determine if the two
  70. // values are equal. Note that some types (e.g. maps) aren't comparable by
  71. // default, so they must be wrapped in a struct, or in an alias type, with Equal
  72. // defined.
  73. func (a *Attributes) Equal(o *Attributes) bool {
  74. if a == nil && o == nil {
  75. return true
  76. }
  77. if a == nil || o == nil {
  78. return false
  79. }
  80. if len(a.m) != len(o.m) {
  81. return false
  82. }
  83. for k, v := range a.m {
  84. ov, ok := o.m[k]
  85. if !ok {
  86. // o missing element of a
  87. return false
  88. }
  89. if eq, ok := v.(interface{ Equal(o interface{}) bool }); ok {
  90. if !eq.Equal(ov) {
  91. return false
  92. }
  93. } else if v != ov {
  94. // Fallback to a standard equality check if Value is unimplemented.
  95. return false
  96. }
  97. }
  98. return true
  99. }
  100. // String prints the attribute map. If any key or values throughout the map
  101. // implement fmt.Stringer, it calls that method and appends.
  102. func (a *Attributes) String() string {
  103. var sb strings.Builder
  104. sb.WriteString("{")
  105. first := true
  106. for k, v := range a.m {
  107. var key, val string
  108. if str, ok := k.(interface{ String() string }); ok {
  109. key = str.String()
  110. }
  111. if str, ok := v.(interface{ String() string }); ok {
  112. val = str.String()
  113. }
  114. if !first {
  115. sb.WriteString(", ")
  116. }
  117. sb.WriteString(fmt.Sprintf("%q: %q, ", key, val))
  118. first = false
  119. }
  120. sb.WriteString("}")
  121. return sb.String()
  122. }