Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep compatibility of slice function #306

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions list.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ func slice(list interface{}, indices ...interface{}) interface{} {
func mustSlice(list interface{}, indices ...interface{}) (interface{}, error) {
tp := reflect.TypeOf(list).Kind()
switch tp {
case reflect.Slice, reflect.Array:
case reflect.Slice, reflect.Array, reflect.String:
l2 := reflect.ValueOf(list)

l := l2.Len()
Expand All @@ -442,7 +442,7 @@ func mustSlice(list interface{}, indices ...interface{}) (interface{}, error) {

return l2.Slice(start, end).Interface(), nil
default:
return nil, fmt.Errorf("list should be type of slice or array but %s", tp)
return nil, fmt.Errorf("list should be type of string, slice or array but %s", tp)
}
}

Expand Down
2 changes: 2 additions & 0 deletions list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ func TestSlice(t *testing.T) {
`{{ slice (list 1 2 3) 1 3 }}`: "[2 3]",
`{{ slice (list 1 2 3) 1 }}`: "[2 3]",
`{{ slice (regexSplit "/" "foo/bar/baz" -1) 1 2 }}`: "[bar]",
`{{ slice "test" 1 3 }}`: "es",
}
for tpl, expect := range tests {
assert.NoError(t, runt(tpl, expect))
Expand All @@ -344,6 +345,7 @@ func TestMustSlice(t *testing.T) {
`{{ mustSlice (list 1 2 3) 1 3 }}`: "[2 3]",
`{{ mustSlice (list 1 2 3) 1 }}`: "[2 3]",
`{{ mustSlice (regexSplit "/" "foo/bar/baz" -1) 1 2 }}`: "[bar]",
`{{ mustSlice "test" 1 3 }}`: "es",
}
for tpl, expect := range tests {
assert.NoError(t, runt(tpl, expect))
Expand Down