forked from go-kratos/kratos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetadata.go
113 lines (98 loc) · 2.74 KB
/
metadata.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package metadata
import (
"context"
"fmt"
"strings"
)
// Metadata is our way of representing request headers internally.
// They're used at the RPC level and translate back and forth
// from Transport headers.
type Metadata map[string]string
// New creates an MD from a given key-values map.
func New(mds ...map[string]string) Metadata {
md := Metadata{}
for _, m := range mds {
for k, v := range m {
if k == "" {
continue
}
key := strings.ToLower(k)
if len(v) > 0 && v != "" {
md[key] = v
}
}
}
return md
}
// Get returns the value associated with the passed key.
func (m Metadata) Get(key string) string {
k := strings.ToLower(key)
return m[k]
}
// Set stores the key-value pair.
func (m Metadata) Set(key string, value string) {
if key == "" || value == "" {
return
}
k := strings.ToLower(key)
m[k] = value
}
// Range iterate over element in metadata.
func (m Metadata) Range(f func(k, v string) bool) {
for k, v := range m {
ret := f(k, v)
if ret == false {
break
}
}
}
// Clone returns a deep copy of Metadata
func (m Metadata) Clone() Metadata {
md := Metadata{}
for k, v := range m {
md[k] = v
}
return md
}
type serverMetadataKey struct{}
// NewServerContext creates a new context with client md attached.
func NewServerContext(ctx context.Context, md Metadata) context.Context {
return context.WithValue(ctx, serverMetadataKey{}, md)
}
// FromServerContext returns the server metadata in ctx if it exists.
func FromServerContext(ctx context.Context) (Metadata, bool) {
md, ok := ctx.Value(serverMetadataKey{}).(Metadata)
return md, ok
}
type clientMetadataKey struct{}
// NewClientContext creates a new context with client md attached.
func NewClientContext(ctx context.Context, md Metadata) context.Context {
return context.WithValue(ctx, clientMetadataKey{}, md)
}
// FromClientContext returns the client metadata in ctx if it exists.
func FromClientContext(ctx context.Context) (Metadata, bool) {
md, ok := ctx.Value(clientMetadataKey{}).(Metadata)
return md, ok
}
// AppendToClientContext returns a new context with the provided kv merged
// with any existing metadata in the context.
func AppendToClientContext(ctx context.Context, kv ...string) context.Context {
if len(kv)%2 == 1 {
panic(fmt.Sprintf("metadata: AppendToOutgoingContext got an odd number of input pairs for metadata: %d", len(kv)))
}
md, _ := FromClientContext(ctx)
md = md.Clone()
for i := 0; i < len(kv); i += 2 {
md.Set(kv[i], kv[i+1])
}
return NewClientContext(ctx, md)
}
// MergeToClientContext merge new metadata into ctx.
func MergeToClientContext(ctx context.Context, cmd Metadata) context.Context {
md, _ := FromClientContext(ctx)
md = md.Clone()
for k, v := range cmd {
md[k] = v
}
return NewClientContext(ctx, md)
}