Skip to content

Commit

Permalink
Merge pull request #203 from jbripley/gzippedbase64string-implicit
Browse files Browse the repository at this point in the history
Add implicit conversion for GZippedBase64String to String
  • Loading branch information
johnynek committed Mar 16, 2015
2 parents a76a558 + bf0386d commit 3b28070
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(_))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand All @@ -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]") {
Expand Down

0 comments on commit 3b28070

Please sign in to comment.