Skip to content

Commit

Permalink
add metadata set method (micro#1232)
Browse files Browse the repository at this point in the history
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
  • Loading branch information
vtolstov committed Feb 21, 2020
1 parent 7e24c0c commit d1e25e7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
10 changes: 10 additions & 0 deletions metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ func Copy(md Metadata) Metadata {
return cmd
}

// Set add key with val to metadata
func Set(ctx context.Context, k, v string) context.Context {
md, ok := FromContext(ctx)
if !ok {
md = make(Metadata)
}
md[k] = v
return context.WithValue(ctx, metaKey{}, md)
}

// Get returns a single value from metadata in the context
func Get(ctx context.Context, key string) (string, bool) {
md, ok := FromContext(ctx)
Expand Down
12 changes: 12 additions & 0 deletions metadata/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@ import (
"testing"
)

func TestMetadataSet(t *testing.T) {
ctx := Set(context.TODO(), "Key", "val")

val, ok := Get(ctx, "Key")
if !ok {
t.Fatal("key Key not found")
}
if val != "val" {
t.Errorf("key Key with value val != %v", val)
}
}

func TestMetadataCopy(t *testing.T) {
md := Metadata{
"Foo": "bar",
Expand Down

0 comments on commit d1e25e7

Please sign in to comment.