upper case the metadata
This commit is contained in:
parent
488dc31743
commit
60ea537bbc
@ -3,6 +3,7 @@ package metadata
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type metaKey struct{}
|
||||
@ -27,14 +28,32 @@ func Get(ctx context.Context, key string) (string, bool) {
|
||||
if !ok {
|
||||
return "", ok
|
||||
}
|
||||
// attempt to get as is
|
||||
val, ok := md[key]
|
||||
if ok {
|
||||
return val, ok
|
||||
}
|
||||
|
||||
// attempt to get lower case
|
||||
val, ok = md[strings.Title(key)]
|
||||
|
||||
return val, ok
|
||||
}
|
||||
|
||||
// FromContext returns metadata from the given context
|
||||
func FromContext(ctx context.Context) (Metadata, bool) {
|
||||
md, ok := ctx.Value(metaKey{}).(Metadata)
|
||||
return md, ok
|
||||
if !ok {
|
||||
return nil, ok
|
||||
}
|
||||
|
||||
// capitalise all values
|
||||
newMD := make(map[string]string)
|
||||
for k, v := range md {
|
||||
newMD[strings.Title(k)] = v
|
||||
}
|
||||
|
||||
return newMD, ok
|
||||
}
|
||||
|
||||
// NewContext creates a new context with the given metadata
|
||||
@ -57,5 +76,4 @@ func MergeContext(ctx context.Context, patchMd Metadata, overwrite bool) context
|
||||
}
|
||||
}
|
||||
return context.WithValue(ctx, metaKey{}, cmd)
|
||||
|
||||
}
|
||||
|
@ -8,7 +8,7 @@ import (
|
||||
|
||||
func TestMetadataCopy(t *testing.T) {
|
||||
md := Metadata{
|
||||
"foo": "bar",
|
||||
"Foo": "bar",
|
||||
"bar": "baz",
|
||||
}
|
||||
|
||||
@ -23,7 +23,7 @@ func TestMetadataCopy(t *testing.T) {
|
||||
|
||||
func TestMetadataContext(t *testing.T) {
|
||||
md := Metadata{
|
||||
"foo": "bar",
|
||||
"Foo": "bar",
|
||||
}
|
||||
|
||||
ctx := NewContext(context.TODO(), md)
|
||||
@ -33,8 +33,8 @@ func TestMetadataContext(t *testing.T) {
|
||||
t.Errorf("Unexpected error retrieving metadata, got %t", ok)
|
||||
}
|
||||
|
||||
if emd["foo"] != md["foo"] {
|
||||
t.Errorf("Expected key: %s val: %s, got key: %s val: %s", "foo", md["foo"], "foo", emd["foo"])
|
||||
if emd["Foo"] != md["Foo"] {
|
||||
t.Errorf("Expected key: %s val: %s, got key: %s val: %s", "Foo", md["Foo"], "Foo", emd["Foo"])
|
||||
}
|
||||
|
||||
if i := len(emd); i != 1 {
|
||||
@ -56,20 +56,20 @@ func TestMergeContext(t *testing.T) {
|
||||
{
|
||||
name: "matching key, overwrite false",
|
||||
args: args{
|
||||
existing: Metadata{"foo": "bar", "sumo": "demo"},
|
||||
append: Metadata{"sumo": "demo2"},
|
||||
existing: Metadata{"Foo": "bar", "Sumo": "demo"},
|
||||
append: Metadata{"Sumo": "demo2"},
|
||||
overwrite: false,
|
||||
},
|
||||
want: Metadata{"foo": "bar", "sumo": "demo"},
|
||||
want: Metadata{"Foo": "bar", "Sumo": "demo"},
|
||||
},
|
||||
{
|
||||
name: "matching key, overwrite true",
|
||||
args: args{
|
||||
existing: Metadata{"foo": "bar", "sumo": "demo"},
|
||||
append: Metadata{"sumo": "demo2"},
|
||||
existing: Metadata{"Foo": "bar", "Sumo": "demo"},
|
||||
append: Metadata{"Sumo": "demo2"},
|
||||
overwrite: true,
|
||||
},
|
||||
want: Metadata{"foo": "bar", "sumo": "demo2"},
|
||||
want: Metadata{"Foo": "bar", "Sumo": "demo2"},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
|
Loading…
Reference in New Issue
Block a user