cleanup metadata
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
		| @@ -406,7 +406,7 @@ func TestLogger(t *testing.T) { | |||||||
| func Test_WithContextAttrFunc(t *testing.T) { | func Test_WithContextAttrFunc(t *testing.T) { | ||||||
| 	loggerContextAttrFuncs := []logger.ContextAttrFunc{ | 	loggerContextAttrFuncs := []logger.ContextAttrFunc{ | ||||||
| 		func(ctx context.Context) []interface{} { | 		func(ctx context.Context) []interface{} { | ||||||
| 			md, ok := metadata.FromIncomingContext(ctx) | 			md, ok := metadata.FromOutgoingContext(ctx) | ||||||
| 			if !ok { | 			if !ok { | ||||||
| 				return nil | 				return nil | ||||||
| 			} | 			} | ||||||
| @@ -425,7 +425,7 @@ func Test_WithContextAttrFunc(t *testing.T) { | |||||||
| 	logger.DefaultContextAttrFuncs = append(logger.DefaultContextAttrFuncs, loggerContextAttrFuncs...) | 	logger.DefaultContextAttrFuncs = append(logger.DefaultContextAttrFuncs, loggerContextAttrFuncs...) | ||||||
|  |  | ||||||
| 	ctx := context.TODO() | 	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") | 		"Source-Service", "Test-System") | ||||||
|  |  | ||||||
| 	buf := bytes.NewBuffer(nil) | 	buf := bytes.NewBuffer(nil) | ||||||
| @@ -445,9 +445,9 @@ func Test_WithContextAttrFunc(t *testing.T) { | |||||||
| 		t.Fatalf("logger info, buf %s", buf.Bytes()) | 		t.Fatalf("logger info, buf %s", buf.Bytes()) | ||||||
| 	} | 	} | ||||||
| 	buf.Reset() | 	buf.Reset() | ||||||
| 	imd, _ := metadata.FromIncomingContext(ctx) | 	omd, _ := metadata.FromOutgoingContext(ctx) | ||||||
| 	l.Info(ctx, "test message1") | 	l.Info(ctx, "test message1") | ||||||
| 	imd.Set("Source-Service", "Test-System2") | 	omd.Set("Source-Service", "Test-System2") | ||||||
| 	l.Info(ctx, "test message2") | 	l.Info(ctx, "test message2") | ||||||
|  |  | ||||||
| 	// t.Logf("xxx %s", buf.Bytes()) | 	// t.Logf("xxx %s", buf.Bytes()) | ||||||
|   | |||||||
| @@ -218,24 +218,6 @@ func AppendContext(ctx context.Context, kv ...string) context.Context { | |||||||
| 	return context.WithValue(ctx, metadataCurrentKey{}, rawMetadata{md: md.md, added: added}) | 	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 | // AppendOutgoingContext returns a new context with the provided kv merged | ||||||
| // with any existing metadata in the context. Please refer to the documentation | // with any existing metadata in the context. Please refer to the documentation | ||||||
| // of Pairs for a description of kv. | // of Pairs for a description of kv. | ||||||
|   | |||||||
| @@ -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) { | func TestPairs(t *testing.T) { | ||||||
| 	md := Pairs("key1", "val1", "key2", "val2") | 	md := Pairs("key1", "val1", "key2", "val2") | ||||||
| 	if v := md.Get("key1"); v == nil { | 	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) { | func TestAppendOutgoingContext(t *testing.T) { | ||||||
| 	md := New(1) | 	md := New(1) | ||||||
| 	md.Set("key1", "val1") | 	md.Set("key1", "val1") | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user