tpl: Last now accepts 0 as limit
authorBaibhav Vatsa <baibhavvatsa@gmail.com>
Fri, 11 Oct 2019 18:15:39 +0000 (13:15 -0500)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Fri, 11 Oct 2019 18:34:51 +0000 (20:34 +0200)
Modified the if conditional because of which last threw an error if 0 was passed as limit. The function now returns an empty slice if it is called with 0 as limit. The behavior of first and last is now the same when 0 is passed as limit. Also added tests to test the new behavior.

Fixes #6419

tpl/collections/collections.go
tpl/collections/collections_test.go

index eb6ecb9bbbae2d59203a9d2fb224a1ab9a0739c5..95079436fde618fdb8b6e42b91b3718b02a1370d 100644 (file)
@@ -378,7 +378,7 @@ func (ns *Namespace) Last(limit interface{}, seq interface{}) (interface{}, erro
                return nil, err
        }
 
-       if limitv < 1 {
+       if limitv < 0 {
                return nil, errors.New("can't return negative/empty count of items from sequence")
        }
 
index 781f44e0a29444103ae535fe317bdfd72cd1dbda..2c68a776617dbd1230d009c15b0242152f7b9e3f 100644 (file)
@@ -495,6 +495,8 @@ func TestLast(t *testing.T) {
                {int64(2), []int{100, 200, 300}, []int{200, 300}},
                {100, []int{100, 200}, []int{100, 200}},
                {"1", []int{100, 200, 300}, []int{300}},
+               {"0", []int{100, 200, 300}, []int{}},
+               {"0", []string{"a", "b", "c"}, []string{}},
                // errors
                {int64(-1), []int{100, 200, 300}, false},
                {"noint", []int{100, 200, 300}, false},