aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/google.golang.org/grpc/metadata/metadata.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/google.golang.org/grpc/metadata/metadata.go')
-rw-r--r--vendor/google.golang.org/grpc/metadata/metadata.go104
1 files changed, 86 insertions, 18 deletions
diff --git a/vendor/google.golang.org/grpc/metadata/metadata.go b/vendor/google.golang.org/grpc/metadata/metadata.go
index be4f9e7..cf6d1b9 100644
--- a/vendor/google.golang.org/grpc/metadata/metadata.go
+++ b/vendor/google.golang.org/grpc/metadata/metadata.go
@@ -17,17 +17,19 @@
17 */ 17 */
18 18
19// Package metadata define the structure of the metadata supported by gRPC library. 19// Package metadata define the structure of the metadata supported by gRPC library.
20// Please refer to https://grpc.io/docs/guides/wire.html for more information about custom-metadata. 20// Please refer to https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md
21// for more information about custom-metadata.
21package metadata // import "google.golang.org/grpc/metadata" 22package metadata // import "google.golang.org/grpc/metadata"
22 23
23import ( 24import (
25 "context"
24 "fmt" 26 "fmt"
25 "strings" 27 "strings"
26
27 "golang.org/x/net/context"
28) 28)
29 29
30// DecodeKeyValue returns k, v, nil. It is deprecated and should not be used. 30// DecodeKeyValue returns k, v, nil.
31//
32// Deprecated: use k and v directly instead.
31func DecodeKeyValue(k, v string) (string, string, error) { 33func DecodeKeyValue(k, v string) (string, string, error) {
32 return k, v, nil 34 return k, v, nil
33} 35}
@@ -44,6 +46,9 @@ type MD map[string][]string
44// - lowercase letters: a-z 46// - lowercase letters: a-z
45// - special characters: -_. 47// - special characters: -_.
46// Uppercase letters are automatically converted to lowercase. 48// Uppercase letters are automatically converted to lowercase.
49//
50// Keys beginning with "grpc-" are reserved for grpc-internal use only and may
51// result in errors if set in metadata.
47func New(m map[string]string) MD { 52func New(m map[string]string) MD {
48 md := MD{} 53 md := MD{}
49 for k, val := range m { 54 for k, val := range m {
@@ -62,6 +67,9 @@ func New(m map[string]string) MD {
62// - lowercase letters: a-z 67// - lowercase letters: a-z
63// - special characters: -_. 68// - special characters: -_.
64// Uppercase letters are automatically converted to lowercase. 69// Uppercase letters are automatically converted to lowercase.
70//
71// Keys beginning with "grpc-" are reserved for grpc-internal use only and may
72// result in errors if set in metadata.
65func Pairs(kv ...string) MD { 73func Pairs(kv ...string) MD {
66 if len(kv)%2 == 1 { 74 if len(kv)%2 == 1 {
67 panic(fmt.Sprintf("metadata: Pairs got the odd number of input pairs for metadata: %d", len(kv))) 75 panic(fmt.Sprintf("metadata: Pairs got the odd number of input pairs for metadata: %d", len(kv)))
@@ -88,6 +96,30 @@ func (md MD) Copy() MD {
88 return Join(md) 96 return Join(md)
89} 97}
90 98
99// Get obtains the values for a given key.
100func (md MD) Get(k string) []string {
101 k = strings.ToLower(k)
102 return md[k]
103}
104
105// Set sets the value of a given key with a slice of values.
106func (md MD) Set(k string, vals ...string) {
107 if len(vals) == 0 {
108 return
109 }
110 k = strings.ToLower(k)
111 md[k] = vals
112}
113
114// Append adds the values to key k, not overwriting what was already stored at that key.
115func (md MD) Append(k string, vals ...string) {
116 if len(vals) == 0 {
117 return
118 }
119 k = strings.ToLower(k)
120 md[k] = append(md[k], vals...)
121}
122
91// Join joins any number of mds into a single MD. 123// Join joins any number of mds into a single MD.
92// The order of values for each key is determined by the order in which 124// The order of values for each key is determined by the order in which
93// the mds containing those values are presented to Join. 125// the mds containing those values are presented to Join.
@@ -104,24 +136,31 @@ func Join(mds ...MD) MD {
104type mdIncomingKey struct{} 136type mdIncomingKey struct{}
105type mdOutgoingKey struct{} 137type mdOutgoingKey struct{}
106 138
107// NewContext is a wrapper for NewOutgoingContext(ctx, md). Deprecated.
108func NewContext(ctx context.Context, md MD) context.Context {
109 return NewOutgoingContext(ctx, md)
110}
111
112// NewIncomingContext creates a new context with incoming md attached. 139// NewIncomingContext creates a new context with incoming md attached.
113func NewIncomingContext(ctx context.Context, md MD) context.Context { 140func NewIncomingContext(ctx context.Context, md MD) context.Context {
114 return context.WithValue(ctx, mdIncomingKey{}, md) 141 return context.WithValue(ctx, mdIncomingKey{}, md)
115} 142}
116 143
117// NewOutgoingContext creates a new context with outgoing md attached. 144// NewOutgoingContext creates a new context with outgoing md attached. If used
145// in conjunction with AppendToOutgoingContext, NewOutgoingContext will
146// overwrite any previously-appended metadata.
118func NewOutgoingContext(ctx context.Context, md MD) context.Context { 147func NewOutgoingContext(ctx context.Context, md MD) context.Context {
119 return context.WithValue(ctx, mdOutgoingKey{}, md) 148 return context.WithValue(ctx, mdOutgoingKey{}, rawMD{md: md})
120} 149}
121 150
122// FromContext is a wrapper for FromIncomingContext(ctx). Deprecated. 151// AppendToOutgoingContext returns a new context with the provided kv merged
123func FromContext(ctx context.Context) (md MD, ok bool) { 152// with any existing metadata in the context. Please refer to the
124 return FromIncomingContext(ctx) 153// documentation of Pairs for a description of kv.
154func AppendToOutgoingContext(ctx context.Context, kv ...string) context.Context {
155 if len(kv)%2 == 1 {
156 panic(fmt.Sprintf("metadata: AppendToOutgoingContext got an odd number of input pairs for metadata: %d", len(kv)))
157 }
158 md, _ := ctx.Value(mdOutgoingKey{}).(rawMD)
159 added := make([][]string, len(md.added)+1)
160 copy(added, md.added)
161 added[len(added)-1] = make([]string, len(kv))
162 copy(added[len(added)-1], kv)
163 return context.WithValue(ctx, mdOutgoingKey{}, rawMD{md: md.md, added: added})
125} 164}
126 165
127// FromIncomingContext returns the incoming metadata in ctx if it exists. The 166// FromIncomingContext returns the incoming metadata in ctx if it exists. The
@@ -132,10 +171,39 @@ func FromIncomingContext(ctx context.Context) (md MD, ok bool) {
132 return 171 return
133} 172}
134 173
174// FromOutgoingContextRaw returns the un-merged, intermediary contents
175// of rawMD. Remember to perform strings.ToLower on the keys. The returned
176// MD should not be modified. Writing to it may cause races. Modification
177// should be made to copies of the returned MD.
178//
179// This is intended for gRPC-internal use ONLY.
180func FromOutgoingContextRaw(ctx context.Context) (MD, [][]string, bool) {
181 raw, ok := ctx.Value(mdOutgoingKey{}).(rawMD)
182 if !ok {
183 return nil, nil, false
184 }
185
186 return raw.md, raw.added, true
187}
188
135// FromOutgoingContext returns the outgoing metadata in ctx if it exists. The 189// FromOutgoingContext returns the outgoing metadata in ctx if it exists. The
136// returned MD should not be modified. Writing to it may cause races. 190// returned MD should not be modified. Writing to it may cause races.
137// Modification should be made to the copies of the returned MD. 191// Modification should be made to copies of the returned MD.
138func FromOutgoingContext(ctx context.Context) (md MD, ok bool) { 192func FromOutgoingContext(ctx context.Context) (MD, bool) {
139 md, ok = ctx.Value(mdOutgoingKey{}).(MD) 193 raw, ok := ctx.Value(mdOutgoingKey{}).(rawMD)
140 return 194 if !ok {
195 return nil, false
196 }
197
198 mds := make([]MD, 0, len(raw.added)+1)
199 mds = append(mds, raw.md)
200 for _, vv := range raw.added {
201 mds = append(mds, Pairs(vv...))
202 }
203 return Join(mds...), ok
204}
205
206type rawMD struct {
207 md MD
208 added [][]string
141} 209}