Skip to content

Commit

Permalink
Fix type aliasing of basic types tripping up derivation, closes #388
Browse files Browse the repository at this point in the history
  • Loading branch information
sirthias committed Apr 27, 2021
1 parent f61a48a commit 407217b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ object MapBasedCodecs {
val key = p.key()
val writeKey = q"w.${TermName(s"write${key.productPrefix}")}(${literal(key.value)})"
val isBasic = basicParams contains p
val method = TermName(s"write${if (isBasic) p.paramType.tpe.toString else ""}")
val method = TermName(s"write${if (isBasic) p.paramType.tpe.dealias.toString else ""}")
val rawWriteEntry = q"$writeKey.$method(value.${p.name})"
if (isBasic) {
if (p.defaultValueMethod.isDefined) q"if (${encName(p, "o")}) $rawWriteEntry" else rawWriteEntry
Expand Down Expand Up @@ -251,7 +251,7 @@ object MapBasedCodecs {
params.filterNot(p => p.isBasicType && decodersForParams(p).exists(isDefinedOn(_, decoderCompanion)))

def readField(p: CaseParam) = {
val tpe = p.paramType.tpe
val tpe = p.paramType.tpe.dealias
if (nonBasicParams contains p) q"r.read[$tpe]()(${decName(p)})" else q"r.${TermName(s"read$tpe")}()"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,18 @@ object MiscSpec extends AbstractBorerSpec {

"Custom 'Unit' type" - {
sealed trait Unit
implicit val unitDecoder: Decoder[Unit] = Decoder(_ => null)
implicit val unitDecoder: Decoder[Unit] = Decoder { r => r.readNull(); null }

final case class Foo(value: Double, unit: Unit)
implicit val fooDecoder: Decoder[Foo] = MapBasedCodecs.deriveDecoder
verifyDecoding("null", null)
}

"Type alias" - {
type Foo = String
case class Bar(bar: Foo)
implicit val barCodec: Codec[Bar] = MapBasedCodecs.deriveCodec
roundTrip("""{"bar":"yeah"}""", Bar("yeah"))
}
}
}

0 comments on commit 407217b

Please sign in to comment.