functions.go 6.5 KB

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