-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.go
70 lines (57 loc) · 2.77 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
package protokit
import (
"context"
)
type contextKey string
const (
allFilesContextKey = contextKey("all_files")
fileContextKey = contextKey("file")
descriptorContextKey = contextKey("descriptor")
enumContextKey = contextKey("enum")
serviceContextKey = contextKey("service")
)
// ContextWithAllFiles returns a new context with the attached `AllFiles`
func ContextWithAllFiles(ctx context.Context, allFiles map[string]*PKFileDescriptor) context.Context {
return context.WithValue(ctx, allFilesContextKey, allFiles)
}
// AllFilesFromContext returns the `AllFiles` from the context and whether or not the key was found.
func AllFilesFromContext(ctx context.Context) (map[string]*PKFileDescriptor, bool) {
val, ok := ctx.Value(allFilesContextKey).(map[string]*PKFileDescriptor)
return val, ok
}
// ContextWithFileDescriptor returns a new context with the attached `PKFileDescriptor`
func ContextWithFileDescriptor(ctx context.Context, fd *PKFileDescriptor) context.Context {
return context.WithValue(ctx, fileContextKey, fd)
}
// FileDescriptorFromContext returns the `PKFileDescriptor` from the context and whether or not the key was found.
func FileDescriptorFromContext(ctx context.Context) (*PKFileDescriptor, bool) {
val, ok := ctx.Value(fileContextKey).(*PKFileDescriptor)
return val, ok
}
// ContextWithDescriptor returns a new context with the specified `PKDescriptor`
func ContextWithDescriptor(ctx context.Context, d *PKDescriptor) context.Context {
return context.WithValue(ctx, descriptorContextKey, d)
}
// DescriptorFromContext returns the associated `PKDescriptor` for the context and whether or not it was found
func DescriptorFromContext(ctx context.Context) (*PKDescriptor, bool) {
val, ok := ctx.Value(descriptorContextKey).(*PKDescriptor)
return val, ok
}
// ContextWithEnumDescriptor returns a new context with the specified `PKEnumDescriptor`
func ContextWithEnumDescriptor(ctx context.Context, d *PKEnumDescriptor) context.Context {
return context.WithValue(ctx, enumContextKey, d)
}
// EnumDescriptorFromContext returns the associated `PKEnumDescriptor` for the context and whether or not it was found
func EnumDescriptorFromContext(ctx context.Context) (*PKEnumDescriptor, bool) {
val, ok := ctx.Value(enumContextKey).(*PKEnumDescriptor)
return val, ok
}
// ContextWithServiceDescriptor returns a new context with `service`
func ContextWithServiceDescriptor(ctx context.Context, service *PKServiceDescriptor) context.Context {
return context.WithValue(ctx, serviceContextKey, service)
}
// ServiceDescriptorFromContext returns the `Service` from the context and whether or not the key was found.
func ServiceDescriptorFromContext(ctx context.Context) (*PKServiceDescriptor, bool) {
val, ok := ctx.Value(serviceContextKey).(*PKServiceDescriptor)
return val, ok
}