Skip to content

Commit

Permalink
Add implicit owritesMap method and make writes map not implicit (#37)
Browse files Browse the repository at this point in the history
- Add unit test for owritesMap and backwards compatibility
  • Loading branch information
jeffmay authored Apr 16, 2021
1 parent be3a534 commit 256f9e0
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
2 changes: 0 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ name := "play-json-ops-root"
ThisBuild / organization := "com.rallyhealth"
ThisBuild / organizationName := "Rally Health"

ThisBuild / gitVersioningSnapshotLowerBound := "4.0.0"

ThisBuild / bintrayOrganization := Some("rallyhealth")
ThisBuild / bintrayRepository := "maven"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@ private[ops] class JsonImplicits private[ops] extends ImplicitTupleFormats with
}
}

implicit def writesMap[K: WritesKey, V: Writes]: Writes[Map[K, V]] = {
@deprecated("Use owritesMap instead.", "4.2.0")
def writesMap[K: WritesKey, V: Writes]: Writes[Map[K, V]] = owritesMap[K, V]

implicit def owritesMap[K: WritesKey, V: Writes]: OWrites[Map[K, V]] = {
val writesK = WritesKey.of[K]
val stringKeyWriter = Writes.map[V]
Writes[Map[K, V]](values => stringKeyWriter.writes(values.map { case (k, v) => (writesK.write(k), v) }))
OWrites[Map[K, V]](values => stringKeyWriter.writes(values.map { case (k, v) => (writesK.write(k), v) }))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package play.api.libs.json.ops.v4
import org.scalacheck.{Arbitrary, Gen, Shrink}
import org.scalacheck.ops._
import org.scalatest.FreeSpec
import play.api.libs.json.{Format, Json}
import play.api.libs.json.{Format, Json, OWrites, Writes}
import org.scalatest.prop.GeneratorDrivenPropertyChecks._

case class KeyWrapper(key: String)
Expand Down Expand Up @@ -59,4 +59,12 @@ class JsonImplicitsSpec extends FreeSpec {
assertResult(keyWrappedMap)(parsedMap)
}
}

"implicit Writes[Map[Int, String]] should be in scope" in {
implicitly[Writes[Map[Int, String]]]
}

"implicit OWrites[Map[Int, String]] should be in scope" in {
implicitly[OWrites[Map[Int, String]]]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ private[ops] class JsonImplicits private[ops] extends ImplicitTupleFormats with
}
}

implicit def writesMap[K: WritesKey, V: Writes]: Writes[Map[K, V]] = {
def writesMap[K: WritesKey, V: Writes]: Writes[Map[K, V]] = owritesMap[K, V]

implicit def owritesMap[K: WritesKey, V: Writes]: OWrites[Map[K, V]] = {
val writesK = WritesKey.of[K]
val stringKeyWriter = Writes.map[V]
Writes[Map[K, V]](values => stringKeyWriter.writes(values.map { case (k, v) => (writesK.write(k), v) }))
OWrites[Map[K, V]](values => stringKeyWriter.writes(values.map { case (k, v) => (writesK.write(k), v) }))
}
}

0 comments on commit 256f9e0

Please sign in to comment.