From a5ef23117194747c206b62387e2a48dfac96c25c Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Tue, 29 Apr 2025 13:54:16 +0300 Subject: [PATCH] cleanup metadata Signed-off-by: Vasiliy Tolstov --- logger/slog/slog_test.go | 8 ++++---- metadata/metadata.go | 18 ------------------ metadata/metadata_test.go | 26 -------------------------- 3 files changed, 4 insertions(+), 48 deletions(-) diff --git a/logger/slog/slog_test.go b/logger/slog/slog_test.go index 031cc9fe..2beaa95f 100644 --- a/logger/slog/slog_test.go +++ b/logger/slog/slog_test.go @@ -406,7 +406,7 @@ func TestLogger(t *testing.T) { func Test_WithContextAttrFunc(t *testing.T) { loggerContextAttrFuncs := []logger.ContextAttrFunc{ func(ctx context.Context) []interface{} { - md, ok := metadata.FromIncomingContext(ctx) + md, ok := metadata.FromOutgoingContext(ctx) if !ok { return nil } @@ -425,7 +425,7 @@ func Test_WithContextAttrFunc(t *testing.T) { logger.DefaultContextAttrFuncs = append(logger.DefaultContextAttrFuncs, loggerContextAttrFuncs...) ctx := context.TODO() - ctx = metadata.AppendIncomingContext(ctx, "X-Request-Id", uuid.New().String(), + ctx = metadata.AppendOutgoingContext(ctx, "X-Request-Id", uuid.New().String(), "Source-Service", "Test-System") buf := bytes.NewBuffer(nil) @@ -445,9 +445,9 @@ func Test_WithContextAttrFunc(t *testing.T) { t.Fatalf("logger info, buf %s", buf.Bytes()) } buf.Reset() - imd, _ := metadata.FromIncomingContext(ctx) + omd, _ := metadata.FromOutgoingContext(ctx) l.Info(ctx, "test message1") - imd.Set("Source-Service", "Test-System2") + omd.Set("Source-Service", "Test-System2") l.Info(ctx, "test message2") // t.Logf("xxx %s", buf.Bytes()) diff --git a/metadata/metadata.go b/metadata/metadata.go index 77a7c247..59c4501e 100644 --- a/metadata/metadata.go +++ b/metadata/metadata.go @@ -218,24 +218,6 @@ func AppendContext(ctx context.Context, kv ...string) context.Context { return context.WithValue(ctx, metadataCurrentKey{}, rawMetadata{md: md.md, added: added}) } -// AppendIncomingContext returns a new context with the provided kv merged -// with any existing metadata in the context. Please refer to the documentation -// of Pairs for a description of kv. -func AppendIncomingContext(ctx context.Context, kv ...string) context.Context { - if len(kv)%2 == 1 { - panic(fmt.Sprintf("metadata: AppendIncomingContext got an odd number of input pairs for metadata: %d", len(kv))) - } - md, _ := ctx.Value(metadataIncomingKey{}).(rawMetadata) - added := make([][]string, len(md.added)+1) - copy(added, md.added) - kvCopy := make([]string, 0, len(kv)) - for i := 0; i < len(kv); i += 2 { - kvCopy = append(kvCopy, strings.ToLower(kv[i]), kv[i+1]) - } - added[len(added)-1] = kvCopy - return context.WithValue(ctx, metadataIncomingKey{}, rawMetadata{md: md.md, added: added}) -} - // AppendOutgoingContext returns a new context with the provided kv merged // with any existing metadata in the context. Please refer to the documentation // of Pairs for a description of kv. diff --git a/metadata/metadata_test.go b/metadata/metadata_test.go index ffbc05c1..507c08c9 100644 --- a/metadata/metadata_test.go +++ b/metadata/metadata_test.go @@ -59,18 +59,6 @@ func TestMetadataSetMultiple(t *testing.T) { } } -func TestAppend(t *testing.T) { - ctx := context.Background() - ctx = AppendIncomingContext(ctx, "key1", "val1", "key2", "val2") - md, ok := FromIncomingContext(ctx) - if !ok { - t.Fatal("metadata empty") - } - if v := md.Get("key1"); v == nil { - t.Fatal("key1 not found") - } -} - func TestPairs(t *testing.T) { md := Pairs("key1", "val1", "key2", "val2") if v := md.Get("key1"); v == nil { @@ -269,20 +257,6 @@ func TestNewOutgoingContext(t *testing.T) { } } -func TestAppendIncomingContext(t *testing.T) { - md := New(1) - md.Set("key1", "val1") - ctx := AppendIncomingContext(context.TODO(), "key2", "val2") - - nmd, ok := FromIncomingContext(ctx) - if nmd == nil || !ok { - t.Fatal("AppendIncomingContext not works") - } - if v := nmd.GetJoined("key2"); v != "val2" { - t.Fatal("AppendIncomingContext not works") - } -} - func TestAppendOutgoingContext(t *testing.T) { md := New(1) md.Set("key1", "val1")