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

fix: add RIOE testcases

Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
This commit is contained in:
Dr. Carsten Leue
2023-09-19 22:31:55 +02:00
parent f74a407294
commit 600aeae770
10 changed files with 248 additions and 40 deletions

View File

@@ -1,29 +0,0 @@
// Copyright (c) 2023 IBM Corp.
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package file
import (
"io"
IOE "github.com/IBM/fp-go/ioeither"
)
// onClose closes a closeable resource
func onClose[R io.Closer](r R) IOE.IOEither[error, R] {
return IOE.TryCatchError(func() (R, error) {
return r, r.Close()
})
}

View File

@@ -16,6 +16,7 @@
package file
import (
"io"
"os"
IOE "github.com/IBM/fp-go/ioeither"
@@ -45,3 +46,10 @@ func Remove(name string) IOE.IOEither[error, string] {
return name, os.Remove(name)
})
}
// Close closes an object
func Close[C io.Closer](c C) IOE.IOEither[error, any] {
return IOE.TryCatchError(func() (any, error) {
return c, c.Close()
})
}

View File

@@ -31,7 +31,7 @@ func onReadAll[R io.Reader](r R) IOE.IOEither[error, []byte] {
func ReadAll[R io.ReadCloser](acquire IOE.IOEither[error, R]) IOE.IOEither[error, []byte] {
return IOE.WithResource[[]byte](
acquire,
onClose[R])(
Close[R])(
onReadAll[R],
)
}

View File

@@ -38,7 +38,7 @@ func TestWithTempFileOnClosedFile(t *testing.T) {
return F.Pipe2(
f,
onWriteAll[*os.File]([]byte("Carsten")),
IOE.ChainFirst(F.Constant1[[]byte](onClose(f))),
IOE.ChainFirst(F.Constant1[[]byte](Close(f))),
)
})

View File

@@ -36,7 +36,7 @@ func WriteAll[W io.WriteCloser](data []byte) func(acquire IOE.IOEither[error, W]
return func(onCreate IOE.IOEither[error, W]) IOE.IOEither[error, []byte] {
return IOE.WithResource[[]byte](
onCreate,
onClose[W])(
Close[W])(
onWrite,
)
}
@@ -46,5 +46,5 @@ func WriteAll[W io.WriteCloser](data []byte) func(acquire IOE.IOEither[error, W]
func Write[R any, W io.WriteCloser](acquire IOE.IOEither[error, W]) func(use func(W) IOE.IOEither[error, R]) IOE.IOEither[error, R] {
return IOE.WithResource[R](
acquire,
onClose[W])
Close[W])
}