diff --git a/bijection-core/src/main/scala/com/twitter/bijection/BinaryBijections.scala b/bijection-core/src/main/scala/com/twitter/bijection/BinaryBijections.scala index e45f8a7bc..939dc108e 100644 --- a/bijection-core/src/main/scala/com/twitter/bijection/BinaryBijections.scala +++ b/bijection-core/src/main/scala/com/twitter/bijection/BinaryBijections.scala @@ -30,13 +30,21 @@ case class GZippedBytes(bytes: Array[Byte]) extends AnyVal { case class GZippedBase64String(str: String) extends AnyVal +object GZippedBase64String { + implicit val unwrap: Injection[GZippedBase64String, String] = + new AbstractInjection[GZippedBase64String, String] { + override def apply(gzbs: GZippedBase64String) = gzbs.str + override def invert(str: String) = attemptWhen(str)(Base64.isBase64)(GZippedBase64String(_)) + } +} + case class Base64String(str: String) extends AnyVal object Base64String { implicit val unwrap: Injection[Base64String, String] = new AbstractInjection[Base64String, String] { override def apply(bs: Base64String) = bs.str - override def invert(str: String) = attemptWhen(str)(Base64.isBase64(_))(Base64String(_)) + override def invert(str: String) = attemptWhen(str)(Base64.isBase64)(Base64String(_)) } } diff --git a/bijection-core/src/test/scala/com/twitter/bijection/BinaryBijectionLaws.scala b/bijection-core/src/test/scala/com/twitter/bijection/BinaryBijectionLaws.scala index a32d3275c..0ef9c5ab0 100644 --- a/bijection-core/src/test/scala/com/twitter/bijection/BinaryBijectionLaws.scala +++ b/bijection-core/src/test/scala/com/twitter/bijection/BinaryBijectionLaws.scala @@ -17,14 +17,14 @@ limitations under the License. package com.twitter.bijection import java.nio.ByteBuffer -import org.scalatest.{ PropSpec, MustMatchers } -import org.scalatest.prop.PropertyChecks -import org.scalacheck.Prop._ +import com.twitter.bijection.codec.Base64 +import org.scalacheck.Arbitrary +import org.scalatest.MustMatchers class BinaryBijectionLaws extends CheckProperties with MustMatchers with BaseProperties { - implicit val arbBB = arbitraryViaFn[Array[Byte], ByteBuffer] { ByteBuffer.wrap(_) } + implicit val arbBB = arbitraryViaFn[Array[Byte], ByteBuffer] { ByteBuffer.wrap } // TODO: These are all bijections, property("Array[Byte] <=> ByteBuffer") { @@ -40,10 +40,24 @@ class BinaryBijectionLaws extends CheckProperties with MustMatchers isInjective[Array[Byte], Base64String] } + property("Base64String -> String") { + implicit val arbB64: Arbitrary[Base64String] = arbitraryViaFn { s: String => + Base64String(Base64.encodeBase64String(s.getBytes("UTF-8"))) + } + isInjection[Base64String, String] + } + property("rts Array[Byte] -> GZippedBase64String") { isInjective[Array[Byte], GZippedBase64String] } + property("GZippedBase64String -> String") { + implicit val arbGzB64: Arbitrary[GZippedBase64String] = arbitraryViaFn { s: String => + GZippedBase64String(Base64.encodeBase64String(s.getBytes("UTF-8"))) + } + isInjection[GZippedBase64String, String] + } + implicit val optSer = JavaSerializationInjection[Option[Int]] property("java serialize Option[Int]") {