-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Configure shared and test modules to cross-build scala 3 #676
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b952c5a
Update build setup
RustedBones 0722c82
setup scala3 on shared + test
RustedBones a2ba157
FIx build update
RustedBones 7da41bf
Fix enum macros
RustedBones 3874c6d
Update CI script
RustedBones f4df19e
Fix CI cross building
RustedBones a9184f8
Add back runtime check for scala 3
RustedBones d92f83b
Restrict false EnumType generation in scala 3
RustedBones 5b2f2db
Update sbt-scoverage to 2.0.9
RustedBones cbef5c2
Add comment
RustedBones File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ | ||
/* | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. all code in |
||
* 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What exactly
targets.tar
will contain?