metadata: add Copy method, fix old methods
Some checks failed
lint / lint (pull_request) Failing after 55s
test / test (pull_request) Successful in 3m33s

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2024-12-19 16:03:23 +03:00
parent 69a44eb190
commit 0b190997b1
2 changed files with 33 additions and 14 deletions

View File

@ -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
}

18
semconv/metadata.go Normal file
View File

@ -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"
)