-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipeline_template.go
More file actions
842 lines (788 loc) · 22.8 KB
/
pipeline_template.go
File metadata and controls
842 lines (788 loc) · 22.8 KB
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
package module
import (
"bytes"
"encoding/json"
"fmt"
"net/url"
"reflect"
"regexp"
"sort"
"strconv"
"strings"
"text/template"
"time"
"github.com/google/uuid"
)
// toFloat64 converts any numeric type (or numeric string) to float64.
func toFloat64(v any) float64 {
switch n := v.(type) {
case int:
return float64(n)
case int8:
return float64(n)
case int16:
return float64(n)
case int32:
return float64(n)
case int64:
return float64(n)
case float32:
return float64(n)
case float64:
return n
case string:
f, _ := strconv.ParseFloat(n, 64)
return f
case json.Number:
f, _ := n.Float64()
return f
default:
return 0
}
}
// isIntType returns true if the value is an integer type.
func isIntType(v any) bool {
switch v.(type) {
case int, int8, int16, int32, int64:
return true
default:
return false
}
}
// toInt64Direct converts integer types to int64 without a float64 intermediate.
// This preserves precision for large int64 values (>2^53) that float64 cannot
// represent exactly. Returns 0 for non-integer types.
func toInt64Direct(v any) int64 {
switch n := v.(type) {
case int:
return int64(n)
case int8:
return int64(n)
case int16:
return int64(n)
case int32:
return int64(n)
case int64:
return n
default:
return 0
}
}
// compareValues compares two arbitrary values for sorting. Returns -1, 0, or 1.
// Numeric types (including numeric strings) sort before non-numeric strings.
// Nil values sort last. Other types are compared as their string representation.
func compareValues(a, b any) int {
rankA, numA, strA := classifyForSort(a)
rankB, numB, strB := classifyForSort(b)
if rankA != rankB {
if rankA < rankB {
return -1
}
return 1
}
switch rankA {
case 0: // numeric
if numA < numB {
return -1
}
if numA > numB {
return 1
}
return 0
case 1: // string
if strA < strB {
return -1
}
if strA > strB {
return 1
}
return 0
default: // nil — keep original order (stable sort)
return 0
}
}
// classifyForSort returns a rank and comparable value for a sort key:
//
// rank 0: numeric (numbers and numeric strings), compared by numeric value
// rank 1: non-numeric strings and other types, compared lexicographically
// rank 2: nil values, sorted last
func classifyForSort(v any) (rank int, num float64, str string) {
if v == nil {
return 2, 0, ""
}
switch vv := v.(type) {
case string:
if f, err := strconv.ParseFloat(vv, 64); err == nil {
return 0, f, ""
}
return 1, 0, vv
case int, int8, int16, int32, int64,
uint, uint8, uint16, uint32, uint64,
float32, float64:
return 0, toFloat64(v), ""
default:
return 1, 0, fmt.Sprintf("%v", vv)
}
}
// TemplateEngine resolves {{ .field }} expressions against a PipelineContext.
type TemplateEngine struct{}
// NewTemplateEngine creates a new TemplateEngine.
func NewTemplateEngine() *TemplateEngine {
return &TemplateEngine{}
}
// templateData builds the data map that Go templates see.
func (te *TemplateEngine) templateData(pc *PipelineContext) map[string]any {
data := make(map[string]any)
// Current values are top-level
for k, v := range pc.Current {
data[k] = v
}
// Step outputs accessible under "steps"
data["steps"] = pc.StepOutputs
// Trigger data accessible under "trigger"
data["trigger"] = pc.TriggerData
// Metadata accessible under "meta"
data["meta"] = pc.Metadata
return data
}
// dotChainRe matches dot-access chains like .steps.my-step.field.
// Hyphens are intentionally allowed within identifier segments so that
// hyphenated step names and fields (e.g. .steps.my-step.field) are
// treated as a single chain. This means ambiguous cases like ".x-1"
// are interpreted as a hyphenated identifier ("x-1") rather than as
// subtraction ".x - 1" when applying the auto-fix rewrite.
var dotChainRe = regexp.MustCompile(`\.[a-zA-Z_][a-zA-Z0-9_-]*(?:\.[a-zA-Z_][a-zA-Z0-9_-]*)*`)
// stringLiteralRe matches double-quoted and backtick-quoted string literals.
// Go templates only support double-quoted and backtick strings (not single-quoted),
// so single quotes are intentionally not handled here.
// Note: Go's regexp package uses RE2 (linear-time matching), so there is no risk
// of catastrophic backtracking / ReDoS with this pattern.
var stringLiteralRe = regexp.MustCompile(`"(?:[^"\\]|\\.)*"` + "|`[^`]*`")
// preprocessTemplate rewrites dot-access chains containing hyphens into index
// syntax so that Go's text/template parser does not treat hyphens as minus.
// For example: {{ .steps.my-step.field }} → {{ (index .steps "my-step" "field") }}
func preprocessTemplate(tmplStr string) string {
// Quick exit: nothing to do if there are no actions or no hyphens.
if !strings.Contains(tmplStr, "{{") || !strings.Contains(tmplStr, "-") {
return tmplStr
}
var out strings.Builder
rest := tmplStr
for {
openIdx := strings.Index(rest, "{{")
if openIdx < 0 {
out.WriteString(rest)
break
}
closeIdx := strings.Index(rest[openIdx:], "}}")
if closeIdx < 0 {
out.WriteString(rest)
break
}
closeIdx += openIdx // absolute position
// Write text before the action.
out.WriteString(rest[:openIdx])
action := rest[openIdx+2 : closeIdx] // content between {{ and }}
// Skip pure template comments {{/* ... */}}. Only actions whose entire
// content (after trimming) is a block comment are skipped. Mixed actions
// like {{ x /* comment */ y }} are not skipped since they contain code.
trimmed := strings.TrimSpace(action)
if strings.HasPrefix(trimmed, "/*") && strings.HasSuffix(trimmed, "*/") {
out.WriteString("{{")
out.WriteString(action)
out.WriteString("}}")
rest = rest[closeIdx+2:]
continue
}
// Strip string literals to avoid false matches on quoted hyphens.
var placeholders []string
const placeholderSentinel = "\x00<TMPL_PLACEHOLDER>"
stripped := stringLiteralRe.ReplaceAllStringFunc(action, func(m string) string {
placeholders = append(placeholders, m)
return placeholderSentinel
})
// Rewrite hyphenated dot-chains in the stripped action.
rewritten := dotChainRe.ReplaceAllStringFunc(stripped, func(chain string) string {
segments := strings.Split(chain[1:], ".") // drop leading dot
hasHyphen := false
for _, seg := range segments {
if strings.Contains(seg, "-") {
hasHyphen = true
break
}
}
if !hasHyphen {
return chain // no hyphens → leave as-is
}
// Find the first hyphenated segment.
firstHyphen := -1
for i, seg := range segments {
if strings.Contains(seg, "-") {
firstHyphen = i
break
}
}
// Build the prefix (non-hyphenated dot-access) and the quoted tail.
var prefix string
if firstHyphen == 0 {
prefix = "."
} else {
prefix = "." + strings.Join(segments[:firstHyphen], ".")
}
var quoted []string
for _, seg := range segments[firstHyphen:] {
quoted = append(quoted, `"`+seg+`"`)
}
return "(index " + prefix + " " + strings.Join(quoted, " ") + ")"
})
// Restore string literals from placeholders using strings.Index for O(n) scanning.
var restored string
if len(placeholders) > 0 {
phIdx := 0
var final strings.Builder
remaining := rewritten
for {
idx := strings.Index(remaining, placeholderSentinel)
if idx < 0 {
final.WriteString(remaining)
break
}
final.WriteString(remaining[:idx])
if phIdx < len(placeholders) {
final.WriteString(placeholders[phIdx])
phIdx++
}
remaining = remaining[idx+len(placeholderSentinel):]
}
restored = final.String()
} else {
restored = rewritten
}
out.WriteString("{{")
out.WriteString(restored)
out.WriteString("}}")
rest = rest[closeIdx+2:]
}
return out.String()
}
// funcMapWithContext returns the base template functions plus context-aware
// helper functions (step, trigger) that access PipelineContext data directly.
func (te *TemplateEngine) funcMapWithContext(pc *PipelineContext) template.FuncMap {
fm := templateFuncMap()
// step accesses step outputs by name and optional nested keys.
// Usage: {{ step "parse-request" "path_params" "id" }}
// Returns nil if the step doesn't exist, a key is missing, or an
// intermediate value is not a map (consistent with missingkey=zero).
fm["step"] = func(name string, keys ...string) any {
stepMap, ok := pc.StepOutputs[name]
if !ok || stepMap == nil {
return nil
}
var val any = stepMap
for _, key := range keys {
m, ok := val.(map[string]any)
if !ok {
return nil
}
val = m[key]
}
return val
}
// trigger accesses trigger data by nested keys.
// Usage: {{ trigger "path_params" "id" }}
fm["trigger"] = func(keys ...string) any {
if pc.TriggerData == nil {
return nil
}
var val any = map[string]any(pc.TriggerData)
for _, key := range keys {
m, ok := val.(map[string]any)
if !ok {
return nil
}
val = m[key]
}
return val
}
return fm
}
// Resolve evaluates a template string against a PipelineContext.
// If the string does not contain {{ }}, it is returned as-is.
func (te *TemplateEngine) Resolve(tmplStr string, pc *PipelineContext) (string, error) {
if !strings.Contains(tmplStr, "{{") {
return tmplStr, nil
}
tmplStr = preprocessTemplate(tmplStr)
t, err := template.New("").Funcs(te.funcMapWithContext(pc)).Option("missingkey=zero").Parse(tmplStr)
if err != nil {
return "", fmt.Errorf("template parse error: %w", err)
}
var buf bytes.Buffer
if err := t.Execute(&buf, te.templateData(pc)); err != nil {
return "", fmt.Errorf("template exec error: %w", err)
}
return buf.String(), nil
}
// ResolveMap evaluates all string values in a map that contain {{ }} expressions.
// Non-string values and nested maps/slices are processed recursively.
func (te *TemplateEngine) ResolveMap(data map[string]any, pc *PipelineContext) (map[string]any, error) {
result := make(map[string]any, len(data))
for k, v := range data {
resolved, err := te.resolveValue(v, pc)
if err != nil {
return nil, fmt.Errorf("field %q: %w", k, err)
}
result[k] = resolved
}
return result, nil
}
func (te *TemplateEngine) resolveValue(v any, pc *PipelineContext) (any, error) {
switch val := v.(type) {
case string:
return te.Resolve(val, pc)
case map[string]any:
return te.ResolveMap(val, pc)
case []any:
resolved := make([]any, len(val))
for i, item := range val {
r, err := te.resolveValue(item, pc)
if err != nil {
return nil, err
}
resolved[i] = r
}
return resolved, nil
default:
return v, nil
}
}
// timeLayouts maps common Go time constant names to their layout strings.
var timeLayouts = map[string]string{
"ANSIC": time.ANSIC,
"UnixDate": time.UnixDate,
"RubyDate": time.RubyDate,
"RFC822": time.RFC822,
"RFC822Z": time.RFC822Z,
"RFC850": time.RFC850,
"RFC1123": time.RFC1123,
"RFC1123Z": time.RFC1123Z,
"RFC3339": time.RFC3339,
"RFC3339Nano": time.RFC3339Nano,
"Kitchen": time.Kitchen,
"Stamp": time.Stamp,
"StampMilli": time.StampMilli,
"StampMicro": time.StampMicro,
"StampNano": time.StampNano,
"DateTime": time.DateTime,
"DateOnly": time.DateOnly,
"TimeOnly": time.TimeOnly,
}
// toAnySlice converts any slice type to []any using reflect. Returns nil for non-slices.
func toAnySlice(v any) []any {
if v == nil {
return nil
}
if s, ok := v.([]any); ok {
return s
}
rv := reflect.ValueOf(v)
if rv.Kind() != reflect.Slice {
return nil
}
result := make([]any, rv.Len())
for i := 0; i < rv.Len(); i++ {
result[i] = rv.Index(i).Interface()
}
return result
}
// extractField extracts a value from an item. If keys is provided and item is a map,
// returns map[key]. Otherwise returns item itself.
func extractField(item any, keys []string) any {
if len(keys) > 0 {
if m, ok := item.(map[string]any); ok {
return m[keys[0]]
}
}
return item
}
// templateFuncMap returns the function map available in pipeline templates.
func templateFuncMap() template.FuncMap {
return template.FuncMap{
// uuid generates a new UUID v4 string.
"uuid": func() string {
return uuid.New().String()
},
// uuidv4 generates a new UUID v4 string (alias for uuid).
"uuidv4": func() string {
return uuid.New().String()
},
// now returns the current UTC time formatted with the given Go time layout
// string or named constant (e.g. "RFC3339", "2006-01-02").
// When called with no argument it defaults to RFC3339.
"now": func(args ...string) string {
layout := time.RFC3339
if len(args) > 0 && args[0] != "" {
if l, ok := timeLayouts[args[0]]; ok {
layout = l
} else {
layout = args[0]
}
}
return time.Now().UTC().Format(layout)
},
// lower converts a string to lowercase.
"lower": strings.ToLower,
// default returns the fallback value if the primary value is empty.
"default": func(fallback, val any) any {
if val == nil {
return fallback
}
if s, ok := val.(string); ok && s == "" {
return fallback
}
return val
},
// trimPrefix removes the given prefix from a string if present.
"trimPrefix": func(prefix, s string) string {
return strings.TrimPrefix(s, prefix)
},
// trimSuffix removes the given suffix from a string if present.
"trimSuffix": func(suffix, s string) string {
return strings.TrimSuffix(s, suffix)
},
// json marshals a value to a JSON string.
"json": func(v any) string {
b, err := json.Marshal(v)
if err != nil {
return "{}"
}
return string(b)
},
// config looks up a value from the global config registry (populated by
// a config.provider module). Returns an empty string if the key is not found.
"config": func(key string) string {
if v, ok := GetConfigRegistry().Get(key); ok {
return v
}
return ""
},
// --- String functions ---
// upper converts a string to uppercase.
"upper": strings.ToUpper,
// title converts a string to title case (first letter of each word capitalized).
"title": func(s string) string {
words := strings.Fields(s)
for i, w := range words {
if len(w) > 0 {
words[i] = strings.ToUpper(w[:1]) + w[1:]
}
}
return strings.Join(words, " ")
},
// replace replaces all occurrences of old with new in s.
"replace": func(old, new_, s string) string { return strings.ReplaceAll(s, old, new_) },
// contains reports whether substr is within s.
"contains": func(substr, s string) bool { return strings.Contains(s, substr) },
// hasPrefix tests whether s begins with prefix.
"hasPrefix": func(prefix, s string) bool { return strings.HasPrefix(s, prefix) },
// hasSuffix tests whether s ends with suffix.
"hasSuffix": func(suffix, s string) bool { return strings.HasSuffix(s, suffix) },
// split splits s by sep and returns a slice.
"split": func(sep, s string) []string { return strings.Split(s, sep) },
// join concatenates elements of a slice with sep.
"join": func(sep string, v any) string {
rv := reflect.ValueOf(v)
if rv.Kind() != reflect.Slice {
return fmt.Sprintf("%v", v)
}
parts := make([]string, rv.Len())
for i := 0; i < rv.Len(); i++ {
parts[i] = fmt.Sprintf("%v", rv.Index(i).Interface())
}
return strings.Join(parts, sep)
},
// trimSpace removes leading and trailing whitespace.
"trimSpace": strings.TrimSpace,
// urlEncode percent-encodes a string for use in URLs.
"urlEncode": url.QueryEscape,
// --- Math functions ---
// add returns a + b. Returns int if both are ints, float64 otherwise.
"add": func(a, b any) any {
if isIntType(a) && isIntType(b) {
return int64(toFloat64(a)) + int64(toFloat64(b))
}
return toFloat64(a) + toFloat64(b)
},
// sub returns a - b. Returns int if both are ints, float64 otherwise.
"sub": func(a, b any) any {
if isIntType(a) && isIntType(b) {
return int64(toFloat64(a)) - int64(toFloat64(b))
}
return toFloat64(a) - toFloat64(b)
},
// mul returns a * b. Returns int if both are ints, float64 otherwise.
"mul": func(a, b any) any {
if isIntType(a) && isIntType(b) {
return int64(toFloat64(a)) * int64(toFloat64(b))
}
return toFloat64(a) * toFloat64(b)
},
// div returns a / b as float64. Returns 0 on divide-by-zero.
"div": func(a, b any) any {
fb := toFloat64(b)
if fb == 0 {
return float64(0)
}
return toFloat64(a) / fb
},
// --- Type/Utility functions ---
// toInt converts a value to int64.
"toInt": func(v any) int64 { return int64(toFloat64(v)) },
// toFloat converts a value to float64.
"toFloat": toFloat64,
// toString converts a value to its string representation.
"toString": func(v any) string { return fmt.Sprintf("%v", v) },
// length returns the length of a string, slice, array, or map. Returns 0 for other types.
"length": func(v any) int {
rv := reflect.ValueOf(v)
switch rv.Kind() {
case reflect.String, reflect.Slice, reflect.Array, reflect.Map:
return rv.Len()
default:
return 0
}
},
// coalesce returns the first non-nil, non-empty-string value.
"coalesce": func(vals ...any) any {
for _, v := range vals {
if v == nil {
continue
}
if s, ok := v.(string); ok && s == "" {
continue
}
return v
}
return nil
},
// --- Collection functions ---
// These functions operate on slices ([]any) with optional KEY for map elements.
// sum returns the sum of numeric values in a slice. O(n) single pass.
// Usage: {{ sum .nums }} or {{ sum .items "amount" }}
"sum": func(slice any, keys ...string) any {
items := toAnySlice(slice)
if items == nil {
return float64(0)
}
// Use an int64 accumulator when all values are integer types to avoid
// float64 precision loss for large integers (>2^53).
allInt := true
var intTotal int64
var floatTotal float64
for _, item := range items {
v := extractField(item, keys)
if allInt {
if isIntType(v) {
intTotal += toInt64Direct(v)
} else {
// Switch to float64 mode, carrying over the int accumulator.
allInt = false
floatTotal = float64(intTotal) + toFloat64(v)
}
} else {
floatTotal += toFloat64(v)
}
}
if allInt {
return intTotal
}
return floatTotal
},
// pluck extracts a single field from each map in a slice. O(n).
// Usage: {{ pluck .users "name" }}
"pluck": func(slice any, key string) []any {
items := toAnySlice(slice)
if items == nil {
return []any{}
}
result := make([]any, 0, len(items))
for _, item := range items {
if m, ok := item.(map[string]any); ok {
result = append(result, m[key])
}
}
return result
},
// flatten flattens one level of nested slices. O(n×m).
// Usage: {{ flatten .nested }}
"flatten": func(slice any) []any {
items := toAnySlice(slice)
if items == nil {
return []any{}
}
var result []any
for _, item := range items {
if inner := toAnySlice(item); inner != nil {
result = append(result, inner...)
} else {
result = append(result, item)
}
}
return result
},
// unique deduplicates a slice preserving insertion order. O(n).
// For maps: {{ unique .items "id" }} deduplicates by key value.
// For scalars: {{ unique .tags }}
"unique": func(slice any, keys ...string) []any {
items := toAnySlice(slice)
if items == nil {
return []any{}
}
seen := make(map[string]bool)
var result []any
for _, item := range items {
v := extractField(item, keys)
key := fmt.Sprintf("%v", v)
if !seen[key] {
seen[key] = true
result = append(result, item)
}
}
return result
},
// groupBy groups slice elements by a key value. O(n).
// Usage: {{ groupBy .items "category" }} → map[string][]any
"groupBy": func(slice any, key string) map[string][]any {
items := toAnySlice(slice)
if items == nil {
return map[string][]any{}
}
groups := make(map[string][]any)
for _, item := range items {
if m, ok := item.(map[string]any); ok {
k := fmt.Sprintf("%v", m[key])
groups[k] = append(groups[k], item)
}
}
return groups
},
// sortBy sorts a slice of maps by a key value ascending. O(n log n) stable sort.
// Supports numeric keys (sorted numerically), string keys (sorted lexicographically),
// and mixed types (numeric values sort before strings; nils sort last).
// Usage: {{ sortBy .items "price" }} or {{ sortBy .items "name" }}
"sortBy": func(slice any, key string) []any {
items := toAnySlice(slice)
if items == nil {
return []any{}
}
sorted := make([]any, len(items))
copy(sorted, items)
sort.SliceStable(sorted, func(i, j int) bool {
vi := extractField(sorted[i], []string{key})
vj := extractField(sorted[j], []string{key})
return compareValues(vi, vj) < 0
})
return sorted
},
// first returns the first element of a slice. O(1).
"first": func(slice any) any {
items := toAnySlice(slice)
if len(items) == 0 {
return nil
}
return items[0]
},
// last returns the last element of a slice. O(1).
"last": func(slice any) any {
items := toAnySlice(slice)
if len(items) == 0 {
return nil
}
return items[len(items)-1]
},
// min returns the minimum numeric value in a slice. O(n) single pass.
// Uses int64 comparison when all values are integer types to avoid float64
// precision loss for large integers (>2^53).
// Usage: {{ min .nums }} or {{ min .items "price" }}
"min": func(slice any, keys ...string) any {
items := toAnySlice(slice)
if len(items) == 0 {
return nil
}
first := extractField(items[0], keys)
allInt := isIntType(first)
intMin := toInt64Direct(first)
floatMin := toFloat64(first)
for _, item := range items[1:] {
v := extractField(item, keys)
if allInt {
if isIntType(v) {
n := toInt64Direct(v)
if n < intMin {
intMin = n
}
} else {
// Switch to float64 mode.
allInt = false
floatMin = float64(intMin)
f := toFloat64(v)
if f < floatMin {
floatMin = f
}
}
} else {
f := toFloat64(v)
if f < floatMin {
floatMin = f
}
}
}
if allInt {
return intMin
}
return floatMin
},
// max returns the maximum numeric value in a slice. O(n) single pass.
// Uses int64 comparison when all values are integer types to avoid float64
// precision loss for large integers (>2^53).
// Usage: {{ max .nums }} or {{ max .items "price" }}
"max": func(slice any, keys ...string) any {
items := toAnySlice(slice)
if len(items) == 0 {
return nil
}
first := extractField(items[0], keys)
allInt := isIntType(first)
intMax := toInt64Direct(first)
floatMax := toFloat64(first)
for _, item := range items[1:] {
v := extractField(item, keys)
if allInt {
if isIntType(v) {
n := toInt64Direct(v)
if n > intMax {
intMax = n
}
} else {
// Switch to float64 mode.
allInt = false
floatMax = float64(intMax)
f := toFloat64(v)
if f > floatMax {
floatMax = f
}
}
} else {
f := toFloat64(v)
if f > floatMax {
floatMax = f
}
}
}
if allInt {
return intMax
}
return floatMax
},
}
}