-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
87 lines (67 loc) · 2.3 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package graphify
import (
"context"
adminv1 "github.com/amaury95/graphify/models/domain/admin/v1"
)
/* Admin */
type adminKey struct{}
// ContextWithAdmin ...
func ContextWithAdmin(parent context.Context, admin *adminv1.Admin) context.Context {
return context.WithValue(parent, adminKey{}, admin)
}
// AdminFromContext ...
func AdminFromContext(ctx context.Context) (value *adminv1.Admin, found bool) {
value, found = ctx.Value(adminKey{}).(*adminv1.Admin)
return
}
/* IConnection */
type connectionKey struct{}
// ContextWithConnection ...
func ContextWithConnection(parent context.Context, conn IConnection) context.Context {
return context.WithValue(parent, connectionKey{}, conn)
}
// ConnectionFromContext ...
func ConnectionFromContext(ctx context.Context) (value IConnection, found bool) {
value, found = ctx.Value(connectionKey{}).(IConnection)
return
}
/* IObserver */
type observerKey struct{}
// ContextWithObserver ...
func ContextWithObserver(parent context.Context, observer IObserver[Topic]) context.Context {
return context.WithValue(parent, observerKey{}, observer)
}
// ObserverFromContext ...
func ObserverFromContext(ctx context.Context) (value IObserver[Topic], found bool) {
value, found = ctx.Value(observerKey{}).(IObserver[Topic])
return
}
/* IStorage */
type storageKey struct{}
func ContextWithStorage(parent context.Context, storage IFileStorage) context.Context {
return context.WithValue(parent, storageKey{}, storage)
}
func StorageFromContext(ctx context.Context) (value IFileStorage, found bool) {
value, found = ctx.Value(storageKey{}).(IFileStorage)
return
}
/* Development Environment (Set if env is in development, otherwise assumes production) */
type developmentKey struct{}
// DevelopmentContext ...
func DevelopmentContext(parent context.Context) context.Context {
return context.WithValue(parent, developmentKey{}, true)
}
// IsDevelopmentContext ...
func IsDevelopmentContext(ctx context.Context) (value bool) {
value = ctx.Value(developmentKey{}).(bool)
return
}
/* Secret Key */
type secretKey struct{}
func ContextWithSecret(parent context.Context, secret []byte) context.Context {
return context.WithValue(parent, secretKey{}, secret)
}
func SecretFromContext(ctx context.Context) (value []byte, found bool) {
value, found = ctx.Value(secretKey{}).([]byte)
return
}