Skip to content

Commit

Permalink
Handle sealed class when attach name tag
Browse files Browse the repository at this point in the history
  • Loading branch information
nox213 committed Sep 26, 2024
1 parent c4613f1 commit 3d7d325
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,14 @@ object Macros {
fail(tpe, _),
)


val myself: Option[Symbol] = sealedParents.find(_ == tpe.typeSymbol)
val segments =
sealedParents
myself.toList.map(_.fullName.split('.')) ++
sealedParents
.flatMap(_.asClass.knownDirectSubclasses)
.map(_.fullName.split('.'))


// -1 because even if there is only one subclass, and so no name segments
// are needed to differentiate between them, we want to keep at least
// the rightmost name segment
Expand Down
7 changes: 5 additions & 2 deletions upickle/implicits/src-3/upickle/implicits/macros.scala
Original file line number Diff line number Diff line change
Expand Up @@ -309,12 +309,15 @@ def tagNameImpl0[T](transform: String => String)(using Quotes, Type[T]): Expr[St
inline def shortTagName[T]: String = ${ shortTagNameImpl[T] }
def shortTagNameImpl[T](using Quotes, Type[T]): Expr[String] =
import quotes.reflect._
val sym = TypeTree.of[T].symbol
val myself = if (TypeRepr.of[T].baseClasses.contains(TypeRepr.of[T].typeSymbol))
Some(TypeRepr.of[T].typeSymbol.fullName.split('.'))
else None
val segments = TypeRepr.of[T].baseClasses
.filter(_.flags.is(Flags.Sealed))
.flatMap(_.children)
.filter(_.flags.is(Flags.Case))
.map(_.fullName.split('.'))
.map(_.fullName.split('.')) ++
myself.toList

val identicalSegmentCount = Range(0, segments.map(_.length).max - 1)
.takeWhile(i => segments.map(_.lift(i)).distinct.size == 1)
Expand Down
9 changes: 9 additions & 0 deletions upickle/test/src/upickle/MacroTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import upickle.default.{read, write, ReadWriter => RW}

case class Trivial(a: Int = 1)

sealed case class SealedClass(i: Int, s: String)
object SealedClass {
implicit val rw: RW[SealedClass] = upickle.default.macroRW
}

case class KeyedPerson(
@upickle.implicits.key("first_name") firstName: String = "N/A",
@upickle.implicits.key("last_name") lastName: String)
Expand Down Expand Up @@ -872,5 +877,9 @@ object MacroTests extends TestSuite {
)

}

test("sealedClass"){
assert(write(SealedClass(3, "Hello")) == """{"$type":"SealedClass","i":3,"s":"Hello"}""")
}
}
}

0 comments on commit 3d7d325

Please sign in to comment.