-
Notifications
You must be signed in to change notification settings - Fork 414
/
interpolate_helper.go
407 lines (364 loc) · 9.79 KB
/
interpolate_helper.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
package interpolate
// This functions come from https://github.com/Masterminds/sprig
// Copyright (C) 2013 Masterminds
// Masterminds/sprig is licensed under the MIT License
import (
"encoding/base64"
"encoding/json"
"fmt"
"net/url"
"path"
"reflect"
"strconv"
"strings"
"text/template"
util "github.com/aokoli/goutils"
"github.com/huandu/xstrings"
"github.com/spf13/cast"
)
// InterpolateHelperFuncs is a list of funcs that can be used in go templates.
var InterpolateHelperFuncs template.FuncMap
func init() {
InterpolateHelperFuncs = wrapHelpers(template.FuncMap{
"abbrev": abbrev,
"abbrevboth": abbrevboth,
"trunc": trunc,
"trim": strings.TrimSpace,
"upper": strings.ToUpper,
"lower": strings.ToLower,
"title": strings.Title,
"untitle": untitle,
"substr": substring,
// Switch order so that "foo" | repeat 5
"repeat": func(count int, str string) string { return strings.Repeat(str, count) },
// Deprecated: Use trimAll.
"trimall": func(a, b string) string { return strings.Trim(b, a) },
// Switch order so that "$foo" | trimall "$"
"trimAll": func(a, b string) string { return strings.Trim(b, a) },
"trimSuffix": func(a, b string) string { return strings.TrimSuffix(b, a) },
"trimPrefix": func(a, b string) string { return strings.TrimPrefix(b, a) },
"nospace": util.DeleteWhiteSpace,
"initials": initials,
"randAlphaNum": randAlphaNumeric,
"randAlpha": randAlpha,
"randASCII": randASCII,
"randNumeric": randNumeric,
"swapcase": util.SwapCase,
"shuffle": xstrings.Shuffle,
"snakecase": xstrings.ToSnakeCase,
"camelcase": xstrings.ToCamelCase,
"quote": quote,
"squote": squote,
"indent": indent,
"nindent": nindent,
"replace": replace,
"plural": plural,
"toString": strval,
"default": dfault,
"empty": empty,
"coalesce": coalesce,
"toJSON": toJSON,
"toPrettyJSON": toPrettyJSON,
"b64enc": base64encode,
"b64dec": base64decode,
"escape": escape,
"stringQuote": stringQuote,
"add": func(i ...interface{}) int64 {
var a int64 = 0
for _, b := range i {
a += toInt64(b)
}
return a
},
"sub": func(a, b interface{}) int64 { return toInt64(a) - toInt64(b) },
"mul": func(a interface{}, v ...interface{}) int64 {
val := toInt64(a)
for _, b := range v {
val = val * toInt64(b)
}
return val
},
"div": func(a, b interface{}) int64 { return toInt64(a) / toInt64(b) },
"mod": func(a, b interface{}) int64 { return toInt64(a) % toInt64(b) },
"ternary": ternary,
"urlencode": func(s string) string { return url.QueryEscape(s) },
"dirname": func(s string) string { return path.Dir(s) },
"basename": func(s string) string { return path.Base(s) },
})
}
// wrapHelpers to handle usage of val struct in interpolate.Do
func wrapHelpers(fs template.FuncMap) template.FuncMap {
wrappedHelpers := make(template.FuncMap, len(fs))
for key, helper := range fs {
helperV := reflect.ValueOf(helper)
// ignore if current helper is not a func
if helperV.Kind() != reflect.Func {
continue
}
helperT := helperV.Type()
paramsCount := helperT.NumIn()
paramsTypes := make([]string, paramsCount)
for i := 0; i < paramsCount; i++ {
paramsTypes[i] = helperT.In(i).Name()
}
// create the wrapper func
wrappedHelpers[key] = func(ps ...interface{}) interface{} {
// if the helper func need more params than ps length, throw an error
if len(ps) < paramsCount {
// panic will be catched be text/template executor
panic(fmt.Sprintf("missing params (expected: %s)", strings.Join(paramsTypes, ", ")))
}
// for all helper's params, forward values from wrapper
values := make([]reflect.Value, len(ps))
for i := 0; i < len(ps); i++ {
if value, ok := ps[i].(*val); ok {
// if the value is a pointer to val, we should return its internal value
values[i] = reflect.ValueOf((*value)["_"])
} else if value, ok := ps[i].(val); ok {
// if the value is a val, we should return its internal value
values[i] = reflect.ValueOf(value["_"])
} else if v := reflect.ValueOf(ps[i]); v.IsValid() {
// for all params that are not val (string, integer...) use it directly
values[i] = v
} else {
// if the value is not valid (means that given value is nil with unknown type), convert to nil void pointer
var v *void
values[i] = reflect.ValueOf(v)
}
}
results := helperV.Call(values)
if len(results) == 0 {
return nil
}
return results[0].Interface()
}
}
return wrappedHelpers
}
// Switch order so that "$foo" | trimall "$"
func dfault(valfault ...interface{}) string {
var castToString = func(i interface{}) string {
s, _ := i.(string)
return s
}
if len(valfault) == 0 {
return ""
}
if len(valfault) == 1 {
s := castToString(valfault[0])
return s
}
for i := len(valfault) - 1; i >= 0; i-- {
if s := castToString(valfault[i]); s != "" {
return s
}
}
return ""
}
// empty returns true if the given value has the zero value for its type.
func empty(given interface{}) bool {
g := reflect.ValueOf(given)
if !g.IsValid() {
return true
}
// Basically adapted from text/template.isTrue
switch g.Kind() {
case reflect.Array, reflect.Slice, reflect.Map, reflect.String:
return g.Len() == 0
case reflect.Bool:
return g.Bool() == false
case reflect.Complex64, reflect.Complex128:
return g.Complex() == 0
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return g.Int() == 0
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return g.Uint() == 0
case reflect.Float32, reflect.Float64:
return g.Float() == 0
case reflect.Struct:
return false
default:
return g.IsNil()
}
}
// coalesce returns the first non-empty value.
func coalesce(v ...interface{}) interface{} {
for _, val := range v {
if !empty(val) {
return val
}
}
return nil
}
// toJSON encodes an item into a JSON string
func toJSON(v interface{}) string {
output, _ := json.Marshal(v)
return string(output)
}
// toPrettyJSON encodes an item into a pretty (indented) JSON string
func toPrettyJSON(v interface{}) string {
output, _ := json.MarshalIndent(v, "", " ")
return string(output)
}
func base64encode(v string) string {
return base64.StdEncoding.EncodeToString([]byte(v))
}
func base64decode(v string) string {
data, err := base64.StdEncoding.DecodeString(v)
if err != nil {
return err.Error()
}
return string(data)
}
func abbrev(width int, s string) string {
if width < 4 {
return s
}
r, _ := util.Abbreviate(s, width)
return r
}
func abbrevboth(left, right int, s string) string {
if right < 4 || left > 0 && right < 7 {
return s
}
r, _ := util.AbbreviateFull(s, left, right)
return r
}
func initials(s string) string {
// Wrap this just to eliminate the var args, which templates don't do well.
return util.Initials(s)
}
func randAlphaNumeric(count int) string {
// It is not possible, it appears, to actually generate an error here.
r, _ := util.RandomAlphaNumeric(count)
return r
}
func randAlpha(count int) string {
r, _ := util.RandomAlphabetic(count)
return r
}
func randASCII(count int) string {
r, _ := util.RandomAscii(count)
return r
}
func randNumeric(count int) string {
r, _ := util.RandomNumeric(count)
return r
}
func untitle(str string) string {
return util.Uncapitalize(str)
}
func quote(str ...interface{}) string {
out := make([]string, len(str))
for i, s := range str {
out[i] = fmt.Sprintf("%q", strval(s))
}
return strings.Join(out, " ")
}
func squote(str ...interface{}) string {
out := make([]string, len(str))
for i, s := range str {
out[i] = fmt.Sprintf("'%v'", s)
}
return strings.Join(out, " ")
}
func indent(spaces int, v string) string {
pad := strings.Repeat(" ", spaces)
return pad + strings.Replace(v, "\n", "\n"+pad, -1)
}
func nindent(spaces int, v string) string {
return "\n" + indent(spaces, v)
}
func replace(old, new, src string) string {
return strings.Replace(src, old, new, -1)
}
func plural(one, many string, count int) string {
if count == 1 {
return one
}
return many
}
func strslice(v interface{}) []string {
switch v := v.(type) {
case []string:
return v
case []interface{}:
l := len(v)
b := make([]string, l)
for i := 0; i < l; i++ {
b[i] = strval(v[i])
}
return b
default:
val := reflect.ValueOf(v)
switch val.Kind() {
case reflect.Array, reflect.Slice:
l := val.Len()
b := make([]string, l)
for i := 0; i < l; i++ {
b[i] = strval(val.Index(i).Interface())
}
return b
default:
return []string{strval(v)}
}
}
}
func strval(v interface{}) string {
switch v := v.(type) {
case string:
return v
case []byte:
return string(v)
case error:
return v.Error()
case fmt.Stringer:
return v.String()
default:
return fmt.Sprintf("%v", v)
}
}
func trunc(c int, s string) string {
if len(s) <= c {
return s
}
return s[0:c]
}
// substring creates a substring of the given string.
//
// If start is < 0, this calls string[:length].
//
// If start is >= 0 and length < 0, this calls string[start:]
//
// Otherwise, this calls string[start, length].
func substring(start, length int, s string) string {
if start < 0 {
return s[:length]
}
if length < 0 {
return s[start:]
}
return s[start:length]
}
func escape(s string) string {
s1 := strings.Replace(s, "_", "-", -1)
s1 = strings.Replace(s1, "/", "-", -1)
s1 = strings.Replace(s1, ".", "-", -1)
return s1
}
func stringQuote(s string) string {
x := strconv.Quote(s)
x = strings.TrimPrefix(x, `"`)
x = strings.TrimSuffix(x, `"`)
return x
}
func ternary(v, v2, a interface{}) interface{} {
if cast.ToBool(a) {
return v
}
return v2
}
// toInt64 converts integer types to 64-bit integers
func toInt64(v interface{}) int64 {
return cast.ToInt64(v)
}