functions.go 6.4 KB

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