Skip to content

Commit

Permalink
Support Scala 3
Browse files Browse the repository at this point in the history
This is prompted by guardian/mobile-apps-api#3198 - we would like
to be able to upgrade MAPI to Scala 3. As MAPI depends on https://github.com/guardian/cross-platform-navigation,
it's good to have a Scala 3 version of this library.

The only tricky part of upgrading `cross-platform-navigation` to Scala 3 is handling the
explicit call to the `unapply()` method, which _used_ to return an `Option[(...,...,...)]`
with all the parameters used to create the case class - in Scala 3 that's changed:

* https://docs.scala-lang.org/scala3/guides/migration/incompat-other-changes.html#explicit-call-to-unapply
* scala/scala3#2335

The least magical way to cope with the changed `unapply()` method is to manually write out
what the unapply method used to produce, and that's what I've done here. Looking at the
discussion on the Scala issue, there are other workarounds which are Scala 3-only (and so
wouldn't easily work in this project, which cross-compiles with Scala 2):

* scala/scala3#2335 (comment) gives us this, which
  did work for me, but just in Scala 3:
  ```
  def doOldUnapply(ns: NavigationSection) = Some(Tuple.fromProductTyped(ns))
  ```
* scala/scala3#2335 (comment) - this is apparently
  a general version of the method above, but didn't work for me - complained that
  `NavigationSection` is not a `Product`, I think?
  • Loading branch information
rtyley committed Sep 5, 2024
1 parent 4b08abe commit 69562d8
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
4 changes: 2 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import sbtversionpolicy.withsbtrelease.ReleaseVersion

name:="cross-platform-navigation"

ThisBuild / scalaVersion := "2.13.14"
ThisBuild / scalaVersion := "3.3.3"

crossScalaVersions := Seq(scalaVersion.value, "2.12.20")
crossScalaVersions := Seq(scalaVersion.value, "2.13.14", "2.12.20")

resolvers ++= Resolver.sonatypeOssRepos("releases")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ case class NavigationSection(

object NavigationSection {

// def fooUnapply[P <: Product](p: P)(using m: scala.deriving.Mirror.ProductOf[P]): Option[m.MirroredElemTypes] =
// Some(Tuple.fromProductTyped(p))

implicit val navigationSectionReads: Reads[NavigationSection] = (
(__ \ "title").read[String] and
(__ \ "path").read[String] and
Expand All @@ -29,14 +32,14 @@ object NavigationSection {
(__ \ "mobileOverride").writeNullable[String] and
(__ \ "sections").lazyWriteNullable(implicitly[Writes[List[NavigationSection]]]) and
(__ \ "editionOverride").writeNullable[String]
)(unlift(NavigationSection.unapply _))
)(unlift((ns: NavigationSection) => Some((ns.title, ns.path, ns.mobileOverride, ns.sections, ns.editionOverride))))

implicit lazy val disNavigationSectionFormat: Format[NavigationSection] = Format(navigationSectionReads, navigationSectionWrites)

}

object Navigation {
implicit val jf = Json.format[Navigation]
implicit val jf: Format[Navigation] = Json.format[Navigation]
}

case class Navigation(pillars: List[NavigationSection])

0 comments on commit 69562d8

Please sign in to comment.