-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalues.go
More file actions
64 lines (52 loc) · 959 Bytes
/
values.go
File metadata and controls
64 lines (52 loc) · 959 Bytes
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
package contextual
import (
"fmt"
"strconv"
)
type ContextKV struct {
Key any
Value any
}
func (c *Cancellable) AddValue(key any, value any) {
c.values.Store(key, value)
}
func (c *Cancellable) GetE(key any) (any, bool) {
v, ok := c.values.Load(key)
return v, ok
}
func (c *Cancellable) Get(key any) any {
if v, ok := c.GetE(key); ok {
return v
}
return nil
}
func (c *Cancellable) GetString(key any) string {
if v, vok := c.GetE(key); vok {
if s, sok := v.(string); sok {
return s
}
return fmt.Sprintf("%v", v) // Changed %s to %v for general value formatting
}
return ""
}
func (c *Cancellable) GetInt(key any) int {
if v, vok := c.GetE(key); vok {
switch i := v.(type) {
case int:
return i
case int16:
return int(i)
case int32:
return int(i)
case int64:
return int(i)
case string:
o, err := strconv.ParseInt(i, 0, 0)
if err == nil {
return int(o)
}
return 0
}
}
return 0
}