blob: 3efca45914937a809f38bfd12cc4d937b5944752 [file] [log] [blame]
khenaidoo5fc5cea2021-08-11 17:39:16 -04001/*
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
19// Package attributes defines a generic key/value store used in various gRPC
20// components.
21//
Joey Armstrongba3d9d12024-01-15 14:22:11 -050022// # Experimental
khenaidoo5fc5cea2021-08-11 17:39:16 -040023//
24// Notice: This package is EXPERIMENTAL and may be changed or removed in a
25// later release.
26package attributes
27
Akash Kankanala761955c2024-02-21 19:32:20 +053028import (
29 "fmt"
30 "strings"
31)
32
khenaidoo5fc5cea2021-08-11 17:39:16 -040033// Attributes is an immutable struct for storing and retrieving generic
34// key/value pairs. Keys must be hashable, and users should define their own
khenaidoo5cb0d402021-12-08 14:09:16 -050035// types for keys. Values should not be modified after they are added to an
36// Attributes or if they were received from one. If values implement 'Equal(o
37// interface{}) bool', it will be called by (*Attributes).Equal to determine
38// whether two values with the same key should be considered equal.
khenaidoo5fc5cea2021-08-11 17:39:16 -040039type Attributes struct {
40 m map[interface{}]interface{}
41}
42
khenaidoo5cb0d402021-12-08 14:09:16 -050043// New returns a new Attributes containing the key/value pair.
44func New(key, value interface{}) *Attributes {
45 return &Attributes{m: map[interface{}]interface{}{key: value}}
khenaidoo5fc5cea2021-08-11 17:39:16 -040046}
47
khenaidoo5cb0d402021-12-08 14:09:16 -050048// WithValue returns a new Attributes containing the previous keys and values
49// and the new key/value pair. If the same key appears multiple times, the
50// last value overwrites all previous values for that key. To remove an
51// existing key, use a nil value. value should not be modified later.
52func (a *Attributes) WithValue(key, value interface{}) *Attributes {
khenaidoo5fc5cea2021-08-11 17:39:16 -040053 if a == nil {
khenaidoo5cb0d402021-12-08 14:09:16 -050054 return New(key, value)
khenaidoo5fc5cea2021-08-11 17:39:16 -040055 }
khenaidoo5cb0d402021-12-08 14:09:16 -050056 n := &Attributes{m: make(map[interface{}]interface{}, len(a.m)+1)}
khenaidoo5fc5cea2021-08-11 17:39:16 -040057 for k, v := range a.m {
58 n.m[k] = v
59 }
khenaidoo5cb0d402021-12-08 14:09:16 -050060 n.m[key] = value
khenaidoo5fc5cea2021-08-11 17:39:16 -040061 return n
62}
63
64// Value returns the value associated with these attributes for key, or nil if
khenaidoo5cb0d402021-12-08 14:09:16 -050065// no value is associated with key. The returned value should not be modified.
khenaidoo5fc5cea2021-08-11 17:39:16 -040066func (a *Attributes) Value(key interface{}) interface{} {
67 if a == nil {
68 return nil
69 }
70 return a.m[key]
71}
khenaidoo5cb0d402021-12-08 14:09:16 -050072
73// Equal returns whether a and o are equivalent. If 'Equal(o interface{})
74// bool' is implemented for a value in the attributes, it is called to
75// determine if the value matches the one stored in the other attributes. If
76// Equal is not implemented, standard equality is used to determine if the two
khenaidoo257f3192021-12-15 16:46:37 -050077// values are equal. Note that some types (e.g. maps) aren't comparable by
78// default, so they must be wrapped in a struct, or in an alias type, with Equal
79// defined.
khenaidoo5cb0d402021-12-08 14:09:16 -050080func (a *Attributes) Equal(o *Attributes) bool {
81 if a == nil && o == nil {
82 return true
83 }
84 if a == nil || o == nil {
85 return false
86 }
87 if len(a.m) != len(o.m) {
88 return false
89 }
90 for k, v := range a.m {
91 ov, ok := o.m[k]
92 if !ok {
93 // o missing element of a
94 return false
95 }
96 if eq, ok := v.(interface{ Equal(o interface{}) bool }); ok {
97 if !eq.Equal(ov) {
98 return false
99 }
100 } else if v != ov {
101 // Fallback to a standard equality check if Value is unimplemented.
102 return false
103 }
104 }
105 return true
106}
Akash Kankanala761955c2024-02-21 19:32:20 +0530107
108// String prints the attribute map. If any key or values throughout the map
109// implement fmt.Stringer, it calls that method and appends.
110func (a *Attributes) String() string {
111 var sb strings.Builder
112 sb.WriteString("{")
113 first := true
114 for k, v := range a.m {
115 var key, val string
116 if str, ok := k.(interface{ String() string }); ok {
117 key = str.String()
118 }
119 if str, ok := v.(interface{ String() string }); ok {
120 val = str.String()
121 }
122 if !first {
123 sb.WriteString(", ")
124 }
125 sb.WriteString(fmt.Sprintf("%q: %q, ", key, val))
126 first = false
127 }
128 sb.WriteString("}")
129 return sb.String()
130}