functions.go 5.3 KB

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