Skip to content

Commit 267acc4

Browse files
authored
Fix providing annotations vs inherited annotations (#529)
1 parent 8afd41b commit 267acc4

File tree

3 files changed

+11
-9
lines changed

3 files changed

+11
-9
lines changed

core/src/main/scala/magnolia1/macro.scala

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,11 +240,11 @@ object Macro:
240240
.filterNot(isObjectOrScala)
241241
.collect {
242242
case s if s != tpe.typeSymbol =>
243-
(fromConstructor(s) ++ fromDeclarations(s)).filter { case (_, anns) =>
243+
(fromConstructor(s)).filter { case (_, anns) =>
244244
anns.nonEmpty
245245
}
246246
}
247-
.flatten
247+
.flatten ++ fromDeclarations(tpe.typeSymbol, inherited = true)
248248
}
249249
}
250250

@@ -256,10 +256,11 @@ object Macro:
256256
}
257257

258258
private def fromDeclarations(
259-
from: Symbol
259+
from: Symbol,
260+
inherited: Boolean = false
260261
): List[(String, List[Expr[Any]])] =
261262
from.fieldMembers.collect { case field: Symbol =>
262-
val annotations = field.annotations ::: field.allOverriddenSymbols.flatMap(_.annotations).toList
263+
val annotations = if (!inherited) field.annotations else field.allOverriddenSymbols.flatMap(_.annotations).toList
263264
field.name -> annotations
264265
.filter(filterAnnotation)
265266
.map(_.asExpr.asInstanceOf[Expr[Any]])

examples/src/main/scala/magnolia1/examples/show.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ trait GenericShow[Out] extends AutoDerivation[[X] =>> Show[Out, X]] {
2626
if (param.annotations.isEmpty && param.inheritedAnnotations.isEmpty)
2727
""
2828
else {
29-
(param.annotations ++ param.inheritedAnnotations).distinct
29+
(param.annotations.map(_.toString) ++ param.inheritedAnnotations.map(a => s"[i]$a")).distinct
3030
.mkString("{", ",", "}")
3131
}
3232

test/src/test/scala/magnolia1/tests/AnnotationsTests.scala

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class AnnotationsTests extends munit.FunSuite:
2525
val res = Show.derived[Pet].show(Dog("Alex", 10, likesMeat = true))
2626
assertEquals(
2727
res,
28-
"{MyTypeAnnotation(2),MyTypeAnnotation(1)}Dog{MyTypeAnnotation(2),MyTypeAnnotation(1)}(name{MyAnnotation(1)}=Alex,age{MyAnnotation(2)}=10,likesMeat{MyAnnotation(3)}=true)"
28+
"{MyTypeAnnotation(2),MyTypeAnnotation(1)}Dog{MyTypeAnnotation(2),MyTypeAnnotation(1)}(name{[i]MyAnnotation(1)}=Alex,age{[i]MyAnnotation(2)}=10,likesMeat{MyAnnotation(3)}=true)"
2929
)
3030
}
3131

@@ -35,13 +35,13 @@ class AnnotationsTests extends munit.FunSuite:
3535
.show(Hamster("Alex", 10, likesNuts = true, likesVeggies = true))
3636
assertEquals(
3737
res,
38-
"{MyTypeAnnotation(1)}Hamster{MyTypeAnnotation(1)}(name{MyAnnotation(1)}=Alex,age{MyAnnotation(2)}=10,likesNuts{MyAnnotation(3)}=true,likesVeggies{MyAnnotation(4)}=true)"
38+
"{MyTypeAnnotation(1)}Hamster{MyTypeAnnotation(1)}(name{[i]MyAnnotation(1)}=Alex,age{MyAnnotation(6),[i]MyAnnotation(2)}=10,likesNuts{[i]MyAnnotation(3)}=true,likesVeggies{MyAnnotation(4)}=true)"
3939
)
4040
}
4141

4242
test("inherit annotations from base class constructor parameters") {
4343
val res = Show.derived[Foo].show(Foo("foo"))
44-
assertEquals(res, "Foo(foo{MyAnnotation(2),MyAnnotation(1)}=foo)")
44+
assertEquals(res, "Foo(foo{MyAnnotation(2),[i]MyAnnotation(1)}=foo)")
4545
}
4646

4747
test(
@@ -50,7 +50,7 @@ class AnnotationsTests extends munit.FunSuite:
5050
val res = Show.derived[Bar].show(Bar("foo", "bar"))
5151
assertEquals(
5252
res,
53-
"Bar(foo{MyAnnotation(2),MyAnnotation(1)}=foo,bar{MyAnnotation(2),MyAnnotation(1)}=bar)"
53+
"Bar(foo{MyAnnotation(2),[i]MyAnnotation(1)}=foo,bar{MyAnnotation(2),[i]MyAnnotation(1)}=bar)"
5454
)
5555
}
5656

@@ -156,6 +156,7 @@ object AnnotationsTests:
156156

157157
case class Hamster(
158158
name: String,
159+
@MyAnnotation(6)
159160
age: Int,
160161
likesNuts: Boolean,
161162
@MyAnnotation(4) likesVeggies: Boolean

0 commit comments

Comments
 (0)