mirror of
https://github.com/go-kratos/kratos.git
synced 2025-01-07 23:02:12 +02:00
8d8cd8c8a8
* add middleware metadata * add transport header * add transport carrier Co-authored-by: longXboy <longxboy@gmail.com>
47 lines
1.4 KiB
Go
47 lines
1.4 KiB
Go
package metadata
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
)
|
|
|
|
type serverMetadataKey struct{}
|
|
|
|
// NewServerContext creates a new context with client md attached.
|
|
func NewServerContext(ctx context.Context, md Metadata) context.Context {
|
|
return context.WithValue(ctx, serverMetadataKey{}, md)
|
|
}
|
|
|
|
// FromServerContext returns the server metadata in ctx if it exists.
|
|
func FromServerContext(ctx context.Context) (Metadata, bool) {
|
|
md, ok := ctx.Value(serverMetadataKey{}).(Metadata)
|
|
return md, ok
|
|
}
|
|
|
|
type clientMetadataKey struct{}
|
|
|
|
// NewClientContext creates a new context with client md attached.
|
|
func NewClientContext(ctx context.Context, md Metadata) context.Context {
|
|
return context.WithValue(ctx, clientMetadataKey{}, md)
|
|
}
|
|
|
|
// FromClientContext returns the client metadata in ctx if it exists.
|
|
func FromClientContext(ctx context.Context) (Metadata, bool) {
|
|
md, ok := ctx.Value(clientMetadataKey{}).(Metadata)
|
|
return md, ok
|
|
}
|
|
|
|
// AppendToClientContext returns a new context with the provided kv merged
|
|
// with any existing metadata in the context.
|
|
func AppendToClientContext(ctx context.Context, kv ...string) context.Context {
|
|
if len(kv)%2 == 1 {
|
|
panic(fmt.Sprintf("metadata: AppendToOutgoingContext got an odd number of input pairs for metadata: %d", len(kv)))
|
|
}
|
|
md, _ := FromClientContext(ctx)
|
|
md = md.Clone()
|
|
for i := 0; i < len(kv); i += 2 {
|
|
md.Set(kv[i], kv[i+1])
|
|
}
|
|
return NewClientContext(ctx, md)
|
|
}
|