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
|
|
|
|
}
|
2024-03-09 17:21:20 +03:00
|
|
|
md, ok := ctx.Value(mdIncomingKey{}).(Metadata)
|
|
|
|
if !ok || md == nil {
|
2021-02-09 12:46:14 +03:00
|
|
|
return nil, false
|
|
|
|
}
|
2024-03-09 17:21:20 +03:00
|
|
|
return 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
|
|
|
|
}
|
2024-03-09 17:21:20 +03:00
|
|
|
md, ok := ctx.Value(mdOutgoingKey{}).(Metadata)
|
|
|
|
if !ok || md == nil {
|
2021-02-09 12:46:14 +03:00
|
|
|
return nil, false
|
|
|
|
}
|
2024-03-09 17:21:20 +03:00
|
|
|
return 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
|
2021-01-21 18:35:31 +03:00
|
|
|
func FromContext(ctx context.Context) (Metadata, bool) {
|
|
|
|
if ctx == nil {
|
|
|
|
return nil, false
|
|
|
|
}
|
2024-03-09 17:21:20 +03:00
|
|
|
md, ok := ctx.Value(mdKey{}).(Metadata)
|
|
|
|
if !ok || md == nil {
|
2021-02-09 12:46:14 +03:00
|
|
|
return nil, false
|
|
|
|
}
|
2024-03-09 17:21:20 +03:00
|
|
|
return md, ok
|
2021-01-21 18:35:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewContext creates a new context with the given metadata
|
|
|
|
func NewContext(ctx context.Context, md Metadata) context.Context {
|
|
|
|
if ctx == nil {
|
|
|
|
ctx = context.Background()
|
|
|
|
}
|
2024-03-09 17:21:20 +03:00
|
|
|
ctx = context.WithValue(ctx, mdKey{}, md)
|
2021-02-09 12:46:14 +03:00
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
}
|
2024-03-09 17:21:20 +03:00
|
|
|
ctx = context.WithValue(ctx, mdIncomingKey{}, md)
|
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
|
|
|
}
|
2024-03-09 17:21:20 +03:00
|
|
|
ctx = context.WithValue(ctx, mdOutgoingKey{}, md)
|
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 {
|
2024-03-09 17:21:20 +03:00
|
|
|
md := Pairs(kv...)
|
2021-02-21 23:54:59 +03:00
|
|
|
omd, ok := FromOutgoingContext(ctx)
|
|
|
|
if !ok {
|
|
|
|
return NewOutgoingContext(ctx, md)
|
|
|
|
}
|
2024-03-09 17:21:20 +03:00
|
|
|
nmd := Merge(omd, md, true)
|
|
|
|
return NewOutgoingContext(ctx, nmd)
|
2021-02-21 23:54:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// AppendIncomingContext apends new md to context
|
2021-02-22 00:08:05 +03:00
|
|
|
func AppendIncomingContext(ctx context.Context, kv ...string) context.Context {
|
2024-03-09 17:21:20 +03:00
|
|
|
md := Pairs(kv...)
|
2021-02-21 23:54:59 +03:00
|
|
|
omd, ok := FromIncomingContext(ctx)
|
|
|
|
if !ok {
|
|
|
|
return NewIncomingContext(ctx, md)
|
|
|
|
}
|
2024-03-09 17:21:20 +03:00
|
|
|
nmd := Merge(omd, md, true)
|
|
|
|
return NewIncomingContext(ctx, nmd)
|
2021-02-21 23:54:59 +03:00
|
|
|
}
|