From 34d1587881c7645ad25b65fd841355f47bf7328b Mon Sep 17 00:00:00 2001 From: devstigneev Date: Fri, 6 Dec 2024 00:08:03 +0300 Subject: [PATCH] removed create empty out/ingoing metadata --- metadata/context.go | 17 +++-------------- metadata/metadata_test.go | 7 +++++++ 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/metadata/context.go b/metadata/context.go index 1406b30d..294d1eda 100644 --- a/metadata/context.go +++ b/metadata/context.go @@ -55,10 +55,7 @@ func NewContext(ctx context.Context, md Metadata) context.Context { if ctx == nil { ctx = context.Background() } - ctx = context.WithValue(ctx, mdKey{}, &rawMetadata{md}) - ctx = context.WithValue(ctx, mdIncomingKey{}, &rawMetadata{}) - ctx = context.WithValue(ctx, mdOutgoingKey{}, &rawMetadata{}) - return ctx + return context.WithValue(ctx, mdKey{}, &rawMetadata{md}) } // SetOutgoingContext modify outgoing context with given metadata @@ -90,11 +87,7 @@ func NewIncomingContext(ctx context.Context, md Metadata) context.Context { if ctx == nil { ctx = context.Background() } - ctx = context.WithValue(ctx, mdIncomingKey{}, &rawMetadata{md}) - if v, ok := ctx.Value(mdOutgoingKey{}).(*rawMetadata); !ok || v == nil { - ctx = context.WithValue(ctx, mdOutgoingKey{}, &rawMetadata{}) - } - return ctx + return context.WithValue(ctx, mdIncomingKey{}, &rawMetadata{md}) } // NewOutgoingContext creates a new context with outcoming metadata attached @@ -102,11 +95,7 @@ func NewOutgoingContext(ctx context.Context, md Metadata) context.Context { if ctx == nil { ctx = context.Background() } - ctx = context.WithValue(ctx, mdOutgoingKey{}, &rawMetadata{md}) - if v, ok := ctx.Value(mdIncomingKey{}).(*rawMetadata); !ok || v == nil { - ctx = context.WithValue(ctx, mdIncomingKey{}, &rawMetadata{}) - } - return ctx + return context.WithValue(ctx, mdOutgoingKey{}, &rawMetadata{md}) } // AppendOutgoingContext apends new md to context diff --git a/metadata/metadata_test.go b/metadata/metadata_test.go index 5aa41133..28cf93b7 100644 --- a/metadata/metadata_test.go +++ b/metadata/metadata_test.go @@ -80,6 +80,13 @@ func TestPassing(t *testing.T) { ctx = NewIncomingContext(ctx, md1) testCtx(ctx) md, ok := FromOutgoingContext(ctx) + if ok { + t.Fatalf("create outgoing context") + } + + ctx = NewOutgoingContext(ctx, New(1)) + testCtx(ctx) + md, ok = FromOutgoingContext(ctx) if !ok { t.Fatalf("missing metadata from outgoing context") }