Debian Codegraph @

util/sprig/functions.go

raw
1 package sprig
2
3 import (
4 "path"
5 "path/filepath"
6 "reflect"
7 "strings"
8 "time"
9 )
10
11 const (
12 loopExecutionLimit = 10_000 // Limit the number of loop executions to prevent execution from taking too long
13 stringLengthLimit = 100_000 // Limit the length of strings to prevent memory issues
14 sliceSizeLimit = 10_000 // Limit the size of slices to prevent memory issues
15 indentSpacesLimit = 100 // Limit indentation width to prevent memory issues; indent allocates spaces*lines bytes
16 )
17
18 // TxtFuncMap produces the function map.
19 //
20 // Use this to pass the functions into the template engine:
21 //
22 // tpl := template.New("foo").Funcs(sprig.FuncMap()))
23 //
24 // TxtFuncMap returns the function map as a plain map[string]any, assignable to any text/template or
25 // html/template FuncMap (including ntfy's vendored internal/template).
26 func TxtFuncMap() map[string]any {
27 return map[string]any{
28 // Date functions
29 "ago": dateAgo,
30 "date": date,
31 "dateInZone": dateInZone,
32 "dateModify": dateModify,
33 "duration": duration,
34 "durationRound": durationRound,
35 "htmlDate": htmlDate,
36 "htmlDateInZone": htmlDateInZone,
37 "mustDateModify": mustDateModify,
38 "mustToDate": mustToDate,
39 "now": time.Now,
40 "toDate": toDate,
41 "unixEpoch": unixEpoch,
42
43 // Strings
44 "trunc": trunc,
45 "trim": strings.TrimSpace,
46 "upper": strings.ToUpper,
47 "lower": strings.ToLower,
48 "title": title,
49 "substr": substring,
50 "repeat": repeat,
51 "trimAll": trimAll,
52 "trimPrefix": trimPrefix,
53 "trimSuffix": trimSuffix,
54 "contains": contains,
55 "hasPrefix": hasPrefix,
56 "hasSuffix": hasSuffix,
57 "quote": quote,
58 "squote": squote,
59 "cat": cat,
60 "indent": indent,
61 "nindent": nindent,
62 "replace": replace,
63 "plural": plural,
64 "sha1sum": sha1sum,
65 "sha256sum": sha256sum,
66 "sha512sum": sha512sum,
67 "adler32sum": adler32sum,
68 "toString": strval,
69
70 // Wrap Atoi to stop errors.
71 "atoi": atoi,
72 "seq": seq,
73 "toDecimal": toDecimal,
74 "split": split,
75 "splitList": splitList,
76 "splitn": splitn,
77 "toStrings": strslice,
78
79 "until": until,
80 "untilStep": untilStep,
81
82 // Basic arithmetic
83 "add1": add1,
84 "add": add,
85 "sub": sub,
86 "div": div,
87 "mod": mod,
88 "mul": mul,
89 "randInt": randInt,
90 "biggest": maxAsInt64,
91 "max": maxAsInt64,
92 "min": minAsInt64,
93 "maxf": maxAsFloat64,
94 "minf": minAsFloat64,
95 "ceil": ceil,
96 "floor": floor,
97 "round": round,
98
99 // string slices. Note that we reverse the order b/c that's better
100 // for template processing.
101 "join": join,
102 "sortAlpha": sortAlpha,
103
104 // Defaults
105 "default": defaultValue,
106 "empty": empty,
107 "coalesce": coalesce,
108 "all": all,
109 "any": anyNonEmpty,
110 "compact": compact,
111 "mustCompact": mustCompact,
112 "fromJSON": fromJSON,
113 "toJSON": toJSON,
114 "toPrettyJSON": toPrettyJSON,
115 "toRawJSON": toRawJSON,
116 "mustFromJSON": mustFromJSON,
117 "mustToJSON": mustToJSON,
118 "mustToPrettyJSON": mustToPrettyJSON,
119 "mustToRawJSON": mustToRawJSON,
120 "ternary": ternary,
121
122 // Reflection
123 "typeOf": typeOf,
124 "typeIs": typeIs,
125 "typeIsLike": typeIsLike,
126 "kindOf": kindOf,
127 "kindIs": kindIs,
128 "deepEqual": reflect.DeepEqual,
129
130 // Paths
131 "base": path.Base,
132 "dir": path.Dir,
133 "clean": path.Clean,
134 "ext": path.Ext,
135 "isAbs": path.IsAbs,
136
137 // Filepaths
138 "osBase": filepath.Base,
139 "osClean": filepath.Clean,
140 "osDir": filepath.Dir,
141 "osExt": filepath.Ext,
142 "osIsAbs": filepath.IsAbs,
143
144 // Encoding
145 "b64enc": base64encode,
146 "b64dec": base64decode,
147 "b32enc": base32encode,
148 "b32dec": base32decode,
149
150 // Data Structures
151 "tuple": list, // FIXME: with the addition of append/prepend these are no longer immutable.
152 "list": list,
153 "dict": dict,
154 "get": get,
155 "set": set,
156 "unset": unset,
157 "hasKey": hasKey,
158 "pluck": pluck,
159 "keys": keys,
160 "pick": pick,
161 "omit": omit,
162 "values": values,
163
164 "append": push,
165 "push": push,
166 "mustAppend": mustPush,
167 "mustPush": mustPush,
168 "prepend": prepend,
169 "mustPrepend": mustPrepend,
170 "first": first,
171 "mustFirst": mustFirst,
172 "rest": rest,
173 "mustRest": mustRest,
174 "last": last,
175 "mustLast": mustLast,
176 "initial": initial,
177 "mustInitial": mustInitial,
178 "reverse": reverse,
179 "mustReverse": mustReverse,
180 "uniq": uniq,
181 "mustUniq": mustUniq,
182 "without": without,
183 "mustWithout": mustWithout,
184 "has": has,
185 "mustHas": mustHas,
186 "slice": slice,
187 "mustSlice": mustSlice,
188 "concat": concat,
189 "dig": dig,
190 "chunk": chunk,
191 "mustChunk": mustChunk,
192
193 // Flow Control
194 "fail": fail,
195
196 // Regex
197 "regexMatch": regexMatch,
198 "mustRegexMatch": mustRegexMatch,
199 "regexFindAll": regexFindAll,
200 "mustRegexFindAll": mustRegexFindAll,
201 "regexFind": regexFind,
202 "mustRegexFind": mustRegexFind,
203 "regexReplaceAll": regexReplaceAll,
204 "mustRegexReplaceAll": mustRegexReplaceAll,
205 "regexReplaceAllLiteral": regexReplaceAllLiteral,
206 "mustRegexReplaceAllLiteral": mustRegexReplaceAllLiteral,
207 "regexSplit": regexSplit,
208 "mustRegexSplit": mustRegexSplit,
209 "regexQuoteMeta": regexQuoteMeta,
210
211 // URLs
212 "urlParse": urlParse,
213 "urlJoin": urlJoin,
214 }
215 }