-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Configure shared and test modules to cross-build scala 3 (#676)
* Update build setup * setup scala3 on shared + test * FIx build update * Fix enum macros * Update CI script * Fix CI cross building * Add back runtime check for scala 3 * Restrict false EnumType generation in scala 3 * Update sbt-scoverage to 2.0.9 * Add comment
- Loading branch information
1 parent
ca4c0d6
commit e3eec4a
Showing
15 changed files
with
535 additions
and
174 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
addSbtPlugin("org.typelevel" % "sbt-typelevel" % "0.6.0") | ||
addSbtPlugin("com.thesamet" % "sbt-protoc" % "1.0.6") | ||
addSbtPlugin("org.scoverage" % "sbt-scoverage" % "2.0.8") | ||
addSbtPlugin("org.scoverage" % "sbt-scoverage" % "2.0.9") | ||
addSbtPlugin("pl.project13.scala" % "sbt-jmh" % "0.4.4") |
49 changes: 49 additions & 0 deletions
49
shared/src/main/scala-2/magnolify/shared/AnnotationTypeMacros.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright 2023 Spotify AB | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package magnolify.shared | ||
|
||
import scala.reflect.macros.whitebox | ||
|
||
object AnnotationTypeMacros { | ||
def annotationTypeMacro[T: c.WeakTypeTag](c: whitebox.Context): c.Tree = { | ||
import c.universe._ | ||
val wtt = weakTypeTag[T] | ||
val pre = wtt.tpe.asInstanceOf[TypeRef].pre | ||
|
||
// Scala 2.12 & 2.13 macros seem to handle annotations differently | ||
// Scala annotation works in both but Java annotations only works in 2.13 | ||
val saType = typeOf[scala.annotation.StaticAnnotation] | ||
val jaType = typeOf[java.lang.annotation.Annotation] | ||
// Annotation for Scala enumerations are on the outer object | ||
val annotated = if (pre <:< typeOf[scala.Enumeration]) pre else wtt.tpe | ||
val trees = annotated.typeSymbol.annotations.map(_.tree).collect { | ||
case t @ q"new $n(..$args)" if t.tpe <:< saType && !(t.tpe <:< jaType) => | ||
// FIXME `t.tree` should work but somehow crashes the compiler | ||
q"new $n(..$args)" | ||
} | ||
|
||
// Get Java annotations via reflection | ||
val j = q"classOf[${annotated.typeSymbol.asClass}].getAnnotations.toList" | ||
val annotations = q"_root_.scala.List(..$trees) ++ $j" | ||
|
||
q"_root_.magnolify.shared.AnnotationType[$wtt]($annotations)" | ||
} | ||
} | ||
|
||
trait AnnotationTypeCompanionMacros { | ||
implicit def gen[T]: AnnotationType[T] = macro AnnotationTypeMacros.annotationTypeMacro[T] | ||
} |
64 changes: 64 additions & 0 deletions
64
shared/src/main/scala-2/magnolify/shared/EnumTypeDerivation.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright 2023 Spotify AB | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package magnolify.shared | ||
|
||
import magnolia1.{CaseClass, SealedTrait} | ||
|
||
import scala.annotation.{implicitNotFound, nowarn} | ||
|
||
trait EnumTypeDerivation { | ||
type Typeclass[T] = EnumType[T] | ||
|
||
// EnumType can only be split into objects with fixed name | ||
// Avoid invalid ADT derivation involving products by requiring | ||
// implicit EnumValue type-class in magnolia join | ||
// see https://github.com/softwaremill/magnolia/issues/267 | ||
@implicitNotFound("Cannot derive EnumType.EnumValue. EnumType only works for sum types") | ||
trait EnumValue[T] | ||
|
||
implicit def genEnumValue[T]: EnumValue[T] = macro EnumTypeMacros.genEnumValueMacro[T] | ||
|
||
@nowarn | ||
def join[T: EnumValue](caseClass: CaseClass[Typeclass, T]): Typeclass[T] = { | ||
val n = caseClass.typeName.short | ||
val ns = caseClass.typeName.owner | ||
EnumType.create( | ||
n, | ||
ns, | ||
List(n), | ||
caseClass.annotations.toList, | ||
_ => caseClass.rawConstruct(Nil) | ||
) | ||
} | ||
|
||
def split[T](sealedTrait: SealedTrait[Typeclass, T]): Typeclass[T] = { | ||
val n = sealedTrait.typeName.short | ||
val ns = sealedTrait.typeName.owner | ||
val subs = sealedTrait.subtypes.map(_.typeclass) | ||
val values = subs.flatMap(_.values).sorted.toList | ||
val annotations = (sealedTrait.annotations ++ subs.flatMap(_.annotations)).toList | ||
EnumType.create( | ||
n, | ||
ns, | ||
values, | ||
annotations, | ||
// it is ok to use the inefficient find here because it will be called only once | ||
// and cached inside an instance of EnumType | ||
v => subs.find(_.name == v).get.from(v) | ||
) | ||
} | ||
} |
Oops, something went wrong.