-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathmetadata.go
More file actions
32 lines (26 loc) · 885 Bytes
/
metadata.go
File metadata and controls
32 lines (26 loc) · 885 Bytes
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
package typhon
import "context"
type metadataKey struct{}
// Metadata provides a transport agnostic way to pass metadata with Typhon.
// It aligns to the interface of Go's default HTTP header type for convenience.
type Metadata map[string][]string
// NewMetadata creates a metadata struct from a map of strings.
func NewMetadata(data map[string]string) Metadata {
meta := make(Metadata, len(data))
for k, v := range data {
meta[k] = []string{v}
}
return meta
}
// AppendMetadataToContext sets the metadata on the context.
func AppendMetadataToContext(ctx context.Context, md Metadata) context.Context {
return context.WithValue(ctx, metadataKey{}, md)
}
// MetadataFromContext retrieves the metadata from the context.
func MetadataFromContext(ctx context.Context) Metadata {
meta, ok := ctx.Value(metadataKey{}).(Metadata)
if !ok {
return Metadata{}
}
return meta
}