functions.go 5.3 KB

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