list.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. package sprig
  2. import (
  3. "fmt"
  4. "math"
  5. "reflect"
  6. "sort"
  7. )
  8. func list(v ...interface{}) []interface{} {
  9. return v
  10. }
  11. func push(list interface{}, v interface{}) []interface{} {
  12. l, err := mustPush(list, v)
  13. if err != nil {
  14. panic(err)
  15. }
  16. return l
  17. }
  18. func mustPush(list interface{}, v interface{}) ([]interface{}, error) {
  19. tp := reflect.TypeOf(list).Kind()
  20. switch tp {
  21. case reflect.Slice, reflect.Array:
  22. l2 := reflect.ValueOf(list)
  23. l := l2.Len()
  24. nl := make([]interface{}, l)
  25. for i := 0; i < l; i++ {
  26. nl[i] = l2.Index(i).Interface()
  27. }
  28. return append(nl, v), nil
  29. default:
  30. return nil, fmt.Errorf("Cannot push on type %s", tp)
  31. }
  32. }
  33. func prepend(list interface{}, v interface{}) []interface{} {
  34. l, err := mustPrepend(list, v)
  35. if err != nil {
  36. panic(err)
  37. }
  38. return l
  39. }
  40. func mustPrepend(list interface{}, v interface{}) ([]interface{}, error) {
  41. tp := reflect.TypeOf(list).Kind()
  42. switch tp {
  43. case reflect.Slice, reflect.Array:
  44. l2 := reflect.ValueOf(list)
  45. l := l2.Len()
  46. nl := make([]interface{}, l)
  47. for i := 0; i < l; i++ {
  48. nl[i] = l2.Index(i).Interface()
  49. }
  50. return append([]interface{}{v}, nl...), nil
  51. default:
  52. return nil, fmt.Errorf("Cannot prepend on type %s", tp)
  53. }
  54. }
  55. func chunk(size int, list interface{}) [][]interface{} {
  56. l, err := mustChunk(size, list)
  57. if err != nil {
  58. panic(err)
  59. }
  60. return l
  61. }
  62. func mustChunk(size int, list interface{}) ([][]interface{}, error) {
  63. tp := reflect.TypeOf(list).Kind()
  64. switch tp {
  65. case reflect.Slice, reflect.Array:
  66. l2 := reflect.ValueOf(list)
  67. l := l2.Len()
  68. cs := int(math.Floor(float64(l-1)/float64(size)) + 1)
  69. nl := make([][]interface{}, cs)
  70. for i := 0; i < cs; i++ {
  71. clen := size
  72. if i == cs-1 {
  73. clen = int(math.Floor(math.Mod(float64(l), float64(size))))
  74. if clen == 0 {
  75. clen = size
  76. }
  77. }
  78. nl[i] = make([]interface{}, clen)
  79. for j := 0; j < clen; j++ {
  80. ix := i*size + j
  81. nl[i][j] = l2.Index(ix).Interface()
  82. }
  83. }
  84. return nl, nil
  85. default:
  86. return nil, fmt.Errorf("Cannot chunk type %s", tp)
  87. }
  88. }
  89. func last(list interface{}) interface{} {
  90. l, err := mustLast(list)
  91. if err != nil {
  92. panic(err)
  93. }
  94. return l
  95. }
  96. func mustLast(list interface{}) (interface{}, error) {
  97. tp := reflect.TypeOf(list).Kind()
  98. switch tp {
  99. case reflect.Slice, reflect.Array:
  100. l2 := reflect.ValueOf(list)
  101. l := l2.Len()
  102. if l == 0 {
  103. return nil, nil
  104. }
  105. return l2.Index(l - 1).Interface(), nil
  106. default:
  107. return nil, fmt.Errorf("Cannot find last on type %s", tp)
  108. }
  109. }
  110. func first(list interface{}) interface{} {
  111. l, err := mustFirst(list)
  112. if err != nil {
  113. panic(err)
  114. }
  115. return l
  116. }
  117. func mustFirst(list interface{}) (interface{}, error) {
  118. tp := reflect.TypeOf(list).Kind()
  119. switch tp {
  120. case reflect.Slice, reflect.Array:
  121. l2 := reflect.ValueOf(list)
  122. l := l2.Len()
  123. if l == 0 {
  124. return nil, nil
  125. }
  126. return l2.Index(0).Interface(), nil
  127. default:
  128. return nil, fmt.Errorf("Cannot find first on type %s", tp)
  129. }
  130. }
  131. func rest(list interface{}) []interface{} {
  132. l, err := mustRest(list)
  133. if err != nil {
  134. panic(err)
  135. }
  136. return l
  137. }
  138. func mustRest(list interface{}) ([]interface{}, error) {
  139. tp := reflect.TypeOf(list).Kind()
  140. switch tp {
  141. case reflect.Slice, reflect.Array:
  142. l2 := reflect.ValueOf(list)
  143. l := l2.Len()
  144. if l == 0 {
  145. return nil, nil
  146. }
  147. nl := make([]interface{}, l-1)
  148. for i := 1; i < l; i++ {
  149. nl[i-1] = l2.Index(i).Interface()
  150. }
  151. return nl, nil
  152. default:
  153. return nil, fmt.Errorf("Cannot find rest on type %s", tp)
  154. }
  155. }
  156. func initial(list interface{}) []interface{} {
  157. l, err := mustInitial(list)
  158. if err != nil {
  159. panic(err)
  160. }
  161. return l
  162. }
  163. func mustInitial(list interface{}) ([]interface{}, error) {
  164. tp := reflect.TypeOf(list).Kind()
  165. switch tp {
  166. case reflect.Slice, reflect.Array:
  167. l2 := reflect.ValueOf(list)
  168. l := l2.Len()
  169. if l == 0 {
  170. return nil, nil
  171. }
  172. nl := make([]interface{}, l-1)
  173. for i := 0; i < l-1; i++ {
  174. nl[i] = l2.Index(i).Interface()
  175. }
  176. return nl, nil
  177. default:
  178. return nil, fmt.Errorf("Cannot find initial on type %s", tp)
  179. }
  180. }
  181. func sortAlpha(list interface{}) []string {
  182. k := reflect.Indirect(reflect.ValueOf(list)).Kind()
  183. switch k {
  184. case reflect.Slice, reflect.Array:
  185. a := strslice(list)
  186. s := sort.StringSlice(a)
  187. s.Sort()
  188. return s
  189. }
  190. return []string{strval(list)}
  191. }
  192. func reverse(v interface{}) []interface{} {
  193. l, err := mustReverse(v)
  194. if err != nil {
  195. panic(err)
  196. }
  197. return l
  198. }
  199. func mustReverse(v interface{}) ([]interface{}, error) {
  200. tp := reflect.TypeOf(v).Kind()
  201. switch tp {
  202. case reflect.Slice, reflect.Array:
  203. l2 := reflect.ValueOf(v)
  204. l := l2.Len()
  205. nl := make([]interface{}, l)
  206. for i := 0; i < l; i++ {
  207. nl[l-i-1] = l2.Index(i).Interface()
  208. }
  209. return nl, nil
  210. default:
  211. return nil, fmt.Errorf("Cannot find reverse on type %s", tp)
  212. }
  213. }
  214. func compact(list interface{}) []interface{} {
  215. l, err := mustCompact(list)
  216. if err != nil {
  217. panic(err)
  218. }
  219. return l
  220. }
  221. func mustCompact(list interface{}) ([]interface{}, error) {
  222. tp := reflect.TypeOf(list).Kind()
  223. switch tp {
  224. case reflect.Slice, reflect.Array:
  225. l2 := reflect.ValueOf(list)
  226. l := l2.Len()
  227. nl := []interface{}{}
  228. var item interface{}
  229. for i := 0; i < l; i++ {
  230. item = l2.Index(i).Interface()
  231. if !empty(item) {
  232. nl = append(nl, item)
  233. }
  234. }
  235. return nl, nil
  236. default:
  237. return nil, fmt.Errorf("Cannot compact on type %s", tp)
  238. }
  239. }
  240. func uniq(list interface{}) []interface{} {
  241. l, err := mustUniq(list)
  242. if err != nil {
  243. panic(err)
  244. }
  245. return l
  246. }
  247. func mustUniq(list interface{}) ([]interface{}, error) {
  248. tp := reflect.TypeOf(list).Kind()
  249. switch tp {
  250. case reflect.Slice, reflect.Array:
  251. l2 := reflect.ValueOf(list)
  252. l := l2.Len()
  253. dest := []interface{}{}
  254. var item interface{}
  255. for i := 0; i < l; i++ {
  256. item = l2.Index(i).Interface()
  257. if !inList(dest, item) {
  258. dest = append(dest, item)
  259. }
  260. }
  261. return dest, nil
  262. default:
  263. return nil, fmt.Errorf("Cannot find uniq on type %s", tp)
  264. }
  265. }
  266. func inList(haystack []interface{}, needle interface{}) bool {
  267. for _, h := range haystack {
  268. if reflect.DeepEqual(needle, h) {
  269. return true
  270. }
  271. }
  272. return false
  273. }
  274. func without(list interface{}, omit ...interface{}) []interface{} {
  275. l, err := mustWithout(list, omit...)
  276. if err != nil {
  277. panic(err)
  278. }
  279. return l
  280. }
  281. func mustWithout(list interface{}, omit ...interface{}) ([]interface{}, error) {
  282. tp := reflect.TypeOf(list).Kind()
  283. switch tp {
  284. case reflect.Slice, reflect.Array:
  285. l2 := reflect.ValueOf(list)
  286. l := l2.Len()
  287. res := []interface{}{}
  288. var item interface{}
  289. for i := 0; i < l; i++ {
  290. item = l2.Index(i).Interface()
  291. if !inList(omit, item) {
  292. res = append(res, item)
  293. }
  294. }
  295. return res, nil
  296. default:
  297. return nil, fmt.Errorf("Cannot find without on type %s", tp)
  298. }
  299. }
  300. func has(needle interface{}, haystack interface{}) bool {
  301. l, err := mustHas(needle, haystack)
  302. if err != nil {
  303. panic(err)
  304. }
  305. return l
  306. }
  307. func mustHas(needle interface{}, haystack interface{}) (bool, error) {
  308. if haystack == nil {
  309. return false, nil
  310. }
  311. tp := reflect.TypeOf(haystack).Kind()
  312. switch tp {
  313. case reflect.Slice, reflect.Array:
  314. l2 := reflect.ValueOf(haystack)
  315. var item interface{}
  316. l := l2.Len()
  317. for i := 0; i < l; i++ {
  318. item = l2.Index(i).Interface()
  319. if reflect.DeepEqual(needle, item) {
  320. return true, nil
  321. }
  322. }
  323. return false, nil
  324. default:
  325. return false, fmt.Errorf("Cannot find has on type %s", tp)
  326. }
  327. }
  328. func slice(list interface{}, indices ...interface{}) interface{} {
  329. l, err := mustSlice(list, indices...)
  330. if err != nil {
  331. panic(err)
  332. }
  333. return l
  334. }
  335. func mustSlice(list interface{}, indices ...interface{}) (interface{}, error) {
  336. tp := reflect.TypeOf(list).Kind()
  337. switch tp {
  338. case reflect.Slice, reflect.Array:
  339. l2 := reflect.ValueOf(list)
  340. l := l2.Len()
  341. if l == 0 {
  342. return nil, nil
  343. }
  344. var start, end int
  345. if len(indices) > 0 {
  346. start = toInt(indices[0])
  347. }
  348. if len(indices) < 2 {
  349. end = l
  350. } else {
  351. end = toInt(indices[1])
  352. }
  353. return l2.Slice(start, end).Interface(), nil
  354. default:
  355. return nil, fmt.Errorf("list should be type of slice or array but %s", tp)
  356. }
  357. }
  358. func concat(lists ...interface{}) interface{} {
  359. var res []interface{}
  360. for _, list := range lists {
  361. tp := reflect.TypeOf(list).Kind()
  362. switch tp {
  363. case reflect.Slice, reflect.Array:
  364. l2 := reflect.ValueOf(list)
  365. for i := 0; i < l2.Len(); i++ {
  366. res = append(res, l2.Index(i).Interface())
  367. }
  368. default:
  369. panic(fmt.Sprintf("Cannot concat type %s as list", tp))
  370. }
  371. }
  372. return res
  373. }