-
-
Notifications
You must be signed in to change notification settings - Fork 564
/
Copy pathhelpers.go
39 lines (33 loc) Β· 898 Bytes
/
helpers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package middleware
import (
"context"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
)
type (
// WrappedServerStream overrides the Context() method of the
// grpc.ServerStream interface.
WrappedServerStream struct {
grpc.ServerStream
ctx context.Context
}
)
// NewWrappedServerStream returns a new wrapped grpc ServerStream.
func NewWrappedServerStream(ctx context.Context, ss grpc.ServerStream) *WrappedServerStream {
return &WrappedServerStream{
ctx: ctx,
ServerStream: ss,
}
}
// Context returns the context for the server stream.
func (w *WrappedServerStream) Context() context.Context {
return w.ctx
}
// MetadataValue returns the first value for the given metadata key if
// key exists, else returns an empty string.
func MetadataValue(md metadata.MD, key string) string {
if vals := md.Get(key); len(vals) > 0 {
return vals[0]
}
return ""
}