Skip to content

Commit

Permalink
tpl/collections: Return en empty slice in after instead of error
Browse files Browse the repository at this point in the history
When the given index is out of bounds. So it can safely be used with `with` etc. without extra length checking.

Fixes #4894
  • Loading branch information
bep committed Jul 1, 2018
1 parent 78e8a74 commit f8212d2
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
2 changes: 1 addition & 1 deletion tpl/collections/collections.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (ns *Namespace) After(index interface{}, seq interface{}) (interface{}, err
}

if indexv >= seqv.Len() {
return nil, errors.New("no items left")
return seqv.Slice(0, 0).Interface(), nil
}

return seqv.Slice(indexv, seqv.Len()).Interface(), nil
Expand Down
7 changes: 4 additions & 3 deletions tpl/collections/collections_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,13 @@ func TestAfter(t *testing.T) {
expect interface{}
}{
{int(2), []string{"a", "b", "c", "d"}, []string{"c", "d"}},
{int32(3), []string{"a", "b"}, false},
{int32(3), []string{"a", "b"}, []string{}},
{int64(2), []int{100, 200, 300}, []int{300}},
{100, []int{100, 200}, false},
{100, []int{100, 200}, []int{}},
{"1", []int{100, 200, 300}, []int{200, 300}},
{int64(-1), []int{100, 200, 300}, false},
{"noint", []int{100, 200, 300}, false},
{2, []string{}, []string{}},
{1, nil, false},
{nil, []int{100}, false},
{1, t, false},
Expand All @@ -70,7 +71,7 @@ func TestAfter(t *testing.T) {
}

require.NoError(t, err, errMsg)
assert.Equal(t, test.expect, result, errMsg)
require.Equal(t, test.expect, result, errMsg)
}
}

Expand Down

0 comments on commit f8212d2

Please sign in to comment.