2021-01-21 18:35:31 +03:00
|
|
|
// Package metadata is a way of defining message headers
|
|
|
|
package metadata
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
)
|
|
|
|
|
2021-02-09 01:08:45 +03:00
|
|
|
type mdIncomingKey struct{}
|
|
|
|
type mdOutgoingKey struct{}
|
|
|
|
type mdKey struct{}
|
|
|
|
|
|
|
|
// FromIncomingContext returns metadata from incoming ctx
|
|
|
|
// returned metadata shoud not be modified or race condition happens
|
|
|
|
func FromIncomingContext(ctx context.Context) (Metadata, bool) {
|
|
|
|
if ctx == nil {
|
|
|
|
return nil, false
|
|
|
|
}
|
2021-02-09 12:46:14 +03:00
|
|
|
md, ok := ctx.Value(mdIncomingKey{}).(*rawMetadata)
|
2021-02-12 17:10:35 +03:00
|
|
|
if !ok || md.md == nil {
|
2021-02-09 12:46:14 +03:00
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
return md.md, ok
|
2021-02-09 01:08:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// FromOutgoingContext returns metadata from outgoing ctx
|
|
|
|
// returned metadata shoud not be modified or race condition happens
|
|
|
|
func FromOutgoingContext(ctx context.Context) (Metadata, bool) {
|
|
|
|
if ctx == nil {
|
|
|
|
return nil, false
|
|
|
|
}
|
2021-02-09 12:46:14 +03:00
|
|
|
md, ok := ctx.Value(mdOutgoingKey{}).(*rawMetadata)
|
2021-02-12 17:10:35 +03:00
|
|
|
if !ok || md.md == nil {
|
2021-02-09 12:46:14 +03:00
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
return md.md, ok
|
2021-02-09 01:08:45 +03:00
|
|
|
}
|
|
|
|
|
2021-01-21 18:35:31 +03:00
|
|
|
// FromContext returns metadata from the given context
|
2021-02-09 01:08:45 +03:00
|
|
|
// returned metadata shoud not be modified or race condition happens
|
|
|
|
//
|
|
|
|
// Deprecated: use FromIncomingContext or FromOutgoingContext
|
2021-01-21 18:35:31 +03:00
|
|
|
func FromContext(ctx context.Context) (Metadata, bool) {
|
|
|
|
if ctx == nil {
|
|
|
|
return nil, false
|
|
|
|
}
|
2021-02-09 12:46:14 +03:00
|
|
|
md, ok := ctx.Value(mdKey{}).(*rawMetadata)
|
2021-02-12 17:10:35 +03:00
|
|
|
if !ok || md.md == nil {
|
2021-02-09 12:46:14 +03:00
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
return md.md, ok
|
2021-01-21 18:35:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewContext creates a new context with the given metadata
|
2021-02-09 01:08:45 +03:00
|
|
|
//
|
|
|
|
// Deprecated: use NewIncomingContext or NewOutgoingContext
|
2021-01-21 18:35:31 +03:00
|
|
|
func NewContext(ctx context.Context, md Metadata) context.Context {
|
|
|
|
if ctx == nil {
|
|
|
|
ctx = context.Background()
|
|
|
|
}
|
2021-02-09 12:46:14 +03:00
|
|
|
ctx = context.WithValue(ctx, mdKey{}, &rawMetadata{md})
|
|
|
|
ctx = context.WithValue(ctx, mdIncomingKey{}, &rawMetadata{})
|
|
|
|
ctx = context.WithValue(ctx, mdOutgoingKey{}, &rawMetadata{})
|
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetOutgoingContext modify outgoing context with given metadata
|
|
|
|
func SetOutgoingContext(ctx context.Context, md Metadata) bool {
|
|
|
|
if ctx == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if omd, ok := ctx.Value(mdOutgoingKey{}).(*rawMetadata); ok {
|
|
|
|
omd.md = md
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetIncomingContext modify incoming context with given metadata
|
|
|
|
func SetIncomingContext(ctx context.Context, md Metadata) bool {
|
|
|
|
if ctx == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if omd, ok := ctx.Value(mdIncomingKey{}).(*rawMetadata); ok {
|
|
|
|
omd.md = md
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
2021-01-21 18:35:31 +03:00
|
|
|
}
|
|
|
|
|
2021-02-09 01:08:45 +03:00
|
|
|
// NewIncomingContext creates a new context with incoming metadata attached
|
|
|
|
func NewIncomingContext(ctx context.Context, md Metadata) context.Context {
|
2021-01-21 18:35:31 +03:00
|
|
|
if ctx == nil {
|
|
|
|
ctx = context.Background()
|
|
|
|
}
|
2021-02-09 12:46:14 +03:00
|
|
|
ctx = context.WithValue(ctx, mdIncomingKey{}, &rawMetadata{md})
|
|
|
|
ctx = context.WithValue(ctx, mdOutgoingKey{}, &rawMetadata{})
|
|
|
|
return ctx
|
2021-02-09 01:08:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewOutgoingContext creates a new context with outcoming metadata attached
|
|
|
|
func NewOutgoingContext(ctx context.Context, md Metadata) context.Context {
|
|
|
|
if ctx == nil {
|
|
|
|
ctx = context.Background()
|
2021-01-21 18:35:31 +03:00
|
|
|
}
|
2021-02-09 12:46:14 +03:00
|
|
|
ctx = context.WithValue(ctx, mdOutgoingKey{}, &rawMetadata{md})
|
|
|
|
ctx = context.WithValue(ctx, mdIncomingKey{}, &rawMetadata{})
|
|
|
|
return ctx
|
2021-01-21 18:35:31 +03:00
|
|
|
}
|
2021-02-21 23:54:59 +03:00
|
|
|
|
|
|
|
// AppendOutgoingContext apends new md to context
|
|
|
|
func AppendOutgoingContext(ctx context.Context, md Metadata) context.Context {
|
|
|
|
omd, ok := FromOutgoingContext(ctx)
|
|
|
|
if !ok {
|
|
|
|
return NewOutgoingContext(ctx, md)
|
|
|
|
}
|
|
|
|
for k, v := range md {
|
|
|
|
omd.Set(k, v)
|
|
|
|
}
|
|
|
|
return NewOutgoingContext(ctx, omd)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AppendIncomingContext apends new md to context
|
|
|
|
func AppendIncomingContext(ctx context.Context, md Metadata) context.Context {
|
|
|
|
omd, ok := FromIncomingContext(ctx)
|
|
|
|
if !ok {
|
|
|
|
return NewIncomingContext(ctx, md)
|
|
|
|
}
|
|
|
|
for k, v := range md {
|
|
|
|
omd.Set(k, v)
|
|
|
|
}
|
|
|
|
return NewIncomingContext(ctx, omd)
|
|
|
|
}
|