blob: 6ff2792ee4fa7b9b1410c8414759f9d445339271 [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//
22// Experimental
23//
24// Notice: This package is EXPERIMENTAL and may be changed or removed in a
25// later release.
26package attributes
27
khenaidoo5fc5cea2021-08-11 17:39:16 -040028// Attributes is an immutable struct for storing and retrieving generic
29// key/value pairs. Keys must be hashable, and users should define their own
khenaidoo5cb0d402021-12-08 14:09:16 -050030// types for keys. Values should not be modified after they are added to an
31// Attributes or if they were received from one. If values implement 'Equal(o
32// interface{}) bool', it will be called by (*Attributes).Equal to determine
33// whether two values with the same key should be considered equal.
khenaidoo5fc5cea2021-08-11 17:39:16 -040034type Attributes struct {
35 m map[interface{}]interface{}
36}
37
khenaidoo5cb0d402021-12-08 14:09:16 -050038// New returns a new Attributes containing the key/value pair.
39func New(key, value interface{}) *Attributes {
40 return &Attributes{m: map[interface{}]interface{}{key: value}}
khenaidoo5fc5cea2021-08-11 17:39:16 -040041}
42
khenaidoo5cb0d402021-12-08 14:09:16 -050043// 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.
47func (a *Attributes) WithValue(key, value interface{}) *Attributes {
khenaidoo5fc5cea2021-08-11 17:39:16 -040048 if a == nil {
khenaidoo5cb0d402021-12-08 14:09:16 -050049 return New(key, value)
khenaidoo5fc5cea2021-08-11 17:39:16 -040050 }
khenaidoo5cb0d402021-12-08 14:09:16 -050051 n := &Attributes{m: make(map[interface{}]interface{}, len(a.m)+1)}
khenaidoo5fc5cea2021-08-11 17:39:16 -040052 for k, v := range a.m {
53 n.m[k] = v
54 }
khenaidoo5cb0d402021-12-08 14:09:16 -050055 n.m[key] = value
khenaidoo5fc5cea2021-08-11 17:39:16 -040056 return n
57}
58
59// Value returns the value associated with these attributes for key, or nil if
khenaidoo5cb0d402021-12-08 14:09:16 -050060// no value is associated with key. The returned value should not be modified.
khenaidoo5fc5cea2021-08-11 17:39:16 -040061func (a *Attributes) Value(key interface{}) interface{} {
62 if a == nil {
63 return nil
64 }
65 return a.m[key]
66}
khenaidoo5cb0d402021-12-08 14:09:16 -050067
68// Equal returns whether a and o are equivalent. If 'Equal(o interface{})
69// bool' is implemented for a value in the attributes, it is called to
70// determine if the value matches the one stored in the other attributes. If
71// Equal is not implemented, standard equality is used to determine if the two
72// values are equal.
73func (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}