From 19b04fe070e2b5dbc22a12fc119349ee57bb3808 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Sun, 15 Dec 2024 23:45:17 +0300 Subject: [PATCH] metadata: add MustGet func Signed-off-by: Vasiliy Tolstov --- metadata/metadata.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/metadata/metadata.go b/metadata/metadata.go index 46744b9a..2e9582d5 100644 --- a/metadata/metadata.go +++ b/metadata/metadata.go @@ -67,6 +67,22 @@ func (md Metadata) Iterator() *Iterator { return iter } +func (md Metadata) MustGet(key string) string { + // fast path + val, ok := md[key] + if !ok { + // slow path + val, ok = md[textproto.CanonicalMIMEHeaderKey(key)] + if !ok { + val, ok = md[strings.ToLower(key)] + } + } + if !ok { + panic("missing metadata key") + } + return val +} + // Get returns value from metadata by key func (md Metadata) Get(key string) (string, bool) { // fast path