add Must*Context methods
All checks were successful
test / test (push) Successful in 11m55s

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2024-12-13 17:02:57 +03:00
parent a7896cc728
commit 4cd55875c6

View File

@ -11,6 +11,34 @@ type (
mdKey struct{} 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 // FromIncomingContext returns metadata from incoming ctx
// returned metadata shoud not be modified or race condition happens // returned metadata shoud not be modified or race condition happens
func FromIncomingContext(ctx context.Context) (Metadata, bool) { func FromIncomingContext(ctx context.Context) (Metadata, bool) {