-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.go
48 lines (38 loc) · 1.2 KB
/
config.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
package config
import (
"context"
"time"
"github.com/deixis/spine/contextutil"
)
// TODO: Move these structs to spine package once ctx.App is gone
// Config defines the app config
type Config struct {
Node string `toml:"node"`
Version string `toml:"version"`
Request Request `toml:"request"`
}
// Request defines the request default configuration
type Request struct {
TimeoutMS time.Duration `toml:"timeout_ms"`
AllowContext bool `toml:"allow_context"`
Panic bool `toml:"panic"`
}
// Timeout returns the TimeoutMS field in time.Duration
func (r *Request) Timeout() time.Duration {
return time.Millisecond * r.TimeoutMS
}
type contextKey struct{}
var activeTreeContextKey = contextKey{}
// TreeFromContext returns a config `Tree` instance associated with `ctx`, or
// a `NopTree` if no `Tree` instance could be found.
func TreeFromContext(ctx contextutil.ValueContext) Tree {
val := ctx.Value(activeTreeContextKey)
if o, ok := val.(Tree); ok {
return o
}
return NopTree()
}
// TreeWithContext returns a copy of parent in which the `Tree` is stored
func TreeWithContext(ctx context.Context, t Tree) context.Context {
return context.WithValue(ctx, activeTreeContextKey, t)
}