Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
slim-sprig
Manage
Activity
Members
Plan
Wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
CodeLinaro
yocto-mirrors
github
go-task
slim-sprig
Commits
e3ce2356
Commit
e3ce2356
authored
4 years ago
by
Karel Bilek
Browse files
Options
Downloads
Patches
Plain Diff
Add `chunk` function to split array into smaller
parent
a81d0aad
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
docs/lists.md
+10
-0
10 additions, 0 deletions
docs/lists.md
functions.go
+2
-0
2 additions, 0 deletions
functions.go
list.go
+45
-0
45 additions, 0 deletions
list.go
list_test.go
+26
-0
26 additions, 0 deletions
list_test.go
with
83 additions
and
0 deletions
docs/lists.md
+
10
−
0
View file @
e3ce2356
...
...
@@ -168,6 +168,16 @@ equivalent of `list[n:m]`.
`slice`
panics if there is a problem while
`mustSlice`
returns an error to the
template engine if there is a problem.
## chunk
To split a list into chunks of given size, use
`chunk size list`
. This is useful for pagination.
```
chunk 3 (list 1 2 3 4 5 6 7 8)
```
This produces list of lists
`[ [ 1 2 3 ] [ 4 5 6 ] [ 7 8 ] ]`
.
## A Note on List Internals
A list is implemented in Go as a
`[]interface{}`
. For Go developers embedding
...
...
This diff is collapsed.
Click to expand it.
functions.go
+
2
−
0
View file @
e3ce2356
...
...
@@ -313,6 +313,8 @@ var genericMap = map[string]interface{}{
"mustSlice"
:
mustSlice
,
"concat"
:
concat
,
"dig"
:
dig
,
"chunk"
:
chunk
,
"mustChunk"
:
mustChunk
,
// Crypto:
"bcrypt"
:
bcrypt
,
...
...
This diff is collapsed.
Click to expand it.
list.go
+
45
−
0
View file @
e3ce2356
...
...
@@ -2,6 +2,7 @@ package sprig
import
(
"fmt"
"math"
"reflect"
"sort"
)
...
...
@@ -72,6 +73,50 @@ func mustPrepend(list interface{}, v interface{}) ([]interface{}, error) {
}
}
func
chunk
(
size
int
,
list
interface
{})
[][]
interface
{}
{
l
,
err
:=
mustChunk
(
size
,
list
)
if
err
!=
nil
{
panic
(
err
)
}
return
l
}
func
mustChunk
(
size
int
,
list
interface
{})
([][]
interface
{},
error
)
{
tp
:=
reflect
.
TypeOf
(
list
)
.
Kind
()
switch
tp
{
case
reflect
.
Slice
,
reflect
.
Array
:
l2
:=
reflect
.
ValueOf
(
list
)
l
:=
l2
.
Len
()
cs
:=
int
(
math
.
Floor
(
float64
(
l
-
1
)
/
float64
(
size
))
+
1
)
nl
:=
make
([][]
interface
{},
cs
)
for
i
:=
0
;
i
<
cs
;
i
++
{
clen
:=
size
if
i
==
cs
-
1
{
clen
=
int
(
math
.
Floor
(
math
.
Mod
(
float64
(
l
),
float64
(
size
))))
if
clen
==
0
{
clen
=
size
}
}
nl
[
i
]
=
make
([]
interface
{},
clen
)
for
j
:=
0
;
j
<
clen
;
j
++
{
ix
:=
i
*
size
+
j
nl
[
i
][
j
]
=
l2
.
Index
(
ix
)
.
Interface
()
}
}
return
nl
,
nil
default
:
return
nil
,
fmt
.
Errorf
(
"Cannot chunk type %s"
,
tp
)
}
}
func
last
(
list
interface
{})
interface
{}
{
l
,
err
:=
mustLast
(
list
)
if
err
!=
nil
{
...
...
This diff is collapsed.
Click to expand it.
list_test.go
+
26
−
0
View file @
e3ce2356
...
...
@@ -44,6 +44,32 @@ func TestMustPush(t *testing.T) {
}
}
func
TestChunk
(
t
*
testing
.
T
)
{
tests
:=
map
[
string
]
string
{
`{{ tuple 1 2 3 4 5 6 7 | chunk 3 | len }}`
:
"3"
,
`{{ tuple | chunk 3 | len }}`
:
"0"
,
`{{ range ( tuple 1 2 3 4 5 6 7 8 9 | chunk 3 ) }}{{. | join "-"}}|{{end}}`
:
"1-2-3|4-5-6|7-8-9|"
,
`{{ range ( tuple 1 2 3 4 5 6 7 8 | chunk 3 ) }}{{. | join "-"}}|{{end}}`
:
"1-2-3|4-5-6|7-8|"
,
`{{ range ( tuple 1 2 | chunk 3 ) }}{{. | join "-"}}|{{end}}`
:
"1-2|"
,
}
for
tpl
,
expect
:=
range
tests
{
assert
.
NoError
(
t
,
runt
(
tpl
,
expect
))
}
}
func
TestMustChunk
(
t
*
testing
.
T
)
{
tests
:=
map
[
string
]
string
{
`{{ tuple 1 2 3 4 5 6 7 | mustChunk 3 | len }}`
:
"3"
,
`{{ tuple | mustChunk 3 | len }}`
:
"0"
,
`{{ range ( tuple 1 2 3 4 5 6 7 8 9 | mustChunk 3 ) }}{{. | join "-"}}|{{end}}`
:
"1-2-3|4-5-6|7-8-9|"
,
`{{ range ( tuple 1 2 3 4 5 6 7 8 | mustChunk 3 ) }}{{. | join "-"}}|{{end}}`
:
"1-2-3|4-5-6|7-8|"
,
`{{ range ( tuple 1 2 | mustChunk 3 ) }}{{. | join "-"}}|{{end}}`
:
"1-2|"
,
}
for
tpl
,
expect
:=
range
tests
{
assert
.
NoError
(
t
,
runt
(
tpl
,
expect
))
}
}
func
TestPrepend
(
t
*
testing
.
T
)
{
tests
:=
map
[
string
]
string
{
`{{ $t := tuple 1 2 3 }}{{ prepend $t 0 | len }}`
:
"4"
,
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment