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

fix: automate eitherize methods

Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
This commit is contained in:
Dr. Carsten Leue
2023-07-18 15:24:00 +02:00
parent 2efe4f64c5
commit eb4975e86d
60 changed files with 2106 additions and 430 deletions

View File

@@ -0,0 +1,19 @@
package generic
import (
"context"
E "github.com/ibm/fp-go/either"
ET "github.com/ibm/fp-go/either"
IOE "github.com/ibm/fp-go/ioeither/generic"
)
// withContext wraps an existing IOEither and performs a context check for cancellation before delegating
func WithContext[GIO ~func() E.Either[error, A], A any](ctx context.Context, ma GIO) GIO {
return IOE.MakeIO[GIO](func() E.Either[error, A] {
if err := context.Cause(ctx); err != nil {
return ET.Left[A](err)
}
return ma()
})
}

View File

@@ -3,16 +3,11 @@ package ioeither
import (
"context"
ET "github.com/ibm/fp-go/either"
G "github.com/ibm/fp-go/context/ioeither/generic"
IOE "github.com/ibm/fp-go/ioeither"
)
// withContext wraps an existing IOEither and performs a context check for cancellation before delegating
func WithContext[A any](ctx context.Context, ma IOE.IOEither[error, A]) IOE.IOEither[error, A] {
return IOE.MakeIO(func() ET.Either[error, A] {
if err := context.Cause(ctx); err != nil {
return ET.Left[A](err)
}
return ma()
})
return G.WithContext(ctx, ma)
}