removed create empty out/ingoing metadata
Some checks failed
lint / lint (pull_request) Has been cancelled
pr / test (pull_request) Has been cancelled

This commit is contained in:
Денис Евстигнеев 2024-12-06 00:08:03 +03:00
parent bf4143cde5
commit 34d1587881
2 changed files with 10 additions and 14 deletions

View File

@ -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

View File

@ -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")
}