-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathContext.go
executable file
·68 lines (61 loc) · 1.32 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 symon_agen
import (
"bytes"
"fmt"
"sort"
"strconv"
"strings"
)
type MonitorContext map[string]string
func NewMonitorContext() MonitorContext {
return make(MonitorContext)
}
func(ctx MonitorContext) String() string {
var buffer bytes.Buffer
keys := make([]string, 0)
for k := range ctx {
keys = append(keys, k)
}
sort.Strings(keys)
for i := range keys {
buffer.WriteString(fmt.Sprintf("%s : %s\n", keys[i],ctx[keys[i]]))
}
return buffer.String()
}
func (ctx MonitorContext) GetInt(key string) int {
intValue, err := strconv.Atoi(ctx[key])
if err != nil {
return 0
}
return intValue
}
func (ctx MonitorContext) GetBool(key string) bool {
switch strings.ToUpper(ctx[key]) {
case "TRUE":
return true
case "YES":
return true
default:
return false
}
}
func (ctx MonitorContext) GetFloat(key string) float64 {
floatValue, err := strconv.ParseFloat(ctx[key], 64)
if err != nil {
return 0
}
return floatValue
}
func (ctx MonitorContext) SetInt(key string, value int64) {
ctx[key] = fmt.Sprintf("%d", value)
}
func (ctx MonitorContext) SetBool(key string, value bool) {
ctx[key] = fmt.Sprintf("%v", value)
}
func (ctx MonitorContext) SetFloat(key string, value float64) {
ctx[key] = fmt.Sprintf("%f", value)
}
func (ctx MonitorContext) Contains(key string) bool {
_, exist := ctx[key]
return exist
}