-
Notifications
You must be signed in to change notification settings - Fork 46
/
context.go
43 lines (32 loc) · 1.27 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
package candishared
import "context"
// ContextKey represent Key of all context
type ContextKey string
const (
// ContextKeyHTTPHeader context key
ContextKeyHTTPHeader ContextKey = "httpHeader"
// ContextKeyTaskQueueRetry context key
ContextKeyTaskQueueRetry ContextKey = "taskQueueRetry"
// ContextKeyTokenClaim context key
ContextKeyTokenClaim ContextKey = "tokenClaim"
// ContextKeyWorkerKey context key
ContextKeyWorkerKey ContextKey = "workerKey"
// ContextKeySQLTransaction context key
ContextKeySQLTransaction ContextKey = "sqltx"
)
// SetToContext will set context with specific key
func SetToContext(ctx context.Context, key ContextKey, value interface{}) context.Context {
return context.WithValue(ctx, key, value)
}
// GetValueFromContext will get context with specific key
func GetValueFromContext(ctx context.Context, key ContextKey) interface{} {
return ctx.Value(key)
}
// ParseTokenClaimFromContext parse token claim from given context
func ParseTokenClaimFromContext(ctx context.Context) *TokenClaim {
return GetValueFromContext(ctx, ContextKeyTokenClaim).(*TokenClaim)
}
// ParseWorkerKeyFromContext parse token claim from given context
func ParseWorkerKeyFromContext(ctx context.Context) []byte {
return GetValueFromContext(ctx, ContextKeyWorkerKey).([]byte)
}