-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
67 lines (53 loc) · 2.27 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
package jasper
import (
"context"
"github.com/tychoish/fun"
)
type ctxKey string
const defaultContextKey ctxKey = "__JASPER_STD_MANAGER"
// WithManager attaches a Manager instance to the context
func WithManager(ctx context.Context, mgr Manager) context.Context {
return WithContextManager(ctx, string(defaultContextKey), mgr)
}
// Context resolves a jasper.Manager from the given context, and if one does
// not exist (or the context is nil), produces the global Manager
// instance.
func Context(ctx context.Context) Manager { return ContextManager(ctx, string(defaultContextKey)) }
// WithContextManager attaches a jasper.Manager with a specific name
// to the context.
func WithContextManager(ctx context.Context, name string, mgr Manager) context.Context {
return context.WithValue(ctx, ctxKey(name), mgr)
}
// WithNewContextLogger checks if a logger is configured with a
// specific name in the current context. If this logger exists,
// WithNewContextLogger is a noop; otherwise, it constructs a logger
// with the sender produced by the provided function and attaches it
// to the context returning that context.
//
// The name provided controls the id of the logger in the context, not
// the name of the logger.
func WithNewContextManager(ctx context.Context, name string, fn func() Manager) context.Context {
if HasContextManager(ctx, name) {
return ctx
}
return WithContextManager(ctx, name, fn())
}
// ContextLoger produces a jasper.Manager stored in the context by a given
// name. If such a context is not stored the standard/default jasper.Manager
// is returned.
func ContextManager(ctx context.Context, name string) Manager {
val := ctx.Value(ctxKey(name))
fun.Invariant.Ok(val != nil, "jasper", name, "manager must be stored")
mgr, ok := val.(Manager)
fun.Invariant.Ok(ok, "stored jasper manager", name, "must be of the correct type")
return mgr
}
// HasContextManager checks the provided context to see if a Manager
// with the given name is attached to the provided context.
func HasContextManager(ctx context.Context, name string) bool {
_, ok := ctx.Value(ctxKey(name)).(Manager)
return ok
}
// HasManager returns true when the default context Manager is
// attached.
func HasManager(ctx context.Context) bool { return HasContextManager(ctx, string(defaultContextKey)) }