From 5b04b89c3db28511fb215f732f7962a0e6ffb723 Mon Sep 17 00:00:00 2001 From: Dale Wijnand <344610+dwijnand@users.noreply.github.com> Date: Mon, 28 Jan 2019 18:39:46 +0000 Subject: [PATCH] Replace scala-collection-compat with ScalaCollectionCompat (#230) * Cleanup imports in Reads.scala * Replace scala-collection-compat with ScalaCollectionCompat --- build.sbt | 14 ++++++---- .../api/libs/json/ScalaCollectionCompat.scala | 10 +++++++ .../api/libs/json/ScalaCollectionCompat.scala | 28 +++++++++++++++++++ .../main/scala/play/api/libs/json/Reads.scala | 12 ++++---- 4 files changed, 52 insertions(+), 12 deletions(-) create mode 100644 play-json/shared/src/main/scala-2.13+/play/api/libs/json/ScalaCollectionCompat.scala create mode 100644 play-json/shared/src/main/scala-2.13-/play/api/libs/json/ScalaCollectionCompat.scala diff --git a/build.sbt b/build.sbt index 4ddc0787c..77e2e8d31 100644 --- a/build.sbt +++ b/build.sbt @@ -124,16 +124,20 @@ lazy val `play-json` = crossProject(JVMPlatform, JSPlatform).crossType(CrossType "org.typelevel" %% "macro-compat" % "1.1.1", "org.scala-lang" % "scala-compiler" % scalaVersion.value % "provided" ), - libraryDependencies ++= - (CrossVersion.partialVersion(scalaVersion.value) match { - case Some((2, 13)) => Seq("org.scala-lang.modules" %%% "scala-collection-compat" % "0.2.0") - case _ => Seq("org.scala-lang.modules" %%% "scala-collection-compat" % "0.1.1") - }), libraryDependencies ++= (CrossVersion.partialVersion(scalaVersion.value) match { case Some((2, 13)) => Seq() case _ => Seq(compilerPlugin("org.scalamacros" % "paradise" % "2.1.0" cross CrossVersion.full)) }), + unmanagedSourceDirectories in Compile += { + //val sourceDir = (sourceDirectory in Compile).value + // ^ gives jvm/src/main, for some reason + val sourceDir = baseDirectory.value / "../shared/src/main" + CrossVersion.partialVersion(scalaVersion.value) match { + case Some((2, n)) if n >= 13 => sourceDir / "scala-2.13+" + case _ => sourceDir / "scala-2.13-" + } + }, sourceGenerators in Compile += Def.task{ val dir = (sourceManaged in Compile).value diff --git a/play-json/shared/src/main/scala-2.13+/play/api/libs/json/ScalaCollectionCompat.scala b/play-json/shared/src/main/scala-2.13+/play/api/libs/json/ScalaCollectionCompat.scala new file mode 100644 index 000000000..ae37c47c8 --- /dev/null +++ b/play-json/shared/src/main/scala-2.13+/play/api/libs/json/ScalaCollectionCompat.scala @@ -0,0 +1,10 @@ +/* + * Copyright (C) 2009-2019 Lightbend Inc. + */ + +package play.api.libs.json + +/** Copied from scala-collection-compat, because its binary API isn't stable. */ +private[json] object ScalaCollectionCompat { + type Factory[-A, +C] = scala.collection.Factory[A, C] +} diff --git a/play-json/shared/src/main/scala-2.13-/play/api/libs/json/ScalaCollectionCompat.scala b/play-json/shared/src/main/scala-2.13-/play/api/libs/json/ScalaCollectionCompat.scala new file mode 100644 index 000000000..e0355e5a4 --- /dev/null +++ b/play-json/shared/src/main/scala-2.13-/play/api/libs/json/ScalaCollectionCompat.scala @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2009-2019 Lightbend Inc. + */ + +package play.api.libs.json + +import scala.language.implicitConversions +import scala.language.higherKinds + +import scala.collection.generic.CanBuildFrom +import scala.collection.mutable + +/** Copied from scala-collection-compat, because its binary API isn't stable. */ +private[json] object ScalaCollectionCompat { + + /** + * A factory that builds a collection of type `C` with elements of type `A`. + * + * Type param `A` is the type of elements (e.g. `Int`, `Boolean`, etc.). + * Type param `C` is the type of collection (e.g. `List[Int]`, `TreeMap[Int, String]`, etc.). + */ + type Factory[-A, +C] = CanBuildFrom[Nothing, A, C] + + final implicit class FactoryOps[-A, +C](private val factory: Factory[A, C]) { + def newBuilder: mutable.Builder[A, C] = factory() + } + +} diff --git a/play-json/shared/src/main/scala/play/api/libs/json/Reads.scala b/play-json/shared/src/main/scala/play/api/libs/json/Reads.scala index b4cf2e1ed..f820ed145 100644 --- a/play-json/shared/src/main/scala/play/api/libs/json/Reads.scala +++ b/play-json/shared/src/main/scala/play/api/libs/json/Reads.scala @@ -4,18 +4,16 @@ package play.api.libs.json -import scala.annotation.implicitNotFound - -import scala.util.control +import scala.language.higherKinds +import scala.annotation.implicitNotFound import scala.collection.Seq -import scala.collection.compat._ import scala.collection.immutable.Map import scala.collection.mutable.Builder +import scala.reflect.ClassTag +import scala.util.control -import scala.language.higherKinds - -import reflect.ClassTag +import ScalaCollectionCompat._ /** * A `Reads` object describes how to decode JSON into a value.