functions.go 5.2 KB

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