Skip to content

Commit

Permalink
fix: small changes to readall
Browse files Browse the repository at this point in the history
Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
  • Loading branch information
CarstenLeue committed Feb 22, 2024
1 parent 85f8071 commit f615072
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions ioeither/file/readall.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,23 @@ package file
import (
"io"

FL "github.com/IBM/fp-go/file"
F "github.com/IBM/fp-go/function"
IOE "github.com/IBM/fp-go/ioeither"
)

func onReadAll[R io.Reader](r R) IOE.IOEither[error, []byte] {
return IOE.TryCatchError(func() ([]byte, error) {
return io.ReadAll(r)
})
}
var (
// readAll is the adapted version of [io.ReadAll]
readAll = IOE.Eitherize1(io.ReadAll)
)

// ReadAll uses a generator function to create a stream, reads it and closes it
func ReadAll[R io.ReadCloser](acquire IOE.IOEither[error, R]) IOE.IOEither[error, []byte] {
return IOE.WithResource[[]byte](
acquire,
Close[R])(
onReadAll[R],
return F.Pipe1(
F.Flow2(
FL.ToReader[R],
readAll,
),
IOE.WithResource[[]byte](acquire, Close[R]),
)
}

0 comments on commit f615072

Please sign in to comment.