prop> (b: Boolean) => | DynamoFormat[Boolean].read(DynamoFormat[Boolean].write(b)) == cats.data.Xor.right(b)
Returns a DynamoFormat for the case where A
can always be converted B
,
with write
, but read
may throw an exception for some value of B
Returns a DynamoFormat for the case where A
can always be converted B
,
with write
, but read
may throw an exception for some value of B
>>> import org.joda.time._ >>> val jodaStringFormat = DynamoFormat.coercedXmap[LocalDate, String, IllegalArgumentException]( ... LocalDate.parse ... )( ... _.toString ... ) >>> jodaStringFormat.read(jodaStringFormat.write(new LocalDate(2007, 8, 18))) Right(2007-08-18) >>> import com.amazonaws.services.dynamodbv2.model.AttributeValue >>> jodaStringFormat.read(new AttributeValue().withS("Togtogdenoggleplop")) Left(TypeCoercionError(java.lang.IllegalArgumentException: Invalid format: "Togtogdenoggleplop"))
prop> (d: Double) => | DynamoFormat[Double].read(DynamoFormat[Double].write(d)) == cats.data.Xor.right(d)
prop> import com.amazonaws.services.dynamodbv2.model.AttributeValue prop> (s: Set[Double]) => | val av = new AttributeValue().withNS(s.map(_.toString).toList: _*) | DynamoFormat[Set[Double]].write(s) == av && | DynamoFormat[Set[Double]].read(av) == cats.data.Xor.right(s)
prop> (i: Int) => | DynamoFormat[Int].read(DynamoFormat[Int].write(i)) == cats.data.Xor.right(i)
prop> import com.amazonaws.services.dynamodbv2.model.AttributeValue prop> (s: Set[Int]) => | val av = new AttributeValue().withNS(s.map(_.toString).toList: _*) | DynamoFormat[Set[Int]].write(s) == av && | DynamoFormat[Set[Int]].read(av) == cats.data.Xor.right(s)
prop> (l: List[String]) => | DynamoFormat[List[String]].read(DynamoFormat[List[String]].write(l)) == | cats.data.Xor.right(l)
prop> (l: Long) => | DynamoFormat[Long].read(DynamoFormat[Long].write(l)) == cats.data.Xor.right(l)
prop> import com.amazonaws.services.dynamodbv2.model.AttributeValue prop> (s: Set[Long]) => | val av = new AttributeValue().withNS(s.map(_.toString).toList: _*) | DynamoFormat[Set[Long]].write(s) == av && | DynamoFormat[Set[Long]].read(av) == cats.data.Xor.right(s)
prop> (m: Map[String, Int]) => | DynamoFormat[Map[String, Int]].read(DynamoFormat[Map[String, Int]].write(m)) == | cats.data.Xor.right(m)
prop> (o: Option[Long]) => | DynamoFormat[Option[Long]].read(DynamoFormat[Option[Long]].write(o)) == | cats.data.Xor.right(o)
This ensures that if, for instance, you specify an update with Some(5) rather
than making the type of Option
explicit, it doesn't fall back to auto-derivation
prop> (s: String) => | DynamoFormat[String].read(DynamoFormat[String].write(s)) == cats.data.Xor.right(s)
prop> import com.amazonaws.services.dynamodbv2.model.AttributeValue prop> (s: Set[String]) => | val av = new AttributeValue().withSS(s.toList: _*) | DynamoFormat[Set[String]].write(s) == av && | DynamoFormat[Set[String]].read(av) == cats.data.Xor.right(s)
prop> (l: Vector[String]) => | DynamoFormat[Vector[String]].read(DynamoFormat[Vector[String]].write(l)) == | cats.data.Xor.right(l)
>>> import org.joda.time._ >>> import cats.data.Xor >>> import com.amazonaws.services.dynamodbv2.model.AttributeValue >>> implicit val jodaLongFormat = DynamoFormat.xmap[DateTime, Long]( ... l => Xor.right(new DateTime(l).withZone(DateTimeZone.UTC)) ... )( ... _.withZone(DateTimeZone.UTC).getMillis ... ) >>> DynamoFormat[DateTime].read(new AttributeValue().withN("0")) Right(1970-01-01T00:00:00.000Z)