Skip to content

Commit 3020549

Browse files
authored
Merge pull request #18 from changvvb/scala3
Support scala3
2 parents e7f08aa + 7595d4b commit 3020549

29 files changed

+904
-68
lines changed

.github/workflows/scala.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ jobs:
1515
- uses: actions/checkout@v2
1616
- name: Setup protoc
1717
uses: arduino/setup-protoc@v1
18+
with:
19+
version: '3.17.3'
1820
- name: Set up JDK 1.8
1921
uses: actions/setup-java@v1
2022
with:

.scalafmt.conf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ maxColumn = 160
22
includeCurlyBraceInSelectChains = false
33
project.git = true
44
project.excludeFilters = ["target/"]
5-
version = 2.6.4
5+
version = 3.2.1
6+
runner.dialect = scala3

build.sbt

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,62 @@
11
import sbt._
22

33
lazy val scala213 = "2.13.6"
4-
lazy val scala212 = "2.12.12"
4+
lazy val scala212 = "2.12.14"
5+
lazy val scala3 = "3.1.0"
56

6-
lazy val supportedScalaVersions = List(scala212, scala213)
7+
lazy val supportedScalaVersions = List(scala212, scala213, scala3)
78

89
val commonSettings = Seq(
9-
scalaVersion := scala213,
10-
scalacOptions += "-language:experimental.macros",
11-
organization := "com.github.changvvb",
12-
crossScalaVersions := supportedScalaVersions,
13-
releasePublishArtifactsAction := PgpKeys.publishSigned.value
10+
scalaVersion := scala3,
11+
scalacOptions += "-language:experimental.macros",
12+
organization := "com.github.changvvb",
13+
crossScalaVersions := supportedScalaVersions,
14+
releasePublishArtifactsAction := PgpKeys.publishSigned.value
1415
)
1516

16-
lazy val `scala-protobuf-java-macro` = project.in(file("pbconverts-macro"))
17-
.settings(libraryDependencies ++= Seq("org.scala-lang" % "scala-reflect" % scalaVersion.value))
18-
.settings(commonSettings)
19-
.enablePlugins(ProtobufPlugin)
2017

21-
lazy val `scala-protobuf-java` = project.in(file("pbconverts"))
22-
.settings(libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.0" % "test")
18+
lazy val `scala-protobuf-java` = project
19+
.in(file("pbconverts"))
20+
.settings(libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.9" % "test")
21+
.settings(libraryDependencies ++= {
22+
CrossVersion.partialVersion(scalaVersion.value) match {
23+
case Some((2, _)) => Seq("org.scala-lang" % "scala-reflect" % scalaVersion.value)
24+
case _ => Nil
25+
}
26+
})
2327
.settings(commonSettings)
24-
.dependsOn(`scala-protobuf-java-macro`)
2528
.enablePlugins(ProtobufTestPlugin)
2629

27-
lazy val root = project.in(file(".")).withId("root")
28-
.aggregate(`scala-protobuf-java`, `scala-protobuf-java-macro`)
30+
lazy val root = project
31+
.in(file("."))
32+
.withId("root")
33+
.aggregate(`scala-protobuf-java`)
2934
.settings(publishArtifact := false)
3035

31-
scalafmtOnCompile in ThisBuild := true
32-
33-
// publish
36+
ThisBuild / scalafmtOnCompile := true
3437

35-
releasePublishArtifactsAction in ThisBuild := releaseStepCommandAndRemaining("+publishSigned")
38+
ThisBuild / releasePublishArtifactsAction := releaseStepCommandAndRemaining("+publishSigned")
3639

37-
publishTo in ThisBuild := {
38-
val nexus = "https://oss.sonatype.org/"
39-
if (version.value.trim.endsWith("SNAPSHOT"))
40-
Some("snapshots" at nexus + "content/repositories/snapshots")
41-
else
42-
Some("releases" at nexus + "service/local/staging/deploy/maven2")
40+
ThisBuild / publishTo := {
41+
val nexus = "https://oss.sonatype.org/"
42+
if (version.value.trim.endsWith("SNAPSHOT"))
43+
Some("snapshots" at nexus + "content/repositories/snapshots")
44+
else
45+
Some("releases" at nexus + "service/local/staging/deploy/maven2")
4346
}
4447

45-
publishMavenStyle in ThisBuild := true
48+
ThisBuild / publishMavenStyle := true
4649

47-
credentials in ThisBuild += Credentials(Path.userHome / ".ivy2" / ".credentials_sonatype")
50+
ThisBuild / credentials += Credentials(Path.userHome / ".ivy2" / ".credentials_sonatype")
4851

49-
publishArtifact in Test := false
52+
Test / publishArtifact := false
5053

51-
pomIncludeRepository in ThisBuild := { _ => false }
54+
ThisBuild / pomIncludeRepository := { _ => false }
5255

53-
homepage in ThisBuild := Some(url("https://github.com/changvvb/scala-protobuf-java"))
56+
ThisBuild / homepage := Some(url("https://github.com/changvvb/scala-protobuf-java"))
5457

55-
pomExtra in ThisBuild := {
56-
<licenses>
58+
ThisBuild / pomExtra := {
59+
<licenses>
5760
<license>
5861
<name>The Apache Software License, Version 2.0</name>
5962
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
@@ -74,5 +77,5 @@ pomExtra in ThisBuild := {
7477
</developers>
7578
}
7679

77-
publishConfiguration in ThisBuild := publishConfiguration.value.withOverwrite(true)
78-
publishLocalConfiguration in ThisBuild := publishLocalConfiguration.value.withOverwrite(true)
80+
ThisBuild / publishConfiguration := publishConfiguration.value.withOverwrite(true)
81+
ThisBuild / publishLocalConfiguration := publishLocalConfiguration.value.withOverwrite(true)
File renamed without changes.
File renamed without changes.

pbconverts-macro/src/main/scala/pbconverts/ProtoScalableMacro.scala renamed to pbconverts/src/main/scala-2/pbconverts/ProtoScalableMacro.scala

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -319,19 +319,18 @@ class ProtoScalableMacro(val c: whitebox.Context) {
319319
val caseClassType = resolveType[T]
320320
val protoType = resolveType[M]
321321
val customTrees = MacroCache.builderFunctionTrees.getOrElse(getBuilderId(), mutable.Map.empty)
322-
val (fixedCustomTrees, preTrees) = customTrees.map {
323-
case (key, tree)
324-
tree match { // setField
325-
case buildFunction: Function
326-
val functionName = TermName("builderFunction$" + MacroCache.getIdentityId)
327-
val fromType = buildFunction.tpe.typeArgs.last // buildFunction 的返回值
328-
val buildExpr = new ToProtoProcessor(q"$functionName($entityIdent)", fromType, caseClassType, protoType, key).tree.get
329-
(key -> buildExpr) -> q"val $functionName = $buildFunction"
330-
case value: Tree // setFieldValue
331-
val identity = TermName("identity$" + MacroCache.getIdentityId)
332-
val buildExpr = new ToProtoProcessor(q"$identity", value.tpe, caseClassType, protoType, key).tree.get
333-
(key -> buildExpr) -> q"val $identity = $value"
334-
}
322+
val (fixedCustomTrees, preTrees) = customTrees.map { case (key, tree)
323+
tree match { // setField
324+
case buildFunction: Function
325+
val functionName = TermName("builderFunction$" + MacroCache.getIdentityId)
326+
val fromType = buildFunction.tpe.typeArgs.last // buildFunction 的返回值
327+
val buildExpr = new ToProtoProcessor(q"$functionName($entityIdent)", fromType, caseClassType, protoType, key).tree.get
328+
(key -> buildExpr) -> q"val $functionName = $buildFunction"
329+
case value: Tree // setFieldValue
330+
val identity = TermName("identity$" + MacroCache.getIdentityId)
331+
val buildExpr = new ToProtoProcessor(q"$identity", value.tpe, caseClassType, protoType, key).tree.get
332+
(key -> buildExpr) -> q"val $identity = $value"
333+
}
335334
}.unzip
336335
val protoableFieldConvertTrees = (defaultProtoableFieldConvertTrees(caseClassType, protoType) ++ fixedCustomTrees.toMap).values
337336
protosImpl(caseClassType, protoType, protoableFieldConvertTrees, preTrees)
@@ -344,19 +343,18 @@ class ProtoScalableMacro(val c: whitebox.Context) {
344343
val customTrees = MacroCache.builderFunctionTrees.getOrElse(builderId, mutable.Map.empty)
345344
val entityType = resolveType[T]
346345

347-
val (fixedCustomTrees, preTrees) = customTrees.map {
348-
case (key, tree)
349-
val selector = entityType.member(TermName(key))
350-
tree match {
351-
case buildFunction: Function // setField
352-
val functionName = TermName("builderFunction$" + MacroCache.getIdentityId)
353-
val expr = new ToScalaProcessor(selector.asMethod, q"$functionName($protoIdent)", buildFunction.body.tpe, resolveType[T], resolveType[M]).tree.get
354-
(key -> expr) -> q"val $functionName = $buildFunction"
355-
case value: Tree // setFieldValue
356-
val identity = TermName("identity$" + MacroCache.getIdentityId)
357-
val expr = new ToScalaProcessor(selector.asMethod, q"$identity", value.tpe, resolveType[T], resolveType[M]).tree.get
358-
(key -> expr) -> q"val $identity = $value"
359-
}
346+
val (fixedCustomTrees, preTrees) = customTrees.map { case (key, tree)
347+
val selector = entityType.member(TermName(key))
348+
tree match {
349+
case buildFunction: Function // setField
350+
val functionName = TermName("builderFunction$" + MacroCache.getIdentityId)
351+
val expr = new ToScalaProcessor(selector.asMethod, q"$functionName($protoIdent)", buildFunction.body.tpe, resolveType[T], resolveType[M]).tree.get
352+
(key -> expr) -> q"val $functionName = $buildFunction"
353+
case value: Tree // setFieldValue
354+
val identity = TermName("identity$" + MacroCache.getIdentityId)
355+
val expr = new ToScalaProcessor(selector.asMethod, q"$identity", value.tpe, resolveType[T], resolveType[M]).tree.get
356+
(key -> expr) -> q"val $identity = $value"
357+
}
360358
}.unzip
361359

362360
val scalableFieldConvertTrees = (defalutScalableFieldConvertTrees(caseClassType, protoType) ++ fixedCustomTrees.toMap).values
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)