-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add .toEitherT
to Validated
#4554
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -265,6 +265,30 @@ sealed abstract class Validated[+E, +A] extends Product with Serializable { | |||||||||||
case Valid(a) => Right(a) | ||||||||||||
} | ||||||||||||
|
||||||||||||
/** | ||||||||||||
* Converts the value to an EitherT[F, E, A] | ||||||||||||
* | ||||||||||||
* Example: | ||||||||||||
* {{{ | ||||||||||||
* scala> import cats.syntax.all._ | ||||||||||||
* scala> given ExecutionContext = ExecutionContext.global | ||||||||||||
* | ||||||||||||
* scala> val v1 = "error".invalid[Int] | ||||||||||||
* scala> val v2 = 123.valid[String] | ||||||||||||
* | ||||||||||||
* scala> v1.toEitherT[Future].value | ||||||||||||
* res0: Future[Either[String, Int]] = Future(Left(error)) | ||||||||||||
* | ||||||||||||
* scala> v2.toEitherT[Future].value | ||||||||||||
* res1: Future[Either[String, Int]] = Future(Right(123)) | ||||||||||||
* }}} | ||||||||||||
*/ | ||||||||||||
def toEitherT[F[_]: Applicative]: EitherT[F, E, A] = | ||||||||||||
this match { | ||||||||||||
case Invalid(e) => EitherT.leftT(e) | ||||||||||||
case Valid(a) => EitherT.rightT(a) | ||||||||||||
} | ||||||||||||
Comment on lines
+287
to
+290
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And perhaps
Suggested change
is cleaner |
||||||||||||
|
||||||||||||
/** | ||||||||||||
* Returns Valid values wrapped in Some, and None for Invalid values | ||||||||||||
* | ||||||||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -298,9 +298,9 @@ But, what about if we want _another_ way of combining? We can provide our custom | |
Cats offers you a nice set of combinators for transforming your `Validated` based approach to an `Either` one and vice-versa. | ||
We've used `.toValidated` in our second example, now let's see how to use `.toEither`. | ||
|
||
#### From `Validated` to `Either` | ||
#### From `Validated` to `Either`/`EitherT` | ||
|
||
To do this, simply use `.toEither` combinator: | ||
To do this, simply use `.toEither`/`.toEitherT` combinator: | ||
|
||
```scala mdoc | ||
// Successful case | ||
|
@@ -319,7 +319,7 @@ FormValidatorNec.validateForm( | |
firstName = "John", | ||
lastName = "Doe", | ||
age = 5 | ||
).toEither | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The next sentence says we have an |
||
).toEitherT[Future] | ||
``` | ||
|
||
With this conversion, as you can see, we got an `Either` with a `NonEmptyChain` detailing the possible validation errors or our `RegistrationData` object. | ||
|
@@ -591,7 +591,7 @@ validatedMonad.tuple2(Validated.invalidNec[String, Int]("oops"), Validated.inval | |
``` | ||
|
||
This one short circuits! Therefore, if we were to define a `Monad` (or `FlatMap`) instance for `Validated` we would | ||
have to override `ap` to get the behavior we want. | ||
have to override `ap` to get the behavior we want. | ||
|
||
```scala mdoc:silent:nest | ||
import cats.Monad | ||
|
@@ -629,14 +629,14 @@ But then the behavior of `flatMap` would be inconsistent with that of `ap`, and | |
```scala | ||
// the `<->` operator means "is equivalent to" and returns a data structure | ||
// `IsEq` that is used to verify the equivalence of the two expressions | ||
def flatMapConsistentApply[A, B](fa: F[A], fab: F[A => B]): IsEq[F[B]] = | ||
def flatMapConsistentApply[A, B](fa: F[A], fab: F[A => B]): IsEq[F[B]] = | ||
fab.ap(fa) <-> fab.flatMap(f => fa.map(f)) | ||
``` | ||
|
||
```scala mdoc:silent | ||
import cats.laws._ | ||
|
||
val flatMapLawsForAccumulatingValidatedMonad = | ||
val flatMapLawsForAccumulatingValidatedMonad = | ||
FlatMapLaws[Validated[NonEmptyChain[String], *]](accumulatingValidatedMonad) | ||
|
||
val fa = Validated.invalidNec[String, Int]("oops") | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this is Scala 3 syntax, please replace it with the Scala 2 equivalent (which I assume doc comments are written in)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Didn't compile it myself, but I think this would do it: