package attributes
import "google.golang.org/grpc/attributes"
Package attributes defines a generic key/value store used in various gRPC components.
Experimental
Notice: This package is EXPERIMENTAL and may be changed or removed in a later release.
Index ¶
Examples ¶
Types ¶
type Attributes ¶
type Attributes struct {
// contains filtered or unexported fields
}
Attributes is an immutable struct for storing and retrieving generic
key/value pairs. Keys must be hashable, and users should define their own
types for keys. Values should not be modified after they are added to an
Attributes or if they were received from one. If values implement 'Equal(o
interface{}) bool', it will be called by (*Attributes).Equal to determine
whether two values with the same key should be considered equal.
Code:play
Output:Example¶
package main
import (
"fmt"
"google.golang.org/grpc/attributes"
)
type stringVal struct {
s string
}
func (s stringVal) Equal(o interface{}) bool {
os, ok := o.(stringVal)
return ok && s.s == os.s
}
func main() {
type keyOne struct{}
type keyTwo struct{}
a := attributes.New(keyOne{}, 1).WithValue(keyTwo{}, stringVal{s: "two"})
fmt.Println("Key one:", a.Value(keyOne{}))
fmt.Println("Key two:", a.Value(keyTwo{}))
}
Key one: 1
Key two: {two}
func New ¶
func New(key, value interface{}) *Attributes
New returns a new Attributes containing the key/value pair.
func (*Attributes) Equal ¶
func (a *Attributes) Equal(o *Attributes) bool
Equal returns whether a and o are equivalent. If 'Equal(o interface{}) bool' is implemented for a value in the attributes, it is called to determine if the value matches the one stored in the other attributes. If Equal is not implemented, standard equality is used to determine if the two values are equal. Note that some types (e.g. maps) aren't comparable by default, so they must be wrapped in a struct, or in an alias type, with Equal defined.
func (*Attributes) Value ¶
func (a *Attributes) Value(key interface{}) interface{}
Value returns the value associated with these attributes for key, or nil if no value is associated with key. The returned value should not be modified.
func (*Attributes) WithValue ¶
func (a *Attributes) WithValue(key, value interface{}) *Attributes
WithValue returns a new Attributes containing the previous keys and values
and the new key/value pair. If the same key appears multiple times, the
last value overwrites all previous values for that key. To remove an
existing key, use a nil value. value should not be modified later.
Code:play
Output:Example¶
package main
import (
"fmt"
"google.golang.org/grpc/attributes"
)
type stringVal struct {
s string
}
func (s stringVal) Equal(o interface{}) bool {
os, ok := o.(stringVal)
return ok && s.s == os.s
}
func main() {
type keyOne struct{}
type keyTwo struct{}
a := attributes.New(keyOne{}, 1)
a = a.WithValue(keyTwo{}, stringVal{s: "two"})
fmt.Println("Key one:", a.Value(keyOne{}))
fmt.Println("Key two:", a.Value(keyTwo{}))
}
Key one: 1
Key two: {two}
Source Files ¶
attributes.go
- Version
- v1.53.0
- Published
- Feb 7, 2023
- Platform
- linux/amd64
- Last checked
- 1 hour ago –
Tools for package owners.