From 4cd55875c67ca4112815896439a968cfe1ceec73 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Fri, 13 Dec 2024 17:02:57 +0300 Subject: [PATCH] add Must*Context methods Signed-off-by: Vasiliy Tolstov --- metadata/context.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/metadata/context.go b/metadata/context.go index 294d1eda..1cc48425 100644 --- a/metadata/context.go +++ b/metadata/context.go @@ -11,6 +11,34 @@ type ( mdKey struct{} ) +// 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 +} + // FromIncomingContext returns metadata from incoming ctx // returned metadata shoud not be modified or race condition happens func FromIncomingContext(ctx context.Context) (Metadata, bool) {