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.go141
1 files changed, 141 insertions, 0 deletions
diff --git a/vendor/google.golang.org/grpc/metadata/metadata.go b/vendor/google.golang.org/grpc/metadata/metadata.go
new file mode 100644
index 0000000..be4f9e7
--- /dev/null
+++ b/vendor/google.golang.org/grpc/metadata/metadata.go
@@ -0,0 +1,141 @@
1/*
2 *
3 * Copyright 2014 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 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.
21package metadata // import "google.golang.org/grpc/metadata"
22
23import (
24 "fmt"
25 "strings"
26
27 "golang.org/x/net/context"
28)
29
30// DecodeKeyValue returns k, v, nil. It is deprecated and should not be used.
31func DecodeKeyValue(k, v string) (string, string, error) {
32 return k, v, nil
33}
34
35// MD is a mapping from metadata keys to values. Users should use the following
36// two convenience functions New and Pairs to generate MD.
37type MD map[string][]string
38
39// New creates an MD from a given key-value map.
40//
41// Only the following ASCII characters are allowed in keys:
42// - digits: 0-9
43// - uppercase letters: A-Z (normalized to lower)
44// - lowercase letters: a-z
45// - special characters: -_.
46// Uppercase letters are automatically converted to lowercase.
47func New(m map[string]string) MD {
48 md := MD{}
49 for k, val := range m {
50 key := strings.ToLower(k)
51 md[key] = append(md[key], val)
52 }
53 return md
54}
55
56// Pairs returns an MD formed by the mapping of key, value ...
57// Pairs panics if len(kv) is odd.
58//
59// Only the following ASCII characters are allowed in keys:
60// - digits: 0-9
61// - uppercase letters: A-Z (normalized to lower)
62// - lowercase letters: a-z
63// - special characters: -_.
64// Uppercase letters are automatically converted to lowercase.
65func Pairs(kv ...string) MD {
66 if len(kv)%2 == 1 {
67 panic(fmt.Sprintf("metadata: Pairs got the odd number of input pairs for metadata: %d", len(kv)))
68 }
69 md := MD{}
70 var key string
71 for i, s := range kv {
72 if i%2 == 0 {
73 key = strings.ToLower(s)
74 continue
75 }
76 md[key] = append(md[key], s)
77 }
78 return md
79}
80
81// Len returns the number of items in md.
82func (md MD) Len() int {
83 return len(md)
84}
85
86// Copy returns a copy of md.
87func (md MD) Copy() MD {
88 return Join(md)
89}
90
91// 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
93// the mds containing those values are presented to Join.
94func Join(mds ...MD) MD {
95 out := MD{}
96 for _, md := range mds {
97 for k, v := range md {
98 out[k] = append(out[k], v...)
99 }
100 }
101 return out
102}
103
104type mdIncomingKey struct{}
105type mdOutgoingKey struct{}
106
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.
113func NewIncomingContext(ctx context.Context, md MD) context.Context {
114 return context.WithValue(ctx, mdIncomingKey{}, md)
115}
116
117// NewOutgoingContext creates a new context with outgoing md attached.
118func NewOutgoingContext(ctx context.Context, md MD) context.Context {
119 return context.WithValue(ctx, mdOutgoingKey{}, md)
120}
121
122// FromContext is a wrapper for FromIncomingContext(ctx). Deprecated.
123func FromContext(ctx context.Context) (md MD, ok bool) {
124 return FromIncomingContext(ctx)
125}
126
127// FromIncomingContext returns the incoming metadata in ctx if it exists. The
128// returned MD should not be modified. Writing to it may cause races.
129// Modification should be made to copies of the returned MD.
130func FromIncomingContext(ctx context.Context) (md MD, ok bool) {
131 md, ok = ctx.Value(mdIncomingKey{}).(MD)
132 return
133}
134
135// FromOutgoingContext returns the outgoing metadata in ctx if it exists. The
136// returned MD should not be modified. Writing to it may cause races.
137// Modification should be made to the copies of the returned MD.
138func FromOutgoingContext(ctx context.Context) (md MD, ok bool) {
139 md, ok = ctx.Value(mdOutgoingKey{}).(MD)
140 return
141}