From 0b190997b157da4b59278e9667fe732be2682060 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Thu, 19 Dec 2024 16:03:23 +0300 Subject: [PATCH] metadata: add Copy method, fix old methods Signed-off-by: Vasiliy Tolstov --- metadata/metadata.go | 29 +++++++++++++++-------------- semconv/metadata.go | 18 ++++++++++++++++++ 2 files changed, 33 insertions(+), 14 deletions(-) create mode 100644 semconv/metadata.go diff --git a/metadata/metadata.go b/metadata/metadata.go index 2e9582d5..958f5d48 100644 --- a/metadata/metadata.go +++ b/metadata/metadata.go @@ -9,7 +9,7 @@ import ( var ( // HeaderTopic is the header name that contains topic name - HeaderTopic = "Micro-Topic" + // HeaderTopic = "Micro-Topic" // HeaderContentType specifies content type of message HeaderContentType = "Content-Type" // HeaderEndpoint specifies endpoint in service @@ -68,15 +68,7 @@ func (md Metadata) Iterator() *Iterator { } 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)] - } - } + val, ok := md.Get(key) if !ok { panic("missing metadata key") } @@ -119,11 +111,18 @@ func (md Metadata) Del(keys ...string) { } } +// Copy makes a copy of the metadata +func (md Metadata) CopyTo(dst Metadata) { + for k, v := range md { + dst[k] = v + } +} + // Copy makes a copy of the metadata func Copy(md Metadata, exclude ...string) Metadata { nmd := New(len(md)) - for key, val := range md { - nmd.Set(key, val) + for k, v := range md { + nmd[k] = v } nmd.Del(exclude...) return nmd @@ -147,7 +146,7 @@ func Merge(omd Metadata, mmd Metadata, overwrite bool) Metadata { case ok && !overwrite: continue case val != "": - nmd.Set(key, val) + nmd[key] = val case ok && val == "": nmd.Del(key) } @@ -161,6 +160,8 @@ func Pairs(kv ...string) (Metadata, bool) { return nil, false } md := New(len(kv) / 2) - md.Set(kv...) + for idx := 0; idx < len(kv); idx += 2 { + md[kv[idx]] = kv[idx+1] + } return md, true } diff --git a/semconv/metadata.go b/semconv/metadata.go new file mode 100644 index 00000000..cff6dabd --- /dev/null +++ b/semconv/metadata.go @@ -0,0 +1,18 @@ +package semconv + +var ( + // HeaderTopic is the header name that contains topic name + HeaderTopic = "Micro-Topic" + // HeaderContentType specifies content type of message + HeaderContentType = "Content-Type" + // HeaderEndpoint specifies endpoint in service + HeaderEndpoint = "Micro-Endpoint" + // HeaderService specifies service + HeaderService = "Micro-Service" + // HeaderTimeout specifies timeout of operation + HeaderTimeout = "Micro-Timeout" + // HeaderAuthorization specifies Authorization header + HeaderAuthorization = "Authorization" + // HeaderXRequestID specifies request id + HeaderXRequestID = "X-Request-Id" +)