koazee icon indicating copy to clipboard operation
koazee copied to clipboard

Map() throws panic on empty stream

Open mykhailokulakov opened this issue 3 years ago • 1 comments

Describe the bug Map() throws panic if stream is empty or was filtered out

To Reproduce This code taken from examples. Just added Map():

package main

import (
	"strings"

	"github.com/wesovilabs/koazee"
)

type Person struct {
	Name string
	Male bool
	Age  int
}

var people = []*Person{
	{"John Smith", true, 32},
}

func main() {
	stream := koazee.
		StreamOf(people).
		Filter(func(person *Person) bool {
			return !person.Male
		}).
		Sort(func(person, otherPerson *Person) int {
			return strings.Compare(person.Name, otherPerson.Name)
		}).
		Map(func(person *Person) *string {
			return &person.Name
		})

	stream.Out().Val()
}

Expected behavior Should be no errors. Map() shall be skipped for empty streams

Current behaviour

panic: reflect: slice index out of range

goroutine 1 [running]:
reflect.Value.Index(0x65e9a0, 0xc000004540, 0x97, 0x0, 0x65d860, 0x196, 0x2030000)
	***/go/src/reflect/value.go:939 +0x1ee
github.com/wesovilabs/koazee/internal/maps.(*Map).validate(0xc00007b9b8, 0x40c801, 0xc68bff426f1e59c0)
	***/go/pkg/mod/github.com/wesovilabs/[email protected]/internal/maps/map.go:91 +0x817
github.com/wesovilabs/koazee/internal/maps.(*Map).Run(0xc00007b9b8, 0x4985b8, 0xc00007b968, 0x6e7d50, 0x0)
	***/go/pkg/mod/github.com/wesovilabs/[email protected]/internal/maps/map.go:21 +0x4a
github.com/wesovilabs/koazee/stream.(*streamMap).run(0xc000044220, 0x65e9a0, 0xc000004540, 0x65e9a0, 0xc000004540, 0x97, 0x717a00, 0x6549a0, 0x0, 0x0, ...)
	***/go/pkg/mod/github.com/wesovilabs/[email protected]/stream/map.go:13 +0xff
github.com/wesovilabs/koazee/stream.Stream.run(0x65e9a0, 0xc000004540, 0x65e9a0, 0xc000004540, 0x97, 0x717a00, 0x6549a0, 0x0, 0x0, 0xc00014e020, ...)
	***/go/pkg/mod/github.com/wesovilabs/[email protected]/stream/stream.go:162 +0xa0
github.com/wesovilabs/koazee/stream.Stream.run(0x65e9a0, 0xc000004540, 0x65e9a0, 0xc000004540, 0x97, 0x717a00, 0x6549a0, 0x0, 0x0, 0xc00014e020, ...)
	***/go/pkg/mod/github.com/wesovilabs/[email protected]/stream/stream.go:167 +0x13a
github.com/wesovilabs/koazee/stream.Stream.run(0x65e9a0, 0xc000004540, 0x65e9a0, 0xc000004540, 0x97, 0x717a00, 0x6549a0, 0x0, 0x0, 0xc00014e010, ...)
	***/go/pkg/mod/github.com/wesovilabs/[email protected]/stream/stream.go:167 +0x13a
github.com/wesovilabs/koazee/stream.Stream.Out(0x65e9a0, 0xc0000044a0, 0x65e9a0, 0xc0000044a0, 0x97, 0x717a00, 0x6549a0, 0x1, 0x0, 0xc00014e000, ...)
	***/go/pkg/mod/github.com/wesovilabs/[email protected]/stream/out.go:35 +0x5c
main.main()
	***/main.go:32 +0x28c

Process finished with exit code 2

Version - v0.0.5

mykhailokulakov avatar Sep 25 '20 13:09 mykhailokulakov

I also encountered the same problem. If the length is 0 after the filter, all the judgments are wrong.

AzraelJi avatar Aug 27 '21 02:08 AzraelJi