|
| 1 | +/* |
| 2 | + Copyright The containerd Authors. |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package ttrpc |
| 18 | + |
| 19 | +import "context" |
| 20 | + |
| 21 | +// Metadata represents the key-value pairs (similar to http.Header) to be passed to ttrpc server from a client. |
| 22 | +type Metadata map[string]StringList |
| 23 | + |
| 24 | +// Get returns the metadata for a given key when they exist. |
| 25 | +// If there is no metadata, a nil slice and false are returned. |
| 26 | +func (m Metadata) Get(key string) ([]string, bool) { |
| 27 | + list, ok := m[key] |
| 28 | + if !ok || len(list.List) == 0 { |
| 29 | + return nil, false |
| 30 | + } |
| 31 | + |
| 32 | + return list.List, true |
| 33 | +} |
| 34 | + |
| 35 | +// Set sets the provided values for a given key. |
| 36 | +// The values will overwrite any existing values. |
| 37 | +// If no values provided, a key will be deleted. |
| 38 | +func (m Metadata) Set(key string, values ...string) { |
| 39 | + if len(values) == 0 { |
| 40 | + delete(m, key) |
| 41 | + return |
| 42 | + } |
| 43 | + |
| 44 | + m[key] = StringList{List: values} |
| 45 | +} |
| 46 | + |
| 47 | +// Append appends additional values to the given key. |
| 48 | +func (m Metadata) Append(key string, values ...string) { |
| 49 | + if len(values) == 0 { |
| 50 | + return |
| 51 | + } |
| 52 | + |
| 53 | + list, ok := m[key] |
| 54 | + if ok { |
| 55 | + m.Set(key, append(list.List, values...)...) |
| 56 | + } else { |
| 57 | + m.Set(key, values...) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +type metadataKey struct{} |
| 62 | + |
| 63 | +// GetMetadata retrieves metadata from context.Context (previously attached with WithMetadata) |
| 64 | +func GetMetadata(ctx context.Context) (Metadata, bool) { |
| 65 | + metadata, ok := ctx.Value(metadataKey{}).(Metadata) |
| 66 | + return metadata, ok |
| 67 | +} |
| 68 | + |
| 69 | +// GetMetadataValue gets a specific metadata value by name from context.Context |
| 70 | +func GetMetadataValue(ctx context.Context, name string) (string, bool) { |
| 71 | + metadata, ok := GetMetadata(ctx) |
| 72 | + if !ok { |
| 73 | + return "", false |
| 74 | + } |
| 75 | + |
| 76 | + if list, ok := metadata.Get(name); ok { |
| 77 | + return list[0], true |
| 78 | + } |
| 79 | + |
| 80 | + return "", false |
| 81 | +} |
| 82 | + |
| 83 | +// WithMetadata attaches metadata map to a context.Context |
| 84 | +func WithMetadata(ctx context.Context, headers Metadata) context.Context { |
| 85 | + return context.WithValue(ctx, metadataKey{}, headers) |
| 86 | +} |
0 commit comments