-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added function syntax for optional (#29)
- Loading branch information
1 parent
6a958bc
commit b2053f0
Showing
3 changed files
with
61 additions
and
1 deletion.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
optional/src/main/scala/henkan/optional/FunctionSyntax.scala
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package henkan.optional | ||
|
||
import cats.arrow.FunctionK | ||
import cats.{Functor, ApplicativeError} | ||
import cats.data.NonEmptyList | ||
import syntax.all._ | ||
|
||
trait FunctionSyntax { | ||
final class autoOptionalPartial[OptionalA, OptionalB] private[optional] { | ||
def apply[F[_], A, B](f: A ⇒ F[B])(whenMissing: NonEmptyList[RequiredFieldMissing] ⇒ F[OptionalB])( | ||
implicit | ||
toOptional: ToOptional[B, OptionalB], | ||
validateFromOptional: ValidateFromOptional[OptionalA, A], | ||
F: Functor[F] | ||
): OptionalA ⇒ F[OptionalB] = (oa: OptionalA) ⇒ { | ||
validate(oa).to[A].fold( | ||
whenMissing, | ||
a ⇒ F.map(f(a))(from(_).toOptional[OptionalB]) | ||
) | ||
} | ||
} | ||
|
||
def autoOptional[OptionalA, OptionalB] = new autoOptionalPartial[OptionalA, OptionalB] | ||
|
||
implicit class toOptionalOps[A, F[_]: Functor, B](f: A ⇒ F[B]) { | ||
def toOptional[OptionalA, OptionalB]( | ||
whenMissing: NonEmptyList[RequiredFieldMissing] ⇒ F[OptionalB] | ||
)( | ||
implicit | ||
toOptional: ToOptional[B, OptionalB], | ||
validateFromOptional: ValidateFromOptional[OptionalA, A] | ||
) = autoOptional[OptionalA, OptionalB](f)(whenMissing) | ||
} | ||
} |
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
25 changes: 25 additions & 0 deletions
25
optional/src/test/scala/henkan/optional/FunctionSyntaxSpec.scala
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package henkan.optional | ||
|
||
import cats.{Applicative, ApplicativeError} | ||
import org.specs2.mutable.Specification | ||
import cats.implicits._ | ||
import TestDomain._ | ||
import henkan.optional.syntax.function._ | ||
|
||
class FunctionSyntaxSpec extends Specification { | ||
override def is = s2""" | ||
FunctionSyntax | ||
wraps function with Optional Types $wrapFunction | ||
""" | ||
|
||
def wrapFunction = { | ||
val f: Domain ⇒ Either[String, ParentDomain] = (d: Domain) ⇒ Right(ParentDomain(1d, d)) | ||
|
||
val fm = f.toOptional[Message, ParentMessage](es ⇒ Left("Missing fields: " + es.map(_.fieldName).toList.mkString(", "))) | ||
|
||
fm(Message(Some("a"), Some(1))) must_== Right(ParentMessage(Some(1d), Some(Message(Some("a"), Some(1))))) | ||
|
||
fm(Message(Some("a"), None)) must_== Left("Missing fields: b") | ||
} | ||
} | ||
|