functions.go 6.6 KB

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