Skip to content

Commit

Permalink
Fix ObjectLens compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
thesamet committed Sep 25, 2023
1 parent 3312ec0 commit 843e31c
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ lazy val compilerPlugin = (projectMatrix in file("compiler-plugin"))
mimaBinaryIssueFilters := Seq(
ProblemFilters.exclude[ReversedMissingMethodProblem]("scalapb.options.*"),
ProblemFilters.exclude[DirectMissingMethodProblem]("scalapb.compiler.GeneratorParams.*"),
ProblemFilters.exclude[DirectMissingMethodProblem]("scalapb.gen.*"),
ProblemFilters.exclude[DirectMissingMethodProblem]("scalapb.gen.*")
),
PB.protocVersion := protobufCompilerVersion,
Compile / PB.targets := Seq(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -944,7 +944,7 @@ class ProtobufGenerator(

printer
.add(
s"implicit class ${className}Lens[UpperPB](_l: _root_.scalapb.lenses.Lens[UpperPB, ${message.scalaType.fullName}]) extends _root_.scalapb.lenses.ObjectLens[UpperPB, ${message.scalaType.fullName}](_l) {"
s"implicit class ${className}Lens[UpperPB](_l: _root_.scalapb.lenses.Lens[UpperPB, ${message.scalaType.fullName}]) extends ${C.MessageLens}[UpperPB, ${message.scalaType.fullName}](_l) {"
)
.indent
.print(message.fields) { case (printer, field) =>
Expand Down Expand Up @@ -1470,9 +1470,6 @@ class ProtobufGenerator(
.when(javaConverterImport)(
_.add(s"import _root_.scalapb.internal.compat.JavaConverters.${C.WildCardImport}").add()
)
.when(params.lenses && params.scala3Sources)(
_.add("import scala.language.adhocExtensions").add()
)
.print(file.scalaOptions.getImportList.asScala) { case (printer, i) =>
printer.add(s"import $i")
}
Expand Down Expand Up @@ -1730,6 +1727,10 @@ private class Constants(params: GeneratorParams) extends ConstantsBase {
if (params.scala3Sources) "private" else "private[this]"

val WithOperator: String = if (params.scala3Sources) " & " else " with "

val MessageLens =
if (params.scala3Sources) "_root_.scalapb.lenses.MessageLens"
else "_root_.scalapb.lenses.ObjectLens"
}

object ProtobufGenerator {
Expand Down
6 changes: 5 additions & 1 deletion lenses/src/main/scala/scalapb/lenses/Lenses.scala
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ object Lens extends CompatLensImplicits {
}
}

/** Represents a lens that has sub-lenses. */
class ObjectLens[U, Container](self: Lens[U, Container]) extends Lens[U, Container] {

/** Creates a sub-lens */
Expand All @@ -129,6 +128,11 @@ class ObjectLens[U, Container](self: Lens[U, Container]) extends Lens[U, Contain
u => set(ms.foldLeft[Container](get(u))((p, m) => m(Lens.unit[Container])(p)))(u)
}

// In Scala 3 source mode, message lenses extend this class to avoid an error of extending
// non-open class.
// TODO(thesamet): unify ObjectLens and MessageLens in next major release (#1581)
abstract class MessageLens[U, Container](self: Lens[U, Container]) extends ObjectLens(self)

trait Updatable[A] extends Any {
self: A =>
def update(ms: (Lens[A, A] => Mutation[A])*): A =
Expand Down

0 comments on commit 843e31c

Please sign in to comment.