Skip to content

Commit

Permalink
Add an example of derive keyword usage for a complex generic type
Browse files Browse the repository at this point in the history
  • Loading branch information
plokhotnyuk committed Jul 12, 2024
1 parent 6a7d7ef commit c51c46a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1786,7 +1786,7 @@ object JsonCodecMaker {
tpe match
case ConstantType(c) => Literal(c).asExprOf[T]
case _ => cannotFindValueCodecError(tpe)
} else if (isEnumOrModuleValue(tpe)) Ref(tpe.termSymbol).asExprOf[T]
} else if (isEnumOrModuleValue(tpe)) Ref(tpe.termSymbol).asExprOf[T] // FIXME: add support of non top-level defiend enums
else if (isValueClass(tpe)) {
val tpe1 = valueClassValueType(tpe)
tpe1.asType match
Expand Down Expand Up @@ -2632,7 +2632,7 @@ object JsonCodecMaker {
if ($in.isNextToken('{')) {
$in.rollbackToken()
$in.skip()
${if (tpe =:= TypeRepr.of[None.type]) '{ None }.asExprOf[T] else Ref(tpe.termSymbol).asExprOf[T]}
${if (tpe =:= TypeRepr.of[None.type]) '{ None }.asExprOf[T] else Ref(tpe.termSymbol).asExprOf[T]} // FIXME: add support of non top-level defiend enums
} else $in.readNullOrTokenError($default, '{')
}
} else if (isSealedClass(tpe)) withDecoderFor(methodKey, default, in) { (in, default) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,17 @@ class JsonCodecMakerNewKeywordSpec extends VerifyingSpec {
verifySerDeser(summon[JsonValueCodec[TestEnum]], TestEnum.Value2("VVV"), """{"hint":"Value2","str":"VVV"}""")
}
}
"serialize and deserialize a complex generic types defined with `derives` keyword and a custom compile-time configurations" in {
verifySerDeser(make[LinkedList[Int]](CodecMakerConfig.withAllowRecursiveTypes(true)),
LinkedList.Node2[Int](2, LinkedList.Node[Int](1, LinkedList.End)),
"""{"type":"Node2","value":2,"next":{"value":1,"next":{"type":"End"}}}""")
}
}
}
}

inline given CodecMakerConfig = CodecMakerConfig.withAllowRecursiveTypes(true)

enum LinkedList[+T] derives ConfiguredJsonValueCodec: // Borrowed from https://github.com/com-lihaoyi/upickle/pull/607
case End
case Node(value: T, next: LinkedList[T])
case Node2(value: T, next: Node[T])

0 comments on commit c51c46a

Please sign in to comment.