Skip to content

Commit

Permalink
Scala 3 Case Object fix (#376)
Browse files Browse the repository at this point in the history
Fixes #375
  • Loading branch information
BoopBoopBeepBoop authored Dec 26, 2021
1 parent dd814df commit 9655b81
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
11 changes: 7 additions & 4 deletions implicits/src-3/upickle/implicits/macros.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ package upickle.implicits.macros
import scala.quoted.{ given, _ }
import deriving._, compiletime._

inline def getDefaultParams[T]: Map[String, AnyRef] = ${ getDefaultParmasImpl[T] }
def getDefaultParmasImpl[T](using Quotes, Type[T]): Expr[Map[String, AnyRef]] =
inline def getDefaultParams[T]: Map[String, AnyRef] = ${ getDefaultParamsImpl[T] }
def getDefaultParamsImpl[T](using Quotes, Type[T]): Expr[Map[String, AnyRef]] =
import quotes.reflect._
val sym = TypeTree.of[T].symbol

if (sym.isClassDef) {
val comp = if (sym.isClassDef) sym.companionClass else sym
val comp =
if (sym.isClassDef && !sym.companionClass.isNoSymbol ) sym.companionClass
else sym

val hasDefaults =
for p <- sym.caseFields
yield p.flags.is(Flags.HasDefault)
Expand All @@ -29,7 +32,7 @@ def getDefaultParmasImpl[T](using Quotes, Type[T]): Expr[Map[String, AnyRef]] =
} else {
'{ Map.empty }
}
end getDefaultParmasImpl
end getDefaultParamsImpl

inline def summonList[T <: Tuple]: List[_] =
inline erasedValue[T] match
Expand Down
25 changes: 23 additions & 2 deletions upickle/test/src-3/upickle/Derivation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ case class Cat(name: String, owner: Person)
case class Dog(name: String, age: Int)
derives ReadWriter

case object Cthulu
extends Animal
derives ReadWriter

object DerivationTests extends TestSuite {

val tests = Tests {
Expand Down Expand Up @@ -63,7 +67,24 @@ object DerivationTests extends TestSuite {
val dog = Dog("Ball", 10)
val result = write(dog)
val expected = """{"name":"Ball","age":10}"""
println(result == expected)
assert(result == expected)
}

test("caseObjectWriter") - {
val result1 = write(Cthulu)

val animal: Animal = Cthulu
val result2 = write(animal)

val expected = """{"$type":"upickle.Cthulu"}"""
assert(result1 == expected)
assert(result2 == expected)
}

test ("caseObjectReader") - {
val json = """{"$type":"upickle.Cthulu"}"""
val result = read[Animal](json)
assert(result == Cthulu)
}

test("traitWriter") - {
Expand All @@ -73,7 +94,7 @@ object DerivationTests extends TestSuite {
assert(result == expected)
}

test("readWritrer") - {
test("readWriter") - {
val rw = summon[ReadWriter[Animal]]
val person = Person("Peter", "Somewhere", 30)
val json = write(person)(rw)
Expand Down

0 comments on commit 9655b81

Please sign in to comment.