Skip to content

Commit

Permalink
Add Scala 3 support
Browse files Browse the repository at this point in the history
  • Loading branch information
thesamet committed Jul 19, 2021
1 parent 66415a7 commit 03da6a7
Show file tree
Hide file tree
Showing 13 changed files with 65 additions and 33 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
strategy:
matrix:
os: ["ubuntu-latest"]
scala: [JVM2_12, JVM2_13]
scala: [JVM2_12, JVM2_13, JVM3]
runs-on: ${{ matrix.os }}

steps:
Expand Down
5 changes: 4 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ lazy val e2e = projectMatrix
),
TestProtosGenerator.generateAllTypesProtoSettings,
Compile / PB.protoSources += (Compile / sourceManaged).value / "protobuf",
Compile / PB.protoSources ++= (if (scalaVersion.value.startsWith("2."))
Seq((Compile / sourceDirectory).value / "protobuf-scala2")
else Seq.empty),
Compile / PB.targets := Seq(
genModule("scalapb.validate.compiler.ValidatePreprocessor$") ->
(Compile / sourceManaged).value / "scalapb",
Expand All @@ -140,4 +143,4 @@ lazy val e2e = projectMatrix
(Compile / sourceManaged).value / "scalapb"
)
)
.jvmPlatform(scalaVersions = Seq(Scala212, Scala213))
.jvmPlatform(scalaVersions = Seq(Scala212, Scala213, Scala3))
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,14 @@ class MessagePrinter(
def printObject(fp: FunctionalPrinter): FunctionalPrinter =
fp.add(
s"object ${objectName.name} extends $Validator[${message.scalaType.fullName}] {"
).indented(_.seq(fieldRules.flatMap(_._2.preamble)))
.call(printValidate)
.print(
message.getNestedTypes.asScala
.filterNot(_.getOptions().getExtension(Validate.ignored))
)((fp, fd) => new MessagePrinter(implicits, fd).printObject(fp))
.add("}")
).indented(
_.seq(fieldRules.flatMap(_._2.preamble))
.call(printValidate)
.print(
message.getNestedTypes.asScala
.filterNot(_.getOptions().getExtension(Validate.ignored))
)((fp, fd) => new MessagePrinter(implicits, fd).printObject(fp))
).add("}")

def content: String = {
val fp = new FunctionalPrinter()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,27 @@ import io.envoyproxy.pgv.validate.validate.FieldRules
import io.envoyproxy.pgv.validate.validate.FieldRules.Type
import scalapb.compiler.DescriptorImplicits
import scala.language.reflectiveCalls
import ComparativeRulesGen.NumericRules
import scala.reflect.{classTag, ClassTag}

object IgnoreEmptyRulesGen {
type HasIgnoreEmpty = {
def getIgnoreEmpty: Boolean
}

def numericIsEmpty(hi: HasIgnoreEmpty): Option[Rule] =
Rule.ifSet(hi.getIgnoreEmpty)(ComparativeRulesGen.constRule("0"))
def numericIsEmpty[T: ClassTag](
rules: NumericRules[T] with HasIgnoreEmpty
): Option[Rule] = {
val zero = classTag[T] match {
case ct if ct == classTag[Long] => "0L"
case ct if ct == classTag[Int] => "0"
case ct if ct == classTag[Double] => "0.0"
case ct if ct == classTag[Float] => "0.0f"
case ct =>
throw new RuntimeException(s"Unsupported numeric field type $ct")
}
Rule.ifSet(rules.getIgnoreEmpty)(ComparativeRulesGen.constRule(zero))
}

def ignoreEmptyRule(
fd: FieldDescriptor,
Expand Down Expand Up @@ -74,15 +87,15 @@ object IgnoreEmptyRulesGen {
case Type.Bool(_) =>
None

case Type.Any(anyRules) =>
case Type.Any(_) =>
None

case Type.Map(mapRules) =>
Rule.ifSet(mapRules.getIgnoreEmpty)(
RepeatedRulesGen.maxItems(fd, 0, di)
)

case Type.Enum(enumRules) =>
case Type.Enum(_) =>
None

case _ => None
Expand Down
14 changes: 9 additions & 5 deletions e2e/src/main/scala/e2e/cats/alltypes/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@ import e2e.cats.types.{Color, SubMsg}
import com.google.protobuf.ByteString

package object alltypes {
implicit val subMsg = Ordering.fromLessThan[SubMsg]((x, y) => x.a < y.a)
implicit val subMsg: Ordering[SubMsg] =
Ordering.fromLessThan[SubMsg]((x, y) => x.a < y.a)

implicit val color = Ordering.fromLessThan[Color]((x, y) => x.value < y.value)
implicit val color: Ordering[Color] =
Ordering.fromLessThan[Color]((x, y) => x.value < y.value)

implicit val byteString: Ordering[ByteString] =
Ordering.String.on[ByteString](_.toByteArray.toVector.mkString("-"))

implicit val kernelOrderSubMsg = cats.kernel.Order.fromOrdering(subMsg)
implicit val kernelOrderSubMsg: cats.kernel.Order[SubMsg] =
cats.kernel.Order.fromOrdering(subMsg)

implicit val kernelOrderColor = cats.kernel.Order.fromOrdering(color)
implicit val kernelOrderColor: cats.kernel.Order[Color] =
cats.kernel.Order.fromOrdering(color)

implicit val kernelOrderByteString =
implicit val kernelOrderByteString: cats.kernel.Order[ByteString] =
cats.kernel.Order.fromOrdering(byteString)
}
2 changes: 1 addition & 1 deletion e2e/src/main/scala/scalapb/transforms/MyCustomType.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import scalapb.transforms.field.MyMsg
final case class MyCustomType(a: String)

object MyCustomType {
implicit val tm =
implicit val tm: TypeMapper[MyMsg, MyCustomType] =
TypeMapper[MyMsg, MyCustomType](pb => MyCustomType(pb.a))(custom =>
MyMsg(custom.a)
)
Expand Down
5 changes: 3 additions & 2 deletions e2e/src/main/scala/scalapb/transforms/PositiveInt.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ final case class PositiveInt(n: Int) {
}

object PositiveInt {
implicit val tm = TypeMapper[Int, PositiveInt](PositiveInt(_))(_.n)
implicit val ordering =
implicit val tm: TypeMapper[Int, PositiveInt] =
TypeMapper[Int, PositiveInt](PositiveInt(_))(_.n)
implicit val ordering: Ordering[PositiveInt] =
Ordering.fromLessThan[PositiveInt]((x, y) => x.n < y.n)
}
15 changes: 10 additions & 5 deletions e2e/src/test/scala/scalapb/validate/OptionsSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@ import examplepb.options.NoValidator
class OptionsSpec extends munit.FunSuite {

test("Person empty") {
assertNoDiff(
compileErrors("Validator[NoValidator]"),
"""|error: could not find implicit value for evidence parameter of type scalapb.validate.Validator[examplepb.options.NoValidator]
|Validator[NoValidator]
| ^""".stripMargin
val error =
compileErrors("Validator[NoValidator]")
assert(
error.contains(
"could not find implicit value for evidence parameter of type scalapb.validate.Validator["
) ||
error.contains(
"no implicit argument of type scalapb.validate.Validator"
),
clues(error)
)
}
}
2 changes: 1 addition & 1 deletion e2e/src/test/scala/scalapb/validate/ScalaHarness.scala
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ object ScalaHarness {
val messages = files.flatMap(allMessages)

val typeMap: Map[String, GeneratedMessageCompanion[_ <: GeneratedMessage]] =
messages.map { cmp: GeneratedMessageCompanion[_ <: GeneratedMessage] =>
messages.map { (cmp: GeneratedMessageCompanion[_ <: GeneratedMessage]) =>
(cmp.scalaDescriptor.fullName, cmp)
}.toMap

Expand Down
17 changes: 11 additions & 6 deletions e2e/src/test/scala/scalapb/validate/SkipSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@ import examplepb2.required.{Person => Person2}

class SkipSpec extends munit.FunSuite with ValidationHelpers {
test("third_party validators dont exist") {
assertNoDiff(
val error =
compileErrors(
"implicitly[scalapb.validate.Validator[third_party.third_party.CantChangeThisMessage]]"
),
"""|error: could not find implicit value for parameter e: scalapb.validate.Validator[third_party.third_party.CantChangeThisMessage]
|implicitly[scalapb.validate.Validator[third_party.third_party.CantChangeThisMessage]]
| ^
|""".stripMargin
)

assert(
error.contains(
"could not find implicit value for parameter e: scalapb.validate.Validator[third_party.third_party.CantChangeThisMessage]"
) ||
error.contains(
"no implicit argument of type scalapb.validate.Validator["
),
clues(error)
)
}
}

0 comments on commit 03da6a7

Please sign in to comment.