-
Notifications
You must be signed in to change notification settings - Fork 278
/
Copy pathtemplate.go
201 lines (171 loc) · 4.01 KB
/
template.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package utils
import (
"encoding/json"
"fmt"
"math"
"reflect"
"strconv"
"strings"
"text/template"
)
var TemplateFuncs = template.FuncMap{
"ToJSON": toJson,
"ToJSONPretty": toJsonPretty,
"add": add,
"subtract": subtract,
}
func toJson(v any) string {
a, _ := json.Marshal(v)
return string(a)
}
func toJsonPretty(v any, prefix string, indent string) string {
a, _ := json.MarshalIndent(v, prefix, indent)
return string(a)
}
func add(a, b int) int {
return a + b
}
// subtract b from a
// copy from gomplate.
func subtract(a, b any) (any, error) {
if containsFloat(a, b) {
fa, err := ToFloat64(a)
if err != nil {
return nil, fmt.Errorf("expected a number: %w", err)
}
fb, err := ToFloat64(b)
if err != nil {
return nil, fmt.Errorf("expected a number: %w", err)
}
return fa - fb, nil
}
ia, err := ToInt64(a)
if err != nil {
return nil, fmt.Errorf("expected a number: %w", err)
}
ib, err := ToInt64(b)
if err != nil {
return nil, fmt.Errorf("expected a number: %w", err)
}
return ia - ib, nil
}
func containsFloat(n ...any) bool {
c := false
for _, v := range n {
if isFloat(v) {
return true
}
}
return c
}
func isFloat(n any) bool {
switch i := n.(type) {
case float32, float64:
return true
case string:
_, err := strconv.ParseFloat(i, 64)
if err != nil {
return false
}
if isInt(i) {
return false
}
return true
}
return false
}
func isInt(n any) bool {
switch i := n.(type) {
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
return true
case string:
_, err := strconv.ParseInt(i, 0, 64)
return err == nil
}
return false
}
func ToFloat64(v interface{}) (float64, error) {
if str, ok := v.(string); ok {
return strToFloat64(str)
}
val := reflect.Indirect(reflect.ValueOf(v))
switch val.Kind() {
case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int:
return float64(val.Int()), nil
case reflect.Uint8, reflect.Uint16, reflect.Uint32:
return float64(val.Uint()), nil
case reflect.Uint, reflect.Uint64:
return float64(val.Uint()), nil
case reflect.Float32, reflect.Float64:
return val.Float(), nil
case reflect.Bool:
if val.Bool() {
return 1, nil
}
return 0, nil
default:
return 0, fmt.Errorf("could not convert %v to float64", v)
}
}
func strToInt64(str string) (int64, error) {
if strings.Contains(str, ",") {
str = strings.ReplaceAll(str, ",", "")
}
iv, err := strconv.ParseInt(str, 0, 64)
if err != nil {
// maybe it's a float?
var fv float64
fv, err = strconv.ParseFloat(str, 64)
if err != nil {
return 0, fmt.Errorf("could not convert %q to int64: %w", str, err)
}
return ToInt64(fv)
}
return iv, nil
}
func ToInt64(v interface{}) (int64, error) {
if str, ok := v.(string); ok {
return strToInt64(str)
}
val := reflect.Indirect(reflect.ValueOf(v))
switch val.Kind() {
case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int:
return val.Int(), nil
case reflect.Uint8, reflect.Uint16, reflect.Uint32:
//nolint:gosec // G115 isn't applicable, this is a Uint32 at most
return int64(val.Uint()), nil
case reflect.Uint, reflect.Uint64:
tv := val.Uint()
if tv > math.MaxInt64 {
return 0, fmt.Errorf("could not convert %d to int64, would overflow", tv)
}
return int64(tv), nil
case reflect.Float32, reflect.Float64:
return int64(val.Float()), nil
case reflect.Bool:
if val.Bool() {
return 1, nil
}
return 0, nil
default:
return 0, fmt.Errorf("could not convert %v to int64", v)
}
}
func strToFloat64(str string) (float64, error) {
if strings.Contains(str, ",") {
str = strings.ReplaceAll(str, ",", "")
}
// this is inefficient, but it's the only way I can think of to
// properly convert octal integers to floats
iv, err := strconv.ParseInt(str, 0, 64)
if err != nil {
// ok maybe it's a float?
var fv float64
fv, err = strconv.ParseFloat(str, 64)
if err != nil {
return 0, fmt.Errorf("could not convert %q to float64: %w", str, err)
}
return fv, nil
}
return float64(iv), nil
}