2021-01-21 18:35:31 +03:00
|
|
|
// Package metadata is a way of defining message headers
|
|
|
|
package metadata
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
)
|
|
|
|
|
2021-04-27 08:32:47 +03:00
|
|
|
type (
|
|
|
|
mdIncomingKey struct{}
|
|
|
|
mdOutgoingKey struct{}
|
|
|
|
mdKey struct{}
|
|
|
|
)
|
2021-02-09 01:08:45 +03:00
|
|
|
|
|
|
|
// 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})
|
2021-03-01 13:00:11 +03:00
|
|
|
if v, ok := ctx.Value(mdOutgoingKey{}).(*rawMetadata); !ok || v == nil {
|
|
|
|
ctx = context.WithValue(ctx, mdOutgoingKey{}, &rawMetadata{})
|
|
|
|
}
|
2021-02-09 12:46:14 +03:00
|
|
|
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})
|
2021-03-01 13:00:11 +03:00
|
|
|
if v, ok := ctx.Value(mdIncomingKey{}).(*rawMetadata); !ok || v == nil {
|
|
|
|
ctx = context.WithValue(ctx, mdIncomingKey{}, &rawMetadata{})
|
|
|
|
}
|
2021-02-09 12:46:14 +03:00
|
|
|
return ctx
|
2021-01-21 18:35:31 +03:00
|
|
|
}
|
2021-02-21 23:54:59 +03:00
|
|
|
|
|
|
|
// AppendOutgoingContext apends new md to context
|
2021-02-22 00:08:05 +03:00
|
|
|
func AppendOutgoingContext(ctx context.Context, kv ...string) context.Context {
|
|
|
|
md, ok := Pairs(kv...)
|
|
|
|
if !ok {
|
|
|
|
return ctx
|
|
|
|
}
|
2021-02-21 23:54:59 +03:00
|
|
|
omd, ok := FromOutgoingContext(ctx)
|
|
|
|
if !ok {
|
|
|
|
return NewOutgoingContext(ctx, md)
|
|
|
|
}
|
|
|
|
for k, v := range md {
|
2023-02-11 01:04:43 +03:00
|
|
|
omd[k] = v
|
2021-02-21 23:54:59 +03:00
|
|
|
}
|
|
|
|
return NewOutgoingContext(ctx, omd)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AppendIncomingContext apends new md to context
|
2021-02-22 00:08:05 +03:00
|
|
|
func AppendIncomingContext(ctx context.Context, kv ...string) context.Context {
|
|
|
|
md, ok := Pairs(kv...)
|
|
|
|
if !ok {
|
|
|
|
return ctx
|
|
|
|
}
|
2021-02-21 23:54:59 +03:00
|
|
|
omd, ok := FromIncomingContext(ctx)
|
|
|
|
if !ok {
|
|
|
|
return NewIncomingContext(ctx, md)
|
|
|
|
}
|
|
|
|
for k, v := range md {
|
2023-02-11 01:04:43 +03:00
|
|
|
omd[k] = v
|
2021-02-21 23:54:59 +03:00
|
|
|
}
|
|
|
|
return NewIncomingContext(ctx, omd)
|
|
|
|
}
|