This file contains hidden or 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
| object Conversions { | |
| implicit def thriftToDatetime(dateTime: ThriftDateTime): JodaDateTime = new JodaDateTime( | |
| dateTime.year, dateTime.month, dateTime.day, | |
| dateTime.hour, dateTime.minute, dateTime.second | |
| ) | |
| implicit def datetimeToThrift(dateTime: JodaDateTime): ThriftDateTime = ThriftDateTime( | |
| dateTime.getYear().asInstanceOf[Short], | |
| dateTime.getMonthOfYear().asInstanceOf[Byte], |
This file contains hidden or 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
| import Conversions._ | |
| class ConversionsSuite extends FunSuite { | |
| test("Check implicit conversion from joda to thrift dateTime works") { | |
| val now = JodaDateTime.now | |
| val nowInThrift: ThriftDateTime = now | |
| } | |
| } |
This file contains hidden or 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
| test("Check implicit conversion inside Option from joda to thrift dateTime works") { | |
| val nowOpt: Option[JodaDateTime] = Some(JodaDateTime.now) | |
| val nowOptInThrift: Option[ThriftDateTime] = nowOpt | |
| } |
This file contains hidden or 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
| def convert(jodaOpt: Option[JodaDateTime]): Option[ThriftDateTime] = | |
| jodaOpt.map (datetimeToThrift) |
This file contains hidden or 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
| trait Functor[F[_]] { | |
| /** Lift `f` into `F` and apply to `F[A]`. */ | |
| def map[A, B](fa: F[A])(f: A => B): F[B] | |
| } |
This file contains hidden or 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
| /** | |
| * Implicit conversions from C[JodaDateTime] -> C[ThriftDateTime] | |
| * Use import scalaz._ and Scalaz._ to get implicit functors for Option, List etc. | |
| */ | |
| implicit def liftConversionJodaToThrift[C[_]](wrappedJoda: C[JodaDateTime]) | |
| (implicit func: Functor[C]): C[ThriftDateTime] = | |
| func.map(wrappedJoda)(datetimeToThrift) | |
| /** | |
| * Implicit conversions from C[ThriftDateTime] -> C[JodaDateTime] |
This file contains hidden or 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
| import org.scalatest.FunSuite | |
| import Conversions._ | |
| import scalaz._ | |
| import Scalaz._ | |
| import company.thrift.{ DateTime => ThriftDateTime } | |
| import org.joda.time.{ DateTime => JodaDateTime } | |
| class ConversionsSuite extends FunSuite { | |
| test("Check implicit conversion inside Option from joda to thrift dateTime works") { |
This file contains hidden or 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
| class UserId(id: Int) extends AnyVal | |
| trait UserRegistrationFailures | |
| case object NicknameAlreadyExists extends UserRegistrationFailures | |
| case object EmailAlreadyExists extends UserRegistrationFailures | |
| trait UserService { | |
| def registerUser(nickname: String, email: String, password: String): Future[\/[NonEmptyList[UserRegistrationFailures], UserId]] | |
| } |
This file contains hidden or 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
| object optionForCompr { | |
| val opt1 = Some(1) | |
| val opt2 = Some(2) | |
| val opt3 = Some(3) | |
| for { | |
| r1 <-opt1 | |
| r2 <-opt2 | |
| r3 <-opt3 | |
| } yield r1 + r2 + r3 |
This file contains hidden or 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
| val right: \/[String, Int] = \/-(1) | |
| val left: \/[String, Int] = -\/("errorMessage") | |
| // or in another way (let the compiler infer the same types as above) | |
| val right2 = 1.right[String] | |
| val left2 = "errorMessage".left[Int] |