-
Notifications
You must be signed in to change notification settings - Fork 8
/
utils.go
105 lines (77 loc) · 2.06 KB
/
utils.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package messageformat
import (
"fmt"
"strconv"
"time"
)
// isWhitespace returns true if the rune is a whitespace.
func isWhitespace(char rune) bool {
return char == ' ' || char == '\r' || char == '\n' || char == '\t'
}
// whitespace traverses the input until a non-whitespace char is encountered.
func whitespace(start, end int, ptr_input *[]rune) (rune, int) {
input := *ptr_input
pos := start
for pos < end {
char := input[pos]
switch char {
default:
return char, pos
case ' ', '\r', '\n', '\t':
}
pos++
}
return 0, pos
}
// toString retrieves a value from the given map and tries to return a string representation.
//
// It will returns an error if the value's type is not <nil/string/bool/numeric/time.Duration/fmt.Stringer>.
func toString(data map[string]interface{}, key string) (string, error) {
if v, ok := data[key]; ok {
switch t := v.(type) {
default:
return "", fmt.Errorf("toString: Unsupported type: %T", v)
case nil:
return "", nil
case bool:
return strconv.FormatBool(t), nil
case string:
return t, nil
case int:
return fmt.Sprintf("%d", t), nil
case int8:
return strconv.FormatInt(int64(t), 10), nil
case int16:
return strconv.FormatInt(int64(t), 10), nil
case int32:
return strconv.FormatInt(int64(t), 10), nil
case int64:
return strconv.FormatInt(t, 10), nil
case uint:
return strconv.FormatUint(uint64(t), 10), nil
case uint8:
return strconv.FormatUint(uint64(t), 10), nil
case uint16:
return strconv.FormatUint(uint64(t), 10), nil
case uint32:
return strconv.FormatUint(uint64(t), 10), nil
case uint64:
return strconv.FormatUint(t, 10), nil
case float32:
return strconv.FormatFloat(float64(t), 'f', -1, 32), nil
case float64:
return strconv.FormatFloat(t, 'f', -1, 64), nil
case complex64:
return fmt.Sprintf("%g", t), nil
case complex128:
return fmt.Sprintf("%g", t), nil
case uintptr:
return fmt.Sprintf("%08x", t), nil
case time.Duration:
return t.String(), nil
case fmt.Stringer:
return t.String(), nil
}
}
return "", nil
}