Skip to content

Commit

Permalink
[fix] extract annotations with type parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
pgrandjean committed Jul 31, 2022
1 parent 654fe58 commit 44deea5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
2 changes: 1 addition & 1 deletion core/src/main/scala/shapeless/annotation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ class AnnotationMacros(val c: whitebox.Context) extends CaseClassMacros {
val tpe = weakTypeOf[T]

val annTreeOpts = getAnnotationTreeOptions(tpe, typeAnnotation).map { list =>
list.find(_._1 =:= annTpe).map(_._2)
list.find(_._1.erasure =:= annTpe.erasure).map(_._2)
}

val wrapTpeTrees = annTreeOpts.map {
Expand Down
21 changes: 12 additions & 9 deletions core/src/test/scala/shapeless/annotation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ object AnnotationTestsDefinitions {
case class First() extends saAnnotation
case class Second(i: Int, s: String) extends saAnnotation
case class Third(c: Char) extends saAnnotation
case class Fourth[T](t: T) extends saAnnotation

case class Other() extends saAnnotation
case class Last(b: Boolean) extends saAnnotation
Expand Down Expand Up @@ -58,15 +59,17 @@ object AnnotationTestsDefinitions {
case class CC3(
@First i: Int,
s: String,
@Second(2, "b") @Third('c') ob: Option[Boolean]
@Second(2, "b") @Third('c') ob: Option[Boolean],
@Fourth(4) c: Char
)

case class CC4(
i: Int @First,
s: String,
ob: Option[Boolean] @Second(2, "b") @Third('c')
ob: Option[Boolean] @Second(2, "b") @Third('c'),
c: Char @Fourth(4)
)

type PosInt = Int @First
type Email = String @Third('c')
case class User(age: PosInt, email: Email)
Expand Down Expand Up @@ -193,14 +196,14 @@ class AnnotationTests {
def invalidTypeAnnotations: Unit = {
illTyped(" TypeAnnotations[Dummy, CC2] ", "could not find implicit value for parameter annotations: .*")
illTyped(" TypeAnnotations[Dummy, Base] ", "could not find implicit value for parameter annotations: .*")
illTyped(" TypeAnnotations[Second, Dummy] ", "could not find implicit value for parameter annotations: .*")
illTyped(" TypeAnnotations[Second, Dummy] ", "could not find implicit value for parameter annotations: .*")
}

@Test
def allAnnotations: Unit = {
val cc = AllAnnotations[CC3].apply()
typed[(First :: HNil) :: HNil :: (Second :: Third :: HNil) :: HNil](cc)
assert(cc == (First() :: HNil) :: HNil :: (Second(2, "b") :: Third('c') :: HNil) :: HNil)
typed[(First :: HNil) :: HNil :: (Second :: Third :: HNil) :: (Fourth[_] :: HNil) :: HNil](cc)
assert(cc == (First() :: HNil) :: HNil :: (Second(2, "b") :: Third('c') :: HNil) :: (Fourth(4) :: HNil) :: HNil)

val st = AllAnnotations[Base].apply()
typed[(First :: HNil) :: (Second :: Third :: HNil) :: HNil](st)
Expand All @@ -212,9 +215,9 @@ class AnnotationTests {
typed[(First :: HNil) :: (Second :: Third :: HNil) :: HNil](st)

val cc = AllTypeAnnotations[CC4].apply() // case class
typed[(First :: HNil) :: HNil :: (Second :: Third :: HNil) :: HNil](cc)
assert(cc == (First() :: HNil) :: HNil :: (Second(2, "b") :: Third('c') :: HNil) :: HNil)
typed[(First :: HNil) :: HNil :: (Second :: Third :: HNil) :: (Fourth[_] :: HNil) :: HNil](cc)
assert(cc == (First() :: HNil) :: HNil :: (Second(2, "b") :: Third('c') :: HNil) :: (Fourth(4) :: HNil) :: HNil)

val user = AllTypeAnnotations[User].apply() // type refs
typed[(First :: HNil) :: (Third :: HNil) :: HNil](user)
assert(user == (First() :: HNil) :: (Third('c') :: HNil) :: HNil)
Expand Down

0 comments on commit 44deea5

Please sign in to comment.