Skip to content

Commit

Permalink
support new scala 3 syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
ngbinh authored and thesamet committed Sep 26, 2023
1 parent 24b864c commit 38d623b
Show file tree
Hide file tree
Showing 47 changed files with 351 additions and 147 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ jobs:
strategy:
matrix:
scala: [2_12, 2_13, 3]
script: [mima, e2e, proptest]
script: [mima, e2e, test]
include:
- scala: 2_12
script: examples_and_formatting
- scala: 2_13
script: scala3_compat_test
- scala: 3
script: scala3_compat_test

steps:
- uses: actions/checkout@v3
Expand Down
6 changes: 6 additions & 0 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@ project.excludeFilters = [
"scalapb-runtime/.*/scala.*/com/google/protobuf/.*",
"scalapb-runtime/.*/scala.*/scalapb/options/.*"
]

fileOverride {
"glob:**/src/main/scala-3/**" {
runner.dialect = scala3
}
}
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
## [0.11.14]
- Repeated extensions can now use arbitrary iterable types or any collection type there is a
CollectionAdapter for (#1509)
- Introduced new generator parameter scala3_sources (also available via
scalapb.proto) to generate sources that are compatible with Scala 3 (#1576)

## [0.11.13]
- Added input and output message type for method descriptor (#1503)
Expand Down
65 changes: 56 additions & 9 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,12 @@ lazy val compilerPlugin = (projectMatrix in file("compiler-plugin"))
),
mimaPreviousArtifacts := Set("com.thesamet.scalapb" %% "compilerplugin" % MimaPreviousVersion),
mimaBinaryIssueFilters := Seq(
ProblemFilters.exclude[ReversedMissingMethodProblem]("scalapb.options.*")
ProblemFilters.exclude[ReversedMissingMethodProblem]("scalapb.options.*"),
ProblemFilters.exclude[DirectMissingMethodProblem]("scalapb.compiler.GeneratorParams.*"),
ProblemFilters.exclude[DirectMissingMethodProblem]("scalapb.gen.*"),
ProblemFilters.exclude[DirectMissingMethodProblem](
"scalapb.compiler.ProtobufGenerator.generateMessageCompanionMatcher"
)
),
PB.protocVersion := protobufCompilerVersion,
Compile / PB.targets := Seq(
Expand Down Expand Up @@ -218,10 +223,24 @@ lazy val protocGenScalaNativeImage =
Compile / mainClass := Some("scalapb.ScalaPbCodeGenerator")
)

lazy val ScalaSources = GeneratorAxis("", "scalagen")
lazy val Scala3Sources = GeneratorAxis("Scala3Sources", "scala3sources")

lazy val proptest = (projectMatrix in file("proptest"))
.defaultAxes()
.dependsOn(compilerPlugin % "compile->compile;test->test", runtime, grpcRuntime)
.jvmPlatform(scalaVersions = Seq(Scala212, Scala213, Scala3))
.defaultAxes()
.customRow(
scalaVersions = Seq(Scala212, Scala213, Scala3),
axisValues = Seq(ScalaSources, VirtualAxis.jvm),
settings = Seq()
)
.customRow(
scalaVersions = Seq(Scala213, Scala3),
axisValues = Seq(Scala3Sources, VirtualAxis.jvm),
settings = Seq(
Test / javaOptions += "-Dscala3_sources"
)
)
.settings(commonSettings)
.settings(
publishArtifact := false,
Expand Down Expand Up @@ -370,13 +389,41 @@ lazy val e2e = (projectMatrix in file("e2e"))
)
"scalajvm-3"
else
"scalajvm-2")
"scalajvm-2"),
Compile / PB.targets := Seq(
genModule("scalapb.ScalaPbCodeGenerator$") -> (Compile / sourceManaged).value
)
)
)
.customRow(
scalaVersions = Seq(Scala213, Scala3),
axisValues = Seq(Scala3Sources, VirtualAxis.jvm),
settings = Seq(
Test / unmanagedSourceDirectories += (Test / scalaSource).value.getParentFile / (if (
isScala3.value
)
"scalajvm-3"
else
"scalajvm-2"),
scalacOptions ++=
(if (isScala3.value) Seq("-source", "future") else Seq("-Xsource:3")),
Test / scalacOptions --=
(if (isScala3.value) Seq("-source", "future") else Seq("-Xsource:3")),
Compile / PB.targets := Seq(
(
genModule("scalapb.ScalaPbCodeGenerator$"),
Seq("scala3_sources")
) -> (Compile / sourceManaged).value
)
)
)
.jsPlatform(
Seq(Scala212, Scala213, Scala3),
settings = Seq(
Compile / PB.includePaths += (ThisBuild / baseDirectory).value / "protobuf"
Compile / PB.includePaths += (ThisBuild / baseDirectory).value / "protobuf",
Compile / PB.targets := Seq(
genModule("scalapb.ScalaPbCodeGenerator$") -> (Compile / sourceManaged).value
)
)
)
.settings(e2eCommonSettings)
Expand All @@ -389,10 +436,10 @@ lazy val e2e = (projectMatrix in file("e2e"))
)
else Nil),
PB.protocVersion := versions.protobuf,
Compile / PB.protocOptions += "--experimental_allow_proto3_optional",
Compile / PB.targets := Seq(
genModule("scalapb.ScalaPbCodeGenerator$") -> (Compile / sourceManaged).value
)
Compile / PB.protoSources ++= (if (isScala3.value)
Seq(sourceDirectory.value / "main" / "protobuf-scala3")
else Nil),
Compile / PB.protocOptions += "--experimental_allow_proto3_optional"
)

lazy val conformance = (projectMatrix in file("conformance"))
Expand Down
4 changes: 4 additions & 0 deletions compiler-plugin/src/main/scala/scalapb/GeneratorOption.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,8 @@ object GeneratorOption {
case object RetainSourceCodeInfo extends GeneratorOption {
override def toString = "retain_source_code_info"
}

case object Scala3Sources extends GeneratorOption {
override def toString = "scala3_sources"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,8 @@ class DescriptorImplicits private[compiler] (
.map(Helper.escapeComment)
.filter(_.nonEmpty)
}

def V: ScalaCompatConstants = message.getFile.V
}

implicit class ExtendedEnumDescriptor(val enumDescriptor: EnumDescriptor) {
Expand Down Expand Up @@ -929,6 +931,10 @@ class DescriptorImplicits private[compiler] (
else file.getPackage
}

def emitScala3Sources: Boolean = scalaOptions.getScala3Sources || params.scala3Sources

def V: ScalaCompatConstants = new ScalaCompatConstants(emitScala3Sources)

def javaPackageAsSymbol: String =
javaPackage.split('.').map(_.asSymbol).mkString(".")

Expand Down Expand Up @@ -1185,6 +1191,26 @@ object DescriptorImplicits {
}
}

// For constants that depends on the Scala 2 or 3 mode.
private[scalapb] class ScalaCompatConstants(emitScala3Sources: Boolean) {
val WildcardType: String =
if (emitScala3Sources) "?" else "_"

val WildcardImport: String = if (emitScala3Sources) "*" else "_"

val CompSeqType: String =
s"Seq[_root_.scalapb.GeneratedMessageCompanion[$WildcardType <: _root_.scalapb.GeneratedMessage]]"

val PrivateThis: String =
if (emitScala3Sources) "private" else "private[this]"

val WithOperator: String = if (emitScala3Sources) " & " else " with "

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

object Helper {
def makeUniqueNames[T](values: Seq[(T, String)]): Map[T, String] = {
val newNameMap: Map[String, T] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ case class GeneratorParams(
singleLineToProtoString: Boolean = false,
asciiFormatToString: Boolean = false,
lenses: Boolean = true,
retainSourceCodeInfo: Boolean = false
retainSourceCodeInfo: Boolean = false,
scala3Sources: Boolean = false
)

object GeneratorParams {
Expand Down Expand Up @@ -49,6 +50,8 @@ object GeneratorParams {
case "no_lenses" => Right((params.copy(lenses = false), unrecognized))
case "retain_source_code_info" =>
Right((params.copy(retainSourceCodeInfo = true), unrecognized))
case "scala3_sources" =>
Right((params.copy(scala3Sources = true), unrecognized))
case p => Right((params, unrecognized :+ p))
}
case (l, _) => l
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ final class GrpcServicePrinter(service: ServiceDescriptor, implicits: Descriptor
p.call(generateScalaDoc(service))
.add(s"trait ${service.name} extends _root_.scalapb.grpc.AbstractService {")
.indent
.add(s"override def serviceCompanion = ${service.name}")
.add(
s"override def serviceCompanion: _root_.scalapb.grpc.ServiceCompanion[${service.name}] = ${service.name}"
)
.print(service.methods) { case (p, method) =>
p.call(generateScalaDoc(method)).add(serviceMethodSignature(method, overrideSig = false))
}
Expand Down Expand Up @@ -69,7 +71,9 @@ final class GrpcServicePrinter(service: ServiceDescriptor, implicits: Descriptor
p.call(generateScalaDoc(service))
.add(s"trait ${service.blockingClient} {")
.indent
.add(s"def serviceCompanion = ${service.name}")
.add(
s"def serviceCompanion: _root_.scalapb.grpc.ServiceCompanion[${service.name}] = ${service.name}"
)
.print(service.methods.filter(_.canBeBlocking)) { case (p, method) =>
p.call(generateScalaDoc(method)).add(blockingMethodSignature(method, overrideSig = false))
}
Expand Down
Loading

0 comments on commit 38d623b

Please sign in to comment.