Skip to content

Commit 09e65b8

Browse files
committed
Add tests for Scala 3 generic tuples + code clean up
1 parent c945dd4 commit 09e65b8

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

jsoniter-scala-macros/shared/src/main/scala-3/com/github/plokhotnyuk/jsoniter_scala/macros/JsonCodecMaker.scala

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -777,13 +777,13 @@ object JsonCodecMaker {
777777

778778
def isTuple(tpe: TypeRepr): Boolean = tpe <:< TypeRepr.of[Tuple]
779779

780-
def isTupleXXL(tpe: TypeRepr): Boolean = tpe match {
780+
def isGenericTuple(tpe: TypeRepr): Boolean = tpe match {
781781
case AppliedType(tTpe, _) if tTpe =:= TypeRepr.of[*:] => true
782782
case _ => false
783783
}
784784

785-
def tupleXXLTypeArgs(tpe: TypeRepr): List[TypeRepr] = tpe match {
786-
case AppliedType(_, List(typeArg, tail)) => typeArg.dealias :: tupleXXLTypeArgs(tail)
785+
def genericTupleTypeArgs(tpe: TypeRepr): List[TypeRepr] = tpe match {
786+
case AppliedType(_, List(typeArg, tail)) => typeArg.dealias :: genericTupleTypeArgs(tail)
787787
case _ => Nil
788788
}
789789

@@ -2637,9 +2637,9 @@ object JsonCodecMaker {
26372637
} else $in.readNullOrTokenError($default, '"')
26382638
}
26392639
} else if (isTuple(tpe)) withDecoderFor(methodKey, default, in) { (in, default) =>
2640-
val isXXL = isTupleXXL(tpe)
2640+
val isGeneric = isGenericTuple(tpe)
26412641
val indexedTypes =
2642-
if (isXXL) tupleXXLTypeArgs(tpe)
2642+
if (isGeneric) genericTupleTypeArgs(tpe)
26432643
else typeArgs(tpe)
26442644
val valDefs = indexedTypes.map {
26452645
var i = 0
@@ -2660,7 +2660,7 @@ object JsonCodecMaker {
26602660
}
26612661
val readCreateBlock = Block(valDefs, '{
26622662
if ($in.isNextToken(']')) ${
2663-
if (isXXL) {
2663+
if (isGeneric) {
26642664
Expr.ofTupleFromSeq(valDefs.map(x => Ref(x.symbol).asExprOf[Any]))
26652665
} else {
26662666
Apply(TypeApply(Select.unique(New(Inferred(tpe)), "<init>"),
@@ -3110,9 +3110,9 @@ object JsonCodecMaker {
31103110
else '{ $out.writeNonEscapedAsciiVal(($tx.name: String)) }
31113111
}
31123112
} else if (isTuple(tpe)) withEncoderFor(methodKey, m, out) { (out, x) =>
3113-
val isXXL = isTupleXXL(tpe)
3113+
val isGeneric = isGenericTuple(tpe)
31143114
val indexedTypes =
3115-
if (isXXL) tupleXXLTypeArgs(tpe)
3115+
if (isGeneric) genericTupleTypeArgs(tpe)
31163116
else typeArgs(tpe)
31173117
val writeFields = indexedTypes.map {
31183118
var i = 0
@@ -3121,7 +3121,7 @@ object JsonCodecMaker {
31213121
te.asType match
31223122
case '[t] =>
31233123
val select =
3124-
if (isXXL) {
3124+
if (isGeneric) {
31253125
val getter =
31263126
Select.unique(x.asTerm, "productElement").appliedTo(Literal(IntConstant(i - 1))).asExprOf[Any]
31273127
'{ $getter.asInstanceOf[t] }.asExprOf[t]

jsoniter-scala-macros/shared/src/test/scala-3/com/github/plokhotnyuk/jsoniter_scala/macros/JsonCodecMakerNewTypeSpec.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ class JsonCodecMakerNewTypeSpec extends VerifyingSpec {
6262
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, "24"),
6363
"""[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,"24"]""")
6464
}
65+
"serialize and deserialize Scala 3 generic tuples" in {
66+
verifySerDeser(make[(Byte *: Short *: Int *: Long *: EmptyTuple)],
67+
(1: Byte) *: (2: Short) *: 3 *: 4L *: EmptyTuple,
68+
"""[1,2,3,4]""")
69+
}
6570
"serialize and deserialize Scala 3 immutable array" in {
6671
val json = """{"aa":[[1,2],[3,4]],"a":[1,2,3,4]}"""
6772
val iArrays = IArrays(IArray(IArray[Int](1, 2), IArray[Int](3, 4)), IArray[BigInt](1, 2, 3, 4))

0 commit comments

Comments
 (0)