Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First pass at some wart remover stuff, fixing things it mentioned #206

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made this do the iterator approach, but the real thing wart remover was complaining about was the use of return

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

/**
Expand Down Expand Up @@ -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(_))
}
Original file line number Diff line number Diff line change
Expand Up @@ -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._

/**
Expand Down Expand Up @@ -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(_))
}
12 changes: 11 additions & 1 deletion project/Build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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"),
Expand Down Expand Up @@ -49,6 +52,8 @@ object BijectionBuild extends Build {
Seq()
},

wartremoverErrors ++= Seq(Wart.Serializable, Wart.Any2StringAdd),

OsgiKeys.importPackage <<= scalaVersion { sv => Seq("""scala.*;version="$<range;[==,=+);%s>"""".format(sv)) },

OsgiKeys.importPackage ++= Seq("com.twitter.bijection.*;version=\"[${Bundle-Version}, ${Bundle-Version}]\"", "*"),
Expand Down Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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")