-
Notifications
You must be signed in to change notification settings - Fork 0
/
ctx.go
91 lines (80 loc) · 1.41 KB
/
ctx.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package ctx
import (
"context"
"fmt"
"strings"
"github.com/Laughs-In-Flowers/log"
"github.com/Laughs-In-Flowers/warhola/lib/canvas"
)
type Ctx struct {
canvas.Canvas
log.Logger
Debug bool
DebugMap map[string]string
File string
Geometry string
}
func Is(c context.Context) *Ctx {
cv := Canvas(c)
l := Log(c)
d := Debug(c)
dm := DebugMap(c)
f := File(c)
g := Geometry(c)
return &Ctx{
cv, l, d, dm, f, g,
}
}
func Debug(c context.Context) bool {
dv := c.Value(0)
if d, ok := dv.(bool); ok {
return d
}
return false
}
func DebugMap(c context.Context) map[string]string {
di := c.Value(1)
if div, ok := di.(map[string]string); ok {
return div
}
return nil
}
func DebugMapCollapse(m map[string]string) string {
var inter []string
for k, v := range m {
inter = append(inter, fmt.Sprintf("%s: %s", k, v))
}
return strings.Join(inter, "\n")
}
func Log(c context.Context) log.Logger {
l := c.Value(2)
var ll log.Logger
var ok bool
if ll, ok = l.(log.Logger); ok {
return ll
}
return nil
}
func Canvas(c context.Context) canvas.Canvas {
cv := c.Value(4)
var cvv canvas.Canvas
var ok bool
if cvv, ok = cv.(canvas.Canvas); ok {
return cvv
}
return nil
}
func File(c context.Context) string {
f := c.Value(3)
if ff, ok := f.(string); ok {
return ff
}
return ""
}
func Geometry(c context.Context) string {
g := c.Value(4)
if gg, ok := g.(string); ok {
return gg
}
return ""
}