-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
context.go
49 lines (37 loc) · 1.11 KB
/
context.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
40
41
42
43
44
45
46
47
48
49
package jsonrpc
import (
"context"
"github.com/goccy/go-json"
)
type (
requestIDKey struct{}
metadataIDKey struct{}
methodNameKey struct{}
)
// RequestID takes request id from context.
func RequestID(c context.Context) *json.RawMessage {
v, _ := c.Value(requestIDKey{}).(*json.RawMessage)
return v
}
// WithRequestID adds request id to context.
func WithRequestID(c context.Context, id *json.RawMessage) context.Context {
return context.WithValue(c, requestIDKey{}, id)
}
// GetMetadata takes jsonrpc metadata from context.
func GetMetadata(c context.Context) Metadata {
v, _ := c.Value(metadataIDKey{}).(Metadata)
return v
}
// WithMetadata adds jsonrpc metadata to context.
func WithMetadata(c context.Context, md Metadata) context.Context {
return context.WithValue(c, metadataIDKey{}, md)
}
// MethodName takes method name from context.
func MethodName(c context.Context) string {
v, _ := c.Value(methodNameKey{}).(string)
return v
}
// WithMethodName adds method name to context.
func WithMethodName(c context.Context, name string) context.Context {
return context.WithValue(c, methodNameKey{}, name)
}