Skip to content
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

Add OptionOps.toInvalidNec and OptionOps.toValidNec #2555

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ Here's a (non-exhaustive) list of companies that use Cats in production. Don't s
- [e.near](http://enear.co)
- [E.ON](https://eon.com)
- [formation.ai](https://formation.ai)
- [Free2Move](https://free2move.com)
- [HomeAway](https://www.homeaway.com)
- [iHeartRadio](https://iheart.com)
- [ITV](https://www.itv.com/)
Expand Down
45 changes: 44 additions & 1 deletion core/src/main/scala/cats/syntax/option.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cats
package syntax

import cats.data.{Ior, OptionT, Validated, ValidatedNel}
import cats.data.{Ior, OptionT, Validated, ValidatedNel, ValidatedNec}
import cats.syntax.OptionOps.LiftToPartiallyApplied

trait OptionSyntax {
Expand Down Expand Up @@ -71,6 +71,28 @@ final class OptionOps[A](val oa: Option[A]) extends AnyVal {
*/
def toInvalidNel[B](b: => B): ValidatedNel[A, B] = oa.fold[ValidatedNel[A, B]](Validated.Valid(b))(Validated.invalidNel)

/**
* If the `Option` is a `Some`, wrap its value in a [[cats.data.Chain]]
* and return it in a [[cats.data.Validated.Invalid]].
* If the `Option` is `None`, return the provided `B` value in a
* [[cats.data.Validated.Valid]].
*
* Example:
* {{{
* scala> import cats.data.ValidatedNec
* scala> import cats.implicits._
*
* scala> val error1: Option[String] = Some("error!")
* scala> error1.toInvalidNec(3)
* res0: ValidatedNec[String, Int] = Invalid(Chain(error!))
*
* scala> val error2: Option[String] = None
* scala> error2.toInvalidNec(3)
* res1: ValidatedNec[String, Int] = Valid(3)
* }}}
*/
def toInvalidNec[B](b: => B): ValidatedNec[A, B] = oa.fold[ValidatedNec[A, B]](Validated.Valid(b))(Validated.invalidNec)

/**
* If the `Option` is a `Some`, return its value in a [[cats.data.Validated.Valid]].
* If the `Option` is `None`, return the provided `B` value in a
Expand Down Expand Up @@ -113,6 +135,27 @@ final class OptionOps[A](val oa: Option[A]) extends AnyVal {
*/
def toValidNel[B](b: => B): ValidatedNel[B, A] = oa.fold[ValidatedNel[B, A]](Validated.invalidNel(b))(Validated.Valid(_))

/**
* If the `Option` is a `Some`, return its value in a [[cats.data.Validated.Valid]].
* If the `Option` is `None`, wrap the provided `B` value in a [[cats.data.Chain]]
* and return the result in a [[cats.data.Validated.Invalid]].
*
* Example:
* {{{
* scala> import cats.data.ValidatedNec
* scala> import cats.implicits._
*
* scala> val result1: Option[Int] = Some(3)
* scala> result1.toValidNec("error!")
* res0: ValidatedNec[String, Int] = Valid(3)
*
* scala> val result2: Option[Int] = None
* scala> result2.toValidNec("error!")
* res1: ValidatedNec[String, Int] = Invalid(Chain(error!))
* }}}
*/
def toValidNec[B](b: => B): ValidatedNec[B, A] = oa.fold[ValidatedNec[B, A]](Validated.invalidNec(b))(Validated.Valid(_))

/**
* If the `Option` is a `Some`, return its value in a [[cats.data.Ior.Right]].
* If the `Option` is `None`, wrap the provided `B` value in a [[cats.data.Ior.Left]]
Expand Down