1
0
mirror of https://github.com/IBM/fp-go.git synced 2025-08-10 22:31:32 +02:00

fix: small changes to readall

Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
This commit is contained in:
Dr. Carsten Leue
2024-02-22 13:58:39 +01:00
parent 85f8071c75
commit f61507254d

View File

@@ -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]),
)
}