-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.go
48 lines (37 loc) · 1.23 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
package yeahapi
import "context"
type contextKey int
const (
sessionContextKey = contextKey(iota + 1)
clientContextKey
localizerContextKey
flashContextKey
)
func NewContextWithLocalizer(ctx context.Context, localizer *Localizer) context.Context {
return context.WithValue(ctx, localizerContextKey, localizer)
}
func NewContextWithClient(ctx context.Context, client *Client) context.Context {
return context.WithValue(ctx, clientContextKey, client)
}
func NewContextWithSession(ctx context.Context, session *Session) context.Context {
return context.WithValue(ctx, sessionContextKey, session)
}
func NewContextWithFlash(ctx context.Context, flash Flash) context.Context {
return context.WithValue(ctx, flashContextKey, flash)
}
func LocalizerFromContext(ctx context.Context) *Localizer {
localizer, _ := ctx.Value(localizerContextKey).(*Localizer)
return localizer
}
func SessionFromContext(ctx context.Context) *Session {
session, _ := ctx.Value(sessionContextKey).(*Session)
return session
}
func ClientFromContext(ctx context.Context) *Client {
client, _ := ctx.Value(clientContextKey).(*Client)
return client
}
func FlashFromContext(ctx context.Context) Flash {
message, _ := ctx.Value(flashContextKey).(Flash)
return message
}