2016-12-14 18:41:48 +03:00
|
|
|
// Package metadata is a way of defining message headers
|
2024-03-09 17:21:20 +03:00
|
|
|
package metadata
|
2016-01-28 20:55:28 +03:00
|
|
|
|
|
|
|
import (
|
2020-09-30 16:14:54 +03:00
|
|
|
"net/textproto"
|
2024-03-09 17:21:20 +03:00
|
|
|
"strings"
|
2016-01-28 20:55:28 +03:00
|
|
|
)
|
|
|
|
|
2021-07-23 12:03:18 +03:00
|
|
|
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"
|
2021-07-23 12:17:00 +03:00
|
|
|
// HeaderAuthorization specifies Authorization header
|
|
|
|
HeaderAuthorization = "Authorization"
|
2023-09-01 08:41:23 +03:00
|
|
|
// HeaderXRequestID specifies request id
|
|
|
|
HeaderXRequestID = "X-Request-Id"
|
2021-07-23 12:03:18 +03:00
|
|
|
)
|
2021-01-29 13:17:32 +03:00
|
|
|
|
2016-01-31 00:17:44 +03:00
|
|
|
// Metadata is our way of representing request headers internally.
|
2024-03-09 17:21:20 +03:00
|
|
|
type Metadata map[string][]string
|
2021-01-21 18:35:31 +03:00
|
|
|
|
2020-11-03 01:08:23 +03:00
|
|
|
// Get returns value from metadata by key
|
2020-03-26 21:50:00 +03:00
|
|
|
func (md Metadata) Get(key string) (string, bool) {
|
2020-09-30 16:14:54 +03:00
|
|
|
// fast path
|
2020-03-26 21:50:00 +03:00
|
|
|
val, ok := md[key]
|
2020-09-30 16:14:54 +03:00
|
|
|
if !ok {
|
|
|
|
// slow path
|
|
|
|
val, ok = md[textproto.CanonicalMIMEHeaderKey(key)]
|
2020-03-26 21:50:00 +03:00
|
|
|
}
|
2024-03-09 17:21:20 +03:00
|
|
|
return strings.Join(val, ","), ok
|
2020-03-26 21:50:00 +03:00
|
|
|
}
|
|
|
|
|
2020-11-03 01:08:23 +03:00
|
|
|
// Set is used to store value in metadata
|
2022-05-03 15:19:10 +03:00
|
|
|
func (md Metadata) Set(kv ...string) {
|
|
|
|
if len(kv)%2 == 1 {
|
|
|
|
kv = kv[:len(kv)-1]
|
|
|
|
}
|
|
|
|
for idx := 0; idx < len(kv); idx += 2 {
|
2024-03-09 17:21:20 +03:00
|
|
|
md[textproto.CanonicalMIMEHeaderKey(kv[idx])] = []string{kv[idx+1]}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append is used to append value in metadata
|
|
|
|
func (md Metadata) Append(k string, v ...string) {
|
|
|
|
if len(v) == 0 {
|
|
|
|
return
|
2022-05-03 15:19:10 +03:00
|
|
|
}
|
2024-03-09 17:21:20 +03:00
|
|
|
k = textproto.CanonicalMIMEHeaderKey(k)
|
|
|
|
md[k] = append(md[k], v...)
|
2020-04-12 13:17:23 +03:00
|
|
|
}
|
|
|
|
|
2020-11-03 01:08:23 +03:00
|
|
|
// Del is used to remove value from metadata
|
2022-05-03 15:19:10 +03:00
|
|
|
func (md Metadata) Del(keys ...string) {
|
|
|
|
for _, key := range keys {
|
|
|
|
// fast path
|
|
|
|
delete(md, key)
|
|
|
|
// slow path
|
|
|
|
delete(md, textproto.CanonicalMIMEHeaderKey(key))
|
|
|
|
}
|
2020-03-31 22:55:33 +03:00
|
|
|
}
|
|
|
|
|
2019-10-26 01:27:59 +03:00
|
|
|
// Copy makes a copy of the metadata
|
2024-03-09 23:53:04 +03:00
|
|
|
func Copy(md Metadata, exclude ...string) Metadata {
|
2024-03-09 17:21:20 +03:00
|
|
|
nmd := make(Metadata, len(md))
|
|
|
|
for k, v := range md {
|
|
|
|
nmd[k] = v
|
2019-01-17 12:40:49 +03:00
|
|
|
}
|
2024-03-09 23:53:04 +03:00
|
|
|
nmd.Del(exclude...)
|
2020-09-30 16:14:54 +03:00
|
|
|
return nmd
|
2019-01-17 12:40:49 +03:00
|
|
|
}
|
|
|
|
|
2020-10-12 17:17:59 +03:00
|
|
|
// New return new sized metadata
|
|
|
|
func New(size int) Metadata {
|
|
|
|
if size == 0 {
|
2024-03-09 17:21:20 +03:00
|
|
|
size = 2
|
2020-10-12 17:17:59 +03:00
|
|
|
}
|
|
|
|
return make(Metadata, size)
|
|
|
|
}
|
2021-01-23 00:09:07 +03:00
|
|
|
|
|
|
|
// Merge merges metadata to existing metadata, overwriting if specified
|
|
|
|
func Merge(omd Metadata, mmd Metadata, overwrite bool) Metadata {
|
|
|
|
nmd := Copy(omd)
|
|
|
|
for key, val := range mmd {
|
2024-03-09 17:21:20 +03:00
|
|
|
nval, ok := nmd[key]
|
2021-09-29 13:19:07 +03:00
|
|
|
switch {
|
2024-03-09 17:21:20 +03:00
|
|
|
case ok && overwrite:
|
|
|
|
nmd[key] = nval
|
|
|
|
continue
|
2021-09-29 13:19:07 +03:00
|
|
|
case ok && !overwrite:
|
|
|
|
continue
|
2024-03-09 17:21:20 +03:00
|
|
|
case !ok:
|
|
|
|
for _, v := range val {
|
|
|
|
if v != "" {
|
|
|
|
nval = append(nval, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nmd[key] = nval
|
2021-01-23 00:09:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nmd
|
|
|
|
}
|
2021-02-22 00:08:05 +03:00
|
|
|
|
2021-03-06 23:44:54 +03:00
|
|
|
// Pairs from which metadata created
|
2024-03-09 17:21:20 +03:00
|
|
|
func Pairs(kv ...string) Metadata {
|
2021-02-22 00:08:05 +03:00
|
|
|
if len(kv)%2 == 1 {
|
2024-03-09 17:21:20 +03:00
|
|
|
kv = kv[:len(kv)-1]
|
2021-02-22 00:08:05 +03:00
|
|
|
}
|
2024-03-09 17:21:20 +03:00
|
|
|
md := make(Metadata, len(kv)/2)
|
2022-05-03 15:19:10 +03:00
|
|
|
md.Set(kv...)
|
2024-03-09 17:21:20 +03:00
|
|
|
return md
|
2021-02-22 00:08:05 +03:00
|
|
|
}
|