Merge pull request 'metadata: add Copy method, fix old methods' (#379) from md into v3
All checks were successful
test / test (push) Successful in 3m23s

Reviewed-on: #379
This commit is contained in:
Василий Толстов 2024-12-19 16:21:09 +03:00
commit 98981ba86c
2 changed files with 32 additions and 13 deletions

View File

@ -68,15 +68,7 @@ func (md Metadata) Iterator() *Iterator {
} }
func (md Metadata) MustGet(key string) string { func (md Metadata) MustGet(key string) string {
// fast path val, ok := md.Get(key)
val, ok := md[key]
if !ok {
// slow path
val, ok = md[textproto.CanonicalMIMEHeaderKey(key)]
if !ok {
val, ok = md[strings.ToLower(key)]
}
}
if !ok { if !ok {
panic("missing metadata key") 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 // Copy makes a copy of the metadata
func Copy(md Metadata, exclude ...string) Metadata { func Copy(md Metadata, exclude ...string) Metadata {
nmd := New(len(md)) nmd := New(len(md))
for key, val := range md { for k, v := range md {
nmd.Set(key, val) nmd[k] = v
} }
nmd.Del(exclude...) nmd.Del(exclude...)
return nmd return nmd
@ -147,7 +146,7 @@ func Merge(omd Metadata, mmd Metadata, overwrite bool) Metadata {
case ok && !overwrite: case ok && !overwrite:
continue continue
case val != "": case val != "":
nmd.Set(key, val) nmd[key] = val
case ok && val == "": case ok && val == "":
nmd.Del(key) nmd.Del(key)
} }
@ -161,6 +160,8 @@ func Pairs(kv ...string) (Metadata, bool) {
return nil, false return nil, false
} }
md := New(len(kv) / 2) 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 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"
)