-
Notifications
You must be signed in to change notification settings - Fork 1
/
templatefuncs.go
379 lines (359 loc) · 9.51 KB
/
templatefuncs.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
package templatefuncs
import (
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io/fs"
"os"
"os/exec"
"regexp"
"strconv"
"strings"
"text/template"
)
// fileModeTypeNames maps file mode types to human-readable strings.
var fileModeTypeNames = map[fs.FileMode]string{
0: "file",
fs.ModeDir: "dir",
fs.ModeSymlink: "symlink",
fs.ModeNamedPipe: "named pipe",
fs.ModeSocket: "socket",
fs.ModeDevice: "device",
fs.ModeCharDevice: "char device",
}
// NewFuncMap returns a new [text/template.FuncMap] containing all template
// functions.
func NewFuncMap() template.FuncMap {
return template.FuncMap{
"contains": reverseArgs2(strings.Contains),
"eqFold": eqFoldTemplateFunc,
"fromJSON": eachByteSliceErr(fromJSONTemplateFunc),
"hasPrefix": reverseArgs2(strings.HasPrefix),
"hasSuffix": reverseArgs2(strings.HasSuffix),
"hexDecode": eachStringErr(hex.DecodeString),
"hexEncode": eachByteSlice(hex.EncodeToString),
"join": reverseArgs2(strings.Join),
"list": listTemplateFunc,
"lookPath": eachStringErr(lookPathTemplateFunc),
"lstat": eachString(lstatTemplateFunc),
"prefixLines": prefixLinesTemplateFunc,
"quote": eachString(strconv.Quote),
"regexpReplaceAll": regexpReplaceAllTemplateFunc,
"stat": eachString(statTemplateFunc),
"toJSON": toJSONTemplateFunc,
"toLower": eachString(strings.ToLower),
"toString": toStringTemplateFunc,
"toUpper": eachString(strings.ToUpper),
"trimSpace": eachString(strings.TrimSpace),
}
}
// prefixLinesTemplateFunc is the core implementation of the `prefixLines`
// template function.
func prefixLinesTemplateFunc(prefix, s string) string {
type stateType int
const (
startOfLine stateType = iota
inLine
)
state := startOfLine
var builder strings.Builder
builder.Grow(2 * len(s))
for _, r := range s {
switch state {
case startOfLine:
if _, err := builder.WriteString(prefix); err != nil {
panic(err)
}
if _, err := builder.WriteRune(r); err != nil {
panic(err)
}
if r != '\n' {
state = inLine
}
case inLine:
if _, err := builder.WriteRune(r); err != nil {
panic(err)
}
if r == '\n' {
state = startOfLine
}
}
}
return builder.String()
}
// eqFoldTemplateFunc is the core implementation of the `eqFold` template
// function.
func eqFoldTemplateFunc(first, second string, more ...string) bool {
if strings.EqualFold(first, second) {
return true
}
for _, s := range more {
if strings.EqualFold(first, s) {
return true
}
}
return false
}
// fromJSONTemplateFunc is the core implementation of the `fromJSON` template
// function.
func fromJSONTemplateFunc(data []byte) (any, error) {
var result any
if err := json.Unmarshal(data, &result); err != nil {
return nil, err
}
return result, nil
}
// listTemplateFunc is the core implementation of the `list` template function.
func listTemplateFunc(args ...any) []any {
return args
}
// lookPathTemplateFunc is the core implementation of the `lookPath` template
// function.
func lookPathTemplateFunc(file string) (string, error) {
switch path, err := exec.LookPath(file); {
case err == nil:
return path, nil
case errors.Is(err, exec.ErrNotFound):
return "", nil
case errors.Is(err, fs.ErrNotExist):
return "", nil
default:
return "", err
}
}
// lstatTemplateFunc is the core implementation of the `lstat` template
// function.
func lstatTemplateFunc(name string) any {
switch fileInfo, err := os.Lstat(name); {
case err == nil:
return fileInfoToMap(fileInfo)
case errors.Is(err, fs.ErrNotExist):
return nil
default:
panic(err)
}
}
// regexpReplaceAllTemplateFunc is the core implementation of the
// `regexpReplaceAll` template function.
func regexpReplaceAllTemplateFunc(expr, repl, s string) string {
return regexp.MustCompile(expr).ReplaceAllString(s, repl)
}
// statTemplateFunc is the core implementation of the `stat` template function.
func statTemplateFunc(name string) any {
switch fileInfo, err := os.Stat(name); {
case err == nil:
return fileInfoToMap(fileInfo)
case errors.Is(err, fs.ErrNotExist):
return nil
default:
panic(err)
}
}
// toJSONTemplateFunc is the core implementation of the `toJSON` template
// function.
func toJSONTemplateFunc(arg any) []byte {
data, err := json.Marshal(arg)
if err != nil {
panic(err)
}
return data
}
// toStringTemplateFunc is the core implementation of the `toString` template
// function.
func toStringTemplateFunc(arg any) string {
// FIXME add more types
switch arg := arg.(type) {
case string:
return arg
case []byte:
return string(arg)
case bool:
return strconv.FormatBool(arg)
case float32:
return strconv.FormatFloat(float64(arg), 'f', -1, 32)
case float64:
return strconv.FormatFloat(arg, 'f', -1, 64)
case int:
return strconv.Itoa(arg)
case int32:
return strconv.FormatInt(int64(arg), 10)
case int64:
return strconv.FormatInt(arg, 10)
default:
panic(fmt.Sprintf("%T: unsupported type", arg))
}
}
// eachByteSlice transforms a function that takes a single `[]byte` and returns
// a `T` to a function that takes zero or more `[]byte`-like arguments and
// returns zero or more `T`s.
func eachByteSlice[T any](f func([]byte) T) func(any) any {
return func(arg any) any {
switch arg := arg.(type) {
case []byte:
return f(arg)
case [][]byte:
result := make([]T, 0, len(arg))
for _, a := range arg {
result = append(result, f(a))
}
return result
case string:
return f([]byte(arg))
case []string:
result := make([]T, 0, len(arg))
for _, a := range arg {
result = append(result, f([]byte(a)))
}
return result
default:
panic(fmt.Sprintf("%T: unsupported argument type", arg))
}
}
}
// eachByteSliceErr transforms a function that takes a single `[]byte` and
// returns a `T` and an `error` into a function that takes zero or more
// `[]byte`-like arguments and returns zero or more `Ts` and an error.
func eachByteSliceErr[T any](f func([]byte) (T, error)) func(any) any {
return func(arg any) any {
switch arg := arg.(type) {
case []byte:
result, err := f(arg)
if err != nil {
panic(err)
}
return result
case [][]byte:
result := make([]T, 0, len(arg))
for _, a := range arg {
r, err := f(a)
if err != nil {
panic(err)
}
result = append(result, r)
}
return result
case string:
result, err := f([]byte(arg))
if err != nil {
panic(err)
}
return result
case []string:
result := make([]T, 0, len(arg))
for _, a := range arg {
r, err := f([]byte(a))
if err != nil {
panic(err)
}
result = append(result, r)
}
return result
default:
panic(fmt.Sprintf("%T: unsupported argument type", arg))
}
}
}
// eachString transforms a function that takes a single `string`-like argument
// and returns a `T` into a function that takes zero or more `string`-like
// arguments and returns zero or more `T`s.
func eachString[T any](f func(string) T) func(any) any {
return func(arg any) any {
switch arg := arg.(type) {
case string:
return f(arg)
case []string:
result := make([]T, 0, len(arg))
for _, a := range arg {
result = append(result, f(a))
}
return result
case []byte:
return f(string(arg))
case [][]byte:
result := make([]T, 0, len(arg))
for _, a := range arg {
result = append(result, f(string(a)))
}
return result
case []any:
result := make([]T, 0, len(arg))
for _, a := range arg {
switch a := a.(type) {
case string:
result = append(result, f(a))
case []byte:
result = append(result, f(string(a)))
default:
panic(fmt.Sprintf("%T: unsupported argument type", a))
}
}
return result
default:
panic(fmt.Sprintf("%T: unsupported argument type", arg))
}
}
}
// eachStringErr transforms a function that takes a single `string`-like argument
// and returns a `T` and an `error` into a function that takes zero or more
// `string`-like arguments and returns zero or more `T`s and an `error`.
func eachStringErr[T any](f func(string) (T, error)) func(any) any {
return func(arg any) any {
switch arg := arg.(type) {
case string:
result, err := f(arg)
if err != nil {
panic(err)
}
return result
case []string:
result := make([]T, 0, len(arg))
for _, a := range arg {
r, err := f(a)
if err != nil {
panic(err)
}
result = append(result, r)
}
return result
case []byte:
result, err := f(string(arg))
if err != nil {
panic(err)
}
return result
case [][]byte:
result := make([]T, 0, len(arg))
for _, a := range arg {
r, err := f(string(a))
if err != nil {
panic(err)
}
result = append(result, r)
}
return result
default:
panic(fmt.Sprintf("%T: unsupported argument type", arg))
}
}
}
// fileInfoToMap returns a `map[string]any` of `fileInfo`'s fields.
func fileInfoToMap(fileInfo fs.FileInfo) map[string]any {
return map[string]any{
"name": fileInfo.Name(),
"size": fileInfo.Size(),
"mode": int(fileInfo.Mode()),
"perm": int(fileInfo.Mode().Perm()),
"modTime": fileInfo.ModTime().Unix(),
"isDir": fileInfo.IsDir(),
"type": fileModeTypeNames[fileInfo.Mode()&fs.ModeType],
}
}
// reverseArgs2 transforms a function that takes two arguments and returns an
// `R` into a function that takes the arguments in reverse order and returns an
// `R`.
func reverseArgs2[T1, T2, R any](f func(T1, T2) R) func(T2, T1) R {
return func(arg1 T2, arg2 T1) R {
return f(arg2, arg1)
}
}