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
|
|
|
|
2024-12-13 17:02:57 +03:00
|
|
|
// MustIncomingContext returns metadata from incoming ctx
|
|
|
|
// returned metadata shoud not be modified or race condition happens.
|
|
|
|
// If metadata not exists panics.
|
|
|
|
func MustIncomingContext(ctx context.Context) Metadata {
|
|
|
|
if ctx == nil {
|
|
|
|
panic("missing metadata")
|
|
|
|
}
|
|
|
|
md, ok := ctx.Value(mdIncomingKey{}).(*rawMetadata)
|
|
|
|
if !ok {
|
|
|
|
panic("missing metadata")
|
|
|
|
}
|
|
|
|
return md.md
|
|
|
|
}
|
|
|
|
|
|
|
|
// MustOutgoingContext returns metadata from outgoing ctx
|
|
|
|
// returned metadata shoud not be modified or race condition happens.
|
|
|
|
// If metadata not exists panics.
|
|
|
|
func MustOutgoingContext(ctx context.Context) Metadata {
|
|
|
|
if ctx == nil {
|
|
|
|
panic("missing metadata")
|
|
|
|
}
|
|
|
|
md, ok := ctx.Value(mdOutgoingKey{}).(*rawMetadata)
|
|
|
|
if !ok {
|
|
|
|
panic("missing metadata")
|
|
|
|
}
|
|
|
|
return md.md
|
|
|
|
}
|
|
|
|
|
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
|
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
|
|
|
|
func NewContext(ctx context.Context, md Metadata) context.Context {
|
|
|
|
if ctx == nil {
|
|
|
|
ctx = context.Background()
|
|
|
|
}
|
2024-12-06 00:08:03 +03:00
|
|
|
return context.WithValue(ctx, mdKey{}, &rawMetadata{md})
|
2021-02-09 12:46:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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()
|
|
|
|
}
|
2024-12-06 00:08:03 +03:00
|
|
|
return context.WithValue(ctx, mdIncomingKey{}, &rawMetadata{md})
|
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-12-06 00:08:03 +03:00
|
|
|
return context.WithValue(ctx, mdOutgoingKey{}, &rawMetadata{md})
|
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 {
|
|
|
|
omd.Set(k, v)
|
|
|
|
}
|
|
|
|
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 {
|
|
|
|
omd.Set(k, v)
|
|
|
|
}
|
|
|
|
return NewIncomingContext(ctx, omd)
|
|
|
|
}
|