Skip to content

Commit

Permalink
upper case the metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
asim committed Dec 31, 2019
1 parent 488dc31 commit 60ea537
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
22 changes: 20 additions & 2 deletions metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package metadata

import (
"context"
"strings"
)

type metaKey struct{}
Expand All @@ -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
Expand All @@ -57,5 +76,4 @@ func MergeContext(ctx context.Context, patchMd Metadata, overwrite bool) context
}
}
return context.WithValue(ctx, metaKey{}, cmd)

}
20 changes: 10 additions & 10 deletions metadata/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

func TestMetadataCopy(t *testing.T) {
md := Metadata{
"foo": "bar",
"Foo": "bar",
"bar": "baz",
}

Expand All @@ -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)
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit 60ea537

Please sign in to comment.