Skip to content

Commit

Permalink
Updates and add proptests on github
Browse files Browse the repository at this point in the history
  • Loading branch information
thesamet committed Sep 21, 2023
1 parent d3540ba commit 892f30a
Show file tree
Hide file tree
Showing 45 changed files with 169 additions and 483 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,16 @@ 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
include:
- scala: 2_12
script: scala3compat_test.sh
include:
- scala: 3
script: scala3compat_test.sh

steps:
- uses: actions/checkout@v3
Expand Down
18 changes: 16 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,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
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,45 +15,16 @@ import protocgen.CodeGenResponse
// Exceptions that are caught and passed upstreams as errors.
case class GeneratorException(message: String) extends Exception(message)

private class Constants(params: GeneratorParams) {

val WildCardType: String = {
if (params.scala3Sources) {
"?"
} else {
"_"
}
}

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

val PrivateThis: String = {
if (params.scala3Sources) {
"private"
} else {
"private[this]"
}
}

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

class ProtobufGenerator(
params: GeneratorParams,
implicits: DescriptorImplicits
) {
import implicits._
import DescriptorImplicits.AsSymbolExtension
import ProtobufGenerator._
private val constants = new Constants(params)

private val C = new Constants(params)

def printEnum(printer: FunctionalPrinter, e: EnumDescriptor): FunctionalPrinter = {
val name = e.scalaType.nameSymbol
printer
Expand Down Expand Up @@ -103,7 +74,7 @@ class ProtobufGenerator(
.seq(e.unrecognizedAnnotationList)
.add(
s"""final case class Unrecognized(unrecognizedValue: _root_.scala.Int) extends $name(unrecognizedValue) with _root_.scalapb.UnrecognizedEnum
|lazy val values = scala.collection.immutable.Seq(${e.getValues.asScala
|lazy val values: scala.collection.immutable.Seq[ValueType] = scala.collection.immutable.Seq(${e.getValues.asScala
.map(_.scalaName.asSymbol)
.mkString(", ")})
|def fromValue(__value: _root_.scala.Int): $name = __value match {""".stripMargin
Expand Down Expand Up @@ -578,9 +549,9 @@ class ProtobufGenerator(
fp.when(!message.isValueClass) {
_.add(
s"""@transient
|${constants.PrivateThis} var __serializedSizeMemoized: _root_.scala.Int = 0""".stripMargin
|${C.PrivateThis} var __serializedSizeMemoized: _root_.scala.Int = 0""".stripMargin
)
}.add(s"${constants.PrivateThis} def __computeSerializedSize(): _root_.scala.Int = {")
}.add(s"${C.PrivateThis} def __computeSerializedSize(): _root_.scala.Int = {")
.indent
.add("var __size = 0")
.print(message.fields)(generateSerializedSizeForField)
Expand Down Expand Up @@ -615,7 +586,7 @@ class ProtobufGenerator(
fp.print(message.fields.filter(_.isPacked)) { case (printer, field) =>
val methodName = s"${field.scalaName}SerializedSize"
printer
.add(s"${constants.PrivateThis} def $methodName = {") // closing brace is in each case
.add(s"${C.PrivateThis} def $methodName = {") // closing brace is in each case
.call({ fp =>
Types.fixedSize(field.getType) match {
case Some(size) =>
Expand Down Expand Up @@ -643,7 +614,9 @@ class ProtobufGenerator(
.add(s"__${methodName}Field")
.outdent
.add("}") // closing brace for the method
.add(s"@transient ${constants.PrivateThis} var __${methodName}Field: _root_.scala.Int = 0")
.add(
s"@transient ${C.PrivateThis} var __${methodName}Field: _root_.scala.Int = 0"
)
}
})
}
Expand Down Expand Up @@ -1087,12 +1060,14 @@ class ProtobufGenerator(
def generateMessageCompanionMatcher(methodName: String, messageNumbers: Seq[(Descriptor, Int)])(
fp: FunctionalPrinter
): FunctionalPrinter = {
val signature = s"def $methodName(__number: _root_.scala.Int): _root_.scalapb.GeneratedMessageCompanion[${constants.WildCardType}] = "
val signature =
s"def $methodName(__number: _root_.scala.Int): _root_.scalapb.GeneratedMessageCompanion[${C.WildCardType}] = "

// Due to https://issues.scala-lang.org/browse/SI-9111 we can't directly return the companion
// object.

val outStr = s"var __out: _root_.scalapb.GeneratedMessageCompanion[${constants.WildCardType}] = null"
val outStr =
s"var __out: _root_.scalapb.GeneratedMessageCompanion[${C.WildCardType}] = null"
if (messageNumbers.nonEmpty)
fp.add(signature + "{")
.indent
Expand Down Expand Up @@ -1124,13 +1099,13 @@ class ProtobufGenerator(
message: Descriptor
)(fp: FunctionalPrinter): FunctionalPrinter = {
val signature =
s"lazy val nestedMessagesCompanions: ${constants.CompSeqType} ="
s"lazy val nestedMessagesCompanions: ${C.CompSeqType} ="
if (message.nestedTypes.isEmpty)
fp.add(signature + " Seq.empty")
else
fp.add(signature)
.indent
.add(constants.CompSeqType + "(")
.add(C.CompSeqType + "(")
.indent
.addWithDelimiter(",")(message.nestedTypes.map(m => m.scalaType.fullNameWithMaybeRoot))
.outdent
Expand All @@ -1140,13 +1115,13 @@ class ProtobufGenerator(

// Finding companion objects for top-level types.
def generateMessagesCompanions(file: FileDescriptor)(fp: FunctionalPrinter): FunctionalPrinter = {
val signature = s"lazy val messagesCompanions: ${constants.CompSeqType} ="
val signature = s"lazy val messagesCompanions: ${C.CompSeqType} ="
if (file.getMessageTypes.isEmpty)
fp.add(signature + " Seq.empty")
else
fp.add(signature)
.indent
.add(constants.CompSeqType + "(")
.add(C.CompSeqType + "(")
.indent
.addWithDelimiter(",")(file.getMessageTypes.asScala.map(_.scalaType.fullName).toSeq)
.outdent
Expand All @@ -1157,7 +1132,8 @@ class ProtobufGenerator(
def generateEnumCompanionForField(
message: Descriptor
)(fp: FunctionalPrinter): FunctionalPrinter = {
val signature = s"def enumCompanionForFieldNumber(__fieldNumber: _root_.scala.Int): _root_.scalapb.GeneratedEnumCompanion[${constants.WildCardType}] = "
val signature =
s"def enumCompanionForFieldNumber(__fieldNumber: _root_.scala.Int): _root_.scalapb.GeneratedEnumCompanion[${C.WildCardType}] = "
if (message.fields.exists(_.isEnum))
fp.add(signature + "{")
.indent
Expand Down Expand Up @@ -1292,9 +1268,9 @@ class ProtobufGenerator(
def generateMessageCompanion(
message: Descriptor
)(printer: FunctionalPrinter): FunctionalPrinter = {
val className = message.scalaType.nameSymbol
val companionType = message.companionBaseClasses.mkString(" with ")
val companionTypeDecl = message.companionBaseClasses.mkString(constants.WithOperator)
val className = message.scalaType.nameSymbol
val companionType = message.companionBaseClasses.mkString(" with ")
val companionTypeDecl = message.companionBaseClasses.mkString(C.WithOperator)
printer
.seq(message.companionAnnotationList)
.add(s"""object $className extends $companionType {
Expand Down Expand Up @@ -1492,7 +1468,7 @@ class ProtobufGenerator(
_.add("package " + file.scalaPackage.fullName).add()
)
.when(javaConverterImport)(
_.add("import _root_.scalapb.internal.compat.JavaConverters._").add()
_.add(s"import _root_.scalapb.internal.compat.JavaConverters.${C.WildCardImport}").add()
)
.when(params.lenses && params.scala3Sources)(
_.add("import scala.language.adhocExtensions").add()
Expand Down Expand Up @@ -1733,12 +1709,29 @@ class ProtobufGenerator(
}
}

private[this] object C {
trait ConstantsBase {
val None = "_root_.scala.None"
val UnknownFieldSet = "_root_.scalapb.UnknownFieldSet"
val UnknownFieldSetEmpty = "_root_.scalapb.UnknownFieldSet.empty"
}

private[scalapb] object C extends ConstantsBase

private class Constants(params: GeneratorParams) extends ConstantsBase {
val WildCardType: String =
if (params.scala3Sources) "?" else "_"

val WildCardImport: String = if (params.scala3Sources) "*" else "_"

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

val PrivateThis: String =
if (params.scala3Sources) "private" else "private[this]"

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

object ProtobufGenerator {
def parseParameters(params: String): Either[String, GeneratorParams] =
GeneratorParams.fromString(params)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ object Person extends scalapb.GeneratedMessageCompanion[scalapb.docs.person.Pers

@SerialVersionUID(0L)
final case class Unrecognized(unrecognizedValue: _root_.scala.Int) extends AddressType(unrecognizedValue) with _root_.scalapb.UnrecognizedEnum
lazy val values = scala.collection.immutable.Seq(HOME, WORK)
lazy val values: scala.collection.immutable.Seq[ValueType] = scala.collection.immutable.Seq(HOME, WORK)
def fromValue(__value: _root_.scala.Int): AddressType = __value match {
case 0 => HOME
case 1 => WORK
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ object Color extends _root_.scalapb.GeneratedEnumCompanion[Color] {

@SerialVersionUID(0L)
final case class Unrecognized(unrecognizedValue: _root_.scala.Int) extends Color(unrecognizedValue) with _root_.scalapb.UnrecognizedEnum
lazy val values = scala.collection.immutable.Seq(UNKNOWN, RED, GREEN, BLUE)
lazy val values: scala.collection.immutable.Seq[ValueType] = scala.collection.immutable.Seq(UNKNOWN, RED, GREEN, BLUE)
def fromValue(__value: _root_.scala.Int): Color = __value match {
case 0 => UNKNOWN
case 1 => RED
Expand Down
Empty file.
11 changes: 0 additions & 11 deletions examples/basicWithScala3/build.sbt

This file was deleted.

1 change: 0 additions & 1 deletion examples/basicWithScala3/project/build.properties

This file was deleted.

5 changes: 0 additions & 5 deletions examples/basicWithScala3/project/plugins.sbt

This file was deleted.

26 changes: 0 additions & 26 deletions examples/basicWithScala3/src/main/protobuf/addressbook.proto

This file was deleted.

Loading

0 comments on commit 892f30a

Please sign in to comment.