Skip to content

Commit

Permalink
Use primary constructor to extract field annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
joroKr21 committed Sep 22, 2024
1 parent cd571ef commit 1f8dc82
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
9 changes: 4 additions & 5 deletions core/shared/src/main/scala/shapeless/annotation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -382,11 +382,10 @@ class AnnotationMacros(val c: whitebox.Context) extends CaseClassMacros {

private def getAnnotationTreeOptions(tpe: Type, typeAnnotation: Boolean): List[List[(Type, Tree)]] = {
if (isProduct(tpe)) {
val constructorSyms = tpe
.member(termNames.CONSTRUCTOR)
.asMethod
.paramLists
.flatten
lazy val constructorSyms = tpe
.typeSymbol.asClass
.primaryConstructor.asMethod
.paramLists.iterator.flatten
.map(sym => nameAsString(sym.name) -> sym)
.toMap

Expand Down
14 changes: 13 additions & 1 deletion core/shared/src/test/scala/shapeless/annotation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@

package shapeless

import scala.annotation.{ Annotation => saAnnotation }
import org.junit.Assert._
import org.junit.Test
import shapeless.test.{illTyped, typed}

import scala.annotation.{Annotation => saAnnotation}

object AnnotationTestsDefinitions {

case class First() extends saAnnotation
Expand Down Expand Up @@ -73,6 +75,10 @@ object AnnotationTestsDefinitions {
type PosInt = Int @First
type Email = String @Third('c')
case class User(age: PosInt, email: Email)

case class WithSecondaryCtor(i: Int) {
def this() = this(0)
}
}

class AnnotationTests {
Expand Down Expand Up @@ -233,4 +239,10 @@ class AnnotationTests {
typed[(First :: HNil) :: (Third :: HNil) :: HNil](user)
assert(user == (First() :: HNil) :: (Third('c') :: HNil) :: HNil)
}

@Test
def annotationsWithSecondaryConstructor(): Unit = {
val annotations = Annotations[First, WithSecondaryCtor].apply()
assertEquals(annotations, None :: HNil)
}
}

0 comments on commit 1f8dc82

Please sign in to comment.