forked from yaricom/goNEAT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
28 lines (22 loc) · 857 Bytes
/
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
package neat
import (
"context"
"errors"
)
var ErrNEATOptionsNotFound = errors.New("NEAT options not found in the context")
// key is an unexported type for keys defined in this package.
// This prevents collisions with keys defined in other packages.
type key int
// neatOptionsKey is the key for neat.Options values in Contexts. It is
// unexported; clients use neat.NewContext and neat.FromContext
// instead of using this key directly.
var neatOptionsKey key
// NewContext returns a new Context that carries value of NEAT options.
func NewContext(ctx context.Context, opts *Options) context.Context {
return context.WithValue(ctx, neatOptionsKey, opts)
}
// FromContext returns the NEAT Options value stored in ctx, if any.
func FromContext(ctx context.Context) (*Options, bool) {
u, ok := ctx.Value(neatOptionsKey).(*Options)
return u, ok
}