diff --git a/bijection-core/src/main/scala/com/twitter/bijection/CollectionInjections.scala b/bijection-core/src/main/scala/com/twitter/bijection/CollectionInjections.scala index 1e963571b..4ce627508 100644 --- a/bijection-core/src/main/scala/com/twitter/bijection/CollectionInjections.scala +++ b/bijection-core/src/main/scala/com/twitter/bijection/CollectionInjections.scala @@ -81,16 +81,22 @@ trait CollectionInjections extends StringInjections { } override def invert(d: D): Try[C] = { val builder = dc() - d foreach { b => - val thisB = inj.invert(b) + var failed = false + val iter = d.toIterator + while (iter.hasNext && !failed) { + val thisB = inj.invert(iter.next) if (thisB.isSuccess) { builder += thisB.get } else { - return InversionFailure.failedAttempt(d) + failed = true } } - val res = builder.result() - if (goodInv(d, res)) Success(res) else InversionFailure.failedAttempt(d) + if (failed) { + InversionFailure.failedAttempt(d) + } else { + val res = builder.result() + if (goodInv(d, res)) Success(res) else InversionFailure.failedAttempt(d) + } } } } diff --git a/bijection-core/src/main/scala/com/twitter/bijection/Injection.scala b/bijection-core/src/main/scala/com/twitter/bijection/Injection.scala index 163c238c5..67f9c2e64 100644 --- a/bijection-core/src/main/scala/com/twitter/bijection/Injection.scala +++ b/bijection-core/src/main/scala/com/twitter/bijection/Injection.scala @@ -148,8 +148,8 @@ object Injection extends CollectionInjections implicit def option[A]: Injection[A, Option[A]] = new AbstractInjection[A, Option[A]] { override def apply(a: A) = Some(a) - override def invert(b: Option[A]) = - b.toRight(InversionFailure(b, new NoSuchElementException())).fold(Failure(_), Success(_)) + override def invert(b: Option[A]): Try[A] = + b.toRight[InversionFailure](InversionFailure(b, new NoSuchElementException())).fold[Try[A]](Failure(_), Success(_)) } implicit def identity[A]: Injection[A, A] = new AbstractInjection[A, A] { diff --git a/bijection-protobuf/src/main/scala/com/twitter/bijection/protobuf/ProtobufCodecs.scala b/bijection-protobuf/src/main/scala/com/twitter/bijection/protobuf/ProtobufCodecs.scala index 2ef6ee91e..44fe77c48 100644 --- a/bijection-protobuf/src/main/scala/com/twitter/bijection/protobuf/ProtobufCodecs.scala +++ b/bijection-protobuf/src/main/scala/com/twitter/bijection/protobuf/ProtobufCodecs.scala @@ -6,7 +6,7 @@ import com.google.protobuf.Message import com.google.protobuf.ProtocolMessageEnum import java.lang.{ Integer => JInt } import scala.collection.mutable.{ Map => MMap } -import scala.util.{ Failure, Success } +import scala.util.{ Failure, Success, Try } import scala.reflect._ /** @@ -61,7 +61,7 @@ class ProtobufEnumCodec[T <: ProtocolMessageEnum](klass: Class[T]) extends Injec lazy val valueOf = klass.getMethod("valueOf", classOf[Int]) val cache = MMap[Int, T]() override def apply(enum: T) = enum.getNumber - override def invert(i: Int) = Option { + override def invert(i: Int): Try[T] = Option { cache.getOrElseUpdate(i, valueOf.invoke(null, i.as[JInt]).asInstanceOf[T]) - }.toRight(InversionFailure(i)).fold(Failure(_), Success(_)) + }.toRight(InversionFailure(i)).fold[Try[T]](Failure(_), Success(_)) } diff --git a/bijection-thrift/src/main/scala/com/twitter/bijection/thrift/ThriftCodecs.scala b/bijection-thrift/src/main/scala/com/twitter/bijection/thrift/ThriftCodecs.scala index 4ecfe5b68..5d7e7a811 100644 --- a/bijection-thrift/src/main/scala/com/twitter/bijection/thrift/ThriftCodecs.scala +++ b/bijection-thrift/src/main/scala/com/twitter/bijection/thrift/ThriftCodecs.scala @@ -14,7 +14,7 @@ import org.apache.thrift.transport.TIOStreamTransport import org.codehaus.jackson.map.MappingJsonFactory import java.lang.{ Integer => JInt } import scala.collection.mutable.{ Map => MMap } -import scala.util.{ Failure, Success } +import scala.util.{ Failure, Success, Try } import scala.reflect._ /** @@ -118,7 +118,7 @@ class TEnumCodec[T <: TEnum](klass: Class[T]) extends Injection[T, Int] { lazy val findByValue = klass.getMethod("findByValue", classOf[Int]) val cache = MMap[Int, T]() override def apply(enum: T) = enum.getValue - override def invert(i: Int) = Option { + override def invert(i: Int): Try[T] = Option { cache.getOrElseUpdate(i, findByValue.invoke(null, i.as[JInt]).asInstanceOf[T]) - }.toRight(InversionFailure(i)).fold(Failure(_), Success(_)) + }.toRight(InversionFailure(i)).fold[Try[T]](Failure(_), Success(_)) } diff --git a/project/Build.scala b/project/Build.scala index 581fcc822..76722e155 100644 --- a/project/Build.scala +++ b/project/Build.scala @@ -7,6 +7,7 @@ import com.typesafe.tools.mima.plugin.MimaKeys.previousArtifact import com.typesafe.sbt.osgi.SbtOsgi._ import scalariform.formatter.preferences._ import com.typesafe.sbt.SbtScalariform._ +import wartremover._ object BijectionBuild extends Build { @@ -15,7 +16,9 @@ object BijectionBuild extends Build { case _ => false } - val sharedSettings = Project.defaultSettings ++ osgiSettings ++ scalariformSettings ++ Seq( + + + val sharedSettings = Project.defaultSettings ++ osgiSettings ++ scalariformSettings ++ wartremoverSettings ++ Seq( organization := "com.twitter", crossScalaVersions := Seq("2.10.4", "2.11.5"), @@ -49,6 +52,8 @@ object BijectionBuild extends Build { Seq() }, + wartremoverErrors ++= Seq(Wart.Serializable, Wart.Any2StringAdd), + OsgiKeys.importPackage <<= scalaVersion { sv => Seq("""scala.*;version="$"""".format(sv)) }, OsgiKeys.importPackage ++= Seq("com.twitter.bijection.*;version=\"[${Bundle-Version}, ${Bundle-Version}]\"", "*"), @@ -164,6 +169,11 @@ object BijectionBuild extends Build { /** No dependencies in bijection other than java + scala */ lazy val bijectionCore = module("core").settings( osgiExportAll("com.twitter.bijection"), + wartremoverErrors ++= Seq(Wart.Any, Wart.Product, Wart.Return), + + // Excluded since it contains Any which we want to allow in an Exception + wartremoverExcluded += baseDirectory.value / "src" / "main" / "scala" / "com" / "twitter" / "bijection" / "InversionFailure.scala", + libraryDependencies ++= Seq( "com.novocode" % "junit-interface" % "0.10-M1" % "test", "org.scalatest" %% "scalatest" % "1.9.1" % "test" diff --git a/project/plugins.sbt b/project/plugins.sbt index c80cbe60c..5e0bdfa6a 100644 --- a/project/plugins.sbt +++ b/project/plugins.sbt @@ -10,3 +10,5 @@ addSbtPlugin("com.typesafe" % "sbt-mima-plugin" % "0.1.6") addSbtPlugin("com.typesafe.sbt" % "sbt-osgi" % "0.7.0") addSbtPlugin("com.typesafe.sbt" % "sbt-scalariform" % "1.3.0") + +addSbtPlugin("org.brianmckenna" % "sbt-wartremover" % "0.12") \ No newline at end of file