-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
54 lines (45 loc) · 1.07 KB
/
options.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
package notifier
import (
"context"
"strings"
)
type contextKey string
var (
optionsKey contextKey = "OrionOptions"
)
// Options are request options passed from Orion to server
type Options map[string]interface{}
// FromContext fetches options from provided context
func FromContext(ctx context.Context) Options {
if h := ctx.Value(optionsKey); h != nil {
if options, ok := h.(Options); ok {
return options
}
}
return nil
}
// AddToOptions adds options to context
func AddToOptions(ctx context.Context, key string, value interface{}) context.Context {
h := FromContext(ctx)
if h == nil {
ctx = context.WithValue(ctx, optionsKey, make(Options))
h = FromContext(ctx)
}
if h != nil && key != "" {
h.Add(key, value)
}
return ctx
}
// Add to Options
func (o Options) Add(key string, value interface{}) {
o[strings.ToLower(key)] = value
}
// Del an options
func (o Options) Del(key string) {
delete(o, strings.ToLower(key))
}
// Get an options
func (o Options) Get(key string) (interface{}, bool) {
value, found := o[strings.ToLower(key)]
return value, found
}