File: context.go

package info (click to toggle)
golang-github-pseudomuto-protokit 0.2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 320 kB
  • sloc: makefile: 61
file content (58 lines) | stat: -rw-r--r-- 2,232 bytes parent folder | download | duplicates (2)
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
package protokit

import (
	"context"
)

type contextKey string

const (
	fileContextKey       = contextKey("file")
	descriptorContextKey = contextKey("descriptor")
	enumContextKey       = contextKey("enum")
	serviceContextKey    = contextKey("service")
)

// ContextWithFileDescriptor returns a new context with the attached `FileDescriptor`
func ContextWithFileDescriptor(ctx context.Context, fd *FileDescriptor) context.Context {
	return context.WithValue(ctx, fileContextKey, fd)
}

// FileDescriptorFromContext returns the `FileDescriptor` from the context and whether or not the key was found.
func FileDescriptorFromContext(ctx context.Context) (*FileDescriptor, bool) {
	val, ok := ctx.Value(fileContextKey).(*FileDescriptor)
	return val, ok
}

// ContextWithDescriptor returns a new context with the specified `Descriptor`
func ContextWithDescriptor(ctx context.Context, d *Descriptor) context.Context {
	return context.WithValue(ctx, descriptorContextKey, d)
}

// DescriptorFromContext returns the associated `Descriptor` for the context and whether or not it was found
func DescriptorFromContext(ctx context.Context) (*Descriptor, bool) {
	val, ok := ctx.Value(descriptorContextKey).(*Descriptor)
	return val, ok
}

// ContextWithEnumDescriptor returns a new context with the specified `EnumDescriptor`
func ContextWithEnumDescriptor(ctx context.Context, d *EnumDescriptor) context.Context {
	return context.WithValue(ctx, enumContextKey, d)
}

// EnumDescriptorFromContext returns the associated `EnumDescriptor` for the context and whether or not it was found
func EnumDescriptorFromContext(ctx context.Context) (*EnumDescriptor, bool) {
	val, ok := ctx.Value(enumContextKey).(*EnumDescriptor)
	return val, ok
}

// ContextWithServiceDescriptor returns a new context with `service`
func ContextWithServiceDescriptor(ctx context.Context, service *ServiceDescriptor) 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) (*ServiceDescriptor, bool) {
	val, ok := ctx.Value(serviceContextKey).(*ServiceDescriptor)
	return val, ok
}