Skip to content

Commit ad86a2b

Browse files
committed
Fix misprint in compile error + reduce code duplication
1 parent 1d09d3f commit ad86a2b

File tree

2 files changed

+22
-24
lines changed

2 files changed

+22
-24
lines changed

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -681,8 +681,6 @@ object JsonCodecMaker {
681681

682682
def isTuple(tpe: Type): Boolean = tupleSymbols(tpe.typeSymbol)
683683

684-
def valueClassValueMethod(tpe: Type): MethodSymbol = tpe.decls.head.asMethod
685-
686684
def decodeName(s: Symbol): String = NameTransformer.decode(s.name.toString)
687685

688686
def decodeFieldName(s: Symbol): String = NameTransformer.decode(s.name.toString.trim) // FIXME: Why is there a space at the end of field name?!
@@ -695,9 +693,9 @@ object JsonCodecMaker {
695693
else mtpe.substituteTypes(tpeTypeParams, tpe.typeArgs)
696694
}
697695

698-
def paramType(tpe: Type, p: TermSymbol): Type = resolveConcreteType(tpe, p.typeSignature.dealias)
696+
def valueClassValueSymbol(tpe: Type): MethodSymbol = tpe.decls.head.asMethod
699697

700-
def valueClassValueType(tpe: Type): Type = resolveConcreteType(tpe, valueClassValueMethod(tpe).returnType.dealias)
698+
def valueClassValueType(tpe: Type): Type = resolveConcreteType(tpe, valueClassValueSymbol(tpe).returnType.dealias)
701699

702700
def isNonAbstractScalaClass(tpe: Type): Boolean =
703701
tpe.typeSymbol.isClass && !tpe.typeSymbol.isAbstract && !tpe.typeSymbol.isJava
@@ -939,7 +937,8 @@ object JsonCodecMaker {
939937
Some(q"$module.${TermName("$lessinit$greater$default$" + i)}")
940938
} else None
941939
val isStringified = annotationOption.exists(_.stringified)
942-
Some(FieldInfo(symbol, mappedName, tmpName, getter, defaultValue, paramType(tpe, symbol), isStringified))
940+
val resolvedTpe = resolveConcreteType(tpe, symbol.typeSignature.dealias)
941+
Some(FieldInfo(symbol, mappedName, tmpName, getter, defaultValue, resolvedTpe, isStringified))
943942
}
944943
})
945944
})
@@ -1191,7 +1190,7 @@ object JsonCodecMaker {
11911190
tpe =:= typeOf[LocalTime] || tpe =:= typeOf[MonthDay] || tpe =:= typeOf[OffsetDateTime] ||
11921191
tpe =:= typeOf[OffsetTime] || tpe =:= typeOf[Period] || tpe =:= typeOf[Year] || tpe =:= typeOf[YearMonth] ||
11931192
tpe =:= typeOf[ZonedDateTime] || tpe =:= typeOf[ZoneId] || tpe =:= typeOf[ZoneOffset]) q"out.writeKey($x)"
1194-
else if (isValueClass(tpe)) genWriteKey(q"$x.${valueClassValueMethod(tpe)}", valueClassValueType(tpe) :: types)
1193+
else if (isValueClass(tpe)) genWriteKey(q"$x.${valueClassValueSymbol(tpe)}", valueClassValueType(tpe) :: types)
11951194
else if (tpe <:< typeOf[Enumeration#Value]) {
11961195
if (cfg.useScalaEnumValueId) q"out.writeKey($x.id)"
11971196
else q"out.writeKey($x.toString)"
@@ -2136,7 +2135,7 @@ object JsonCodecMaker {
21362135
tpe =:= typeOf[Period] || tpe =:= typeOf[Year] || tpe =:= typeOf[YearMonth] ||
21372136
tpe =:= typeOf[ZonedDateTime] || tpe =:= typeOf[ZoneId] || tpe =:= typeOf[ZoneOffset]) q"out.writeVal($m)"
21382137
else if (isValueClass(tpe)) {
2139-
genWriteVal(q"$m.${valueClassValueMethod(tpe)}", valueClassValueType(tpe) :: types, isStringified, EmptyTree)
2138+
genWriteVal(q"$m.${valueClassValueSymbol(tpe)}", valueClassValueType(tpe) :: types, isStringified, EmptyTree)
21402139
} else if (isOption(tpe, types.tail)) {
21412140
q"""if ($m ne _root_.scala.None) ${genWriteVal(q"$m.get", typeArg1(tpe) :: types, isStringified, EmptyTree)}
21422141
else out.writeNull()"""

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

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -763,9 +763,9 @@ object JsonCodecMaker {
763763

764764
def isTuple(tpe: TypeRepr): Boolean = tpe <:< TypeRepr.of[Tuple]
765765

766-
def valueClassValueSymbol(tpe: TypeRepr): Symbol = tpe.typeSymbol.fieldMembers(0)
766+
def valueClassValueSymbol(tpe: TypeRepr): Symbol = tpe.typeSymbol.fieldMembers.head
767767

768-
def valueClassValueType(tpe: TypeRepr): TypeRepr = tpe.memberType(tpe.typeSymbol.fieldMembers(0)).dealias
768+
def valueClassValueType(tpe: TypeRepr): TypeRepr = tpe.memberType(valueClassValueSymbol(tpe)).dealias
769769

770770
def isNonAbstractScalaClass(tpe: TypeRepr): Boolean = tpe.classSymbol.fold(false) { sym =>
771771
val flags = sym.flags
@@ -788,9 +788,10 @@ object JsonCodecMaker {
788788
def isOption(tpe: TypeRepr, types: List[TypeRepr]): Boolean = tpe <:< TypeRepr.of[Option[_]] &&
789789
(cfg.skipNestedOptionValues || !types.headOption.exists(_ <:< TypeRepr.of[Option[_]]))
790790

791-
def isCollection(tpe: TypeRepr): Boolean =
792-
tpe <:< TypeRepr.of[Iterable[_]] || tpe <:< TypeRepr.of[Iterator[_]] || tpe <:< TypeRepr.of[Array[_]] ||
793-
tpe.typeSymbol.fullName == "scala.IArray$package$.IArray"
791+
def isIArray(tpe: TypeRepr): Boolean = tpe.typeSymbol.fullName == "scala.IArray$package$.IArray"
792+
793+
def isCollection(tpe: TypeRepr): Boolean = tpe <:< TypeRepr.of[Iterable[_]] || tpe <:< TypeRepr.of[Iterator[_]] ||
794+
tpe <:< TypeRepr.of[Array[_]] || isIArray(tpe)
794795

795796
def isJavaEnum(tpe: TypeRepr): Boolean = tpe <:< TypeRepr.of[java.lang.Enum[_]]
796797

@@ -1121,7 +1122,7 @@ object JsonCodecMaker {
11211122
case AppliedType(base, _) => base.appliedTo(ctArgs)
11221123
case AnnotatedType(AppliedType(base, _), annot) => AnnotatedType(base.appliedTo(ctArgs), annot)
11231124
case _ => polyRes.appliedTo(ctArgs)
1124-
case other => fail(s"Primary constructior for ${tpe.show} is not MethodType or PolyType but $other")
1125+
case other => fail(s"Primary constructor for ${tpe.show} is not MethodType or PolyType but $other")
11251126
} else if (sym.isTerm) Ref(sym).tpe
11261127
else fail("Only concrete (no free type parametes) Scala classes & objects are supported for ADT leaf classes. " +
11271128
s"Please consider using of them for ADT with base '${tpe.show}' or provide a custom implicitly accessible codec for the ADT base.")
@@ -1627,7 +1628,7 @@ object JsonCodecMaker {
16271628
}
16281629
})
16291630
}
1630-
} else if (tpe1.typeSymbol.fullName == "scala.IArray$package$.IArray") {
1631+
} else if (isIArray(tpe1)) {
16311632
tpe1.asType match
16321633
case '[t1] =>
16331634
val x1 = x1t.asExprOf[IArray[t1]]
@@ -1646,7 +1647,7 @@ object JsonCodecMaker {
16461647
}
16471648
})
16481649
}
1649-
} else if (tpe.typeSymbol.fullName == "scala.IArray$package$.IArray") {
1650+
} else if (isIArray(tpe)) {
16501651
if (tpe1 =:= TypeRepr.of[AnyRef]) {
16511652
'{ IArray.equals(${x1t.asExprOf[IArray[AnyRef]]}, ${x2t.asExprOf[IArray[AnyRef]]}) }
16521653
} else {
@@ -1785,7 +1786,7 @@ object JsonCodecMaker {
17851786
} else if (tpe <:< TypeRepr.of[Array[_]]) withNullValueFor(tpe) {
17861787
typeArg1(tpe).asType match
17871788
case '[t1] => genNewArray[t1]('{ 0 }).asExprOf[T]
1788-
} else if (tpe.typeSymbol.fullName == "scala.IArray$package$.IArray") withNullValueFor(tpe) {
1789+
} else if (isIArray(tpe)) withNullValueFor(tpe) {
17891790
typeArg1(tpe).asType match
17901791
case '[t1] => '{ IArray.unsafeFromArray(${genNewArray[t1]('{ 0 })}) }.asExprOf[T]
17911792
} else if (isConstType(tpe)) {
@@ -2255,8 +2256,7 @@ object JsonCodecMaker {
22552256
}
22562257
}.asExprOf[T]
22572258
} else if (decodeMethodSym.isDefined) Apply(Ref(decodeMethodSym.get), List(in.asTerm, default.asTerm)).asExprOf[T]
2258-
else if (tpe <:< TypeRepr.of[Array[_]] || tpe <:< TypeRepr.of[immutable.ArraySeq[_]] ||
2259-
tpe.typeSymbol.fullName == "scala.IArray$package$.IArray" ||
2259+
else if (tpe <:< TypeRepr.of[Array[_]] || tpe <:< TypeRepr.of[immutable.ArraySeq[_]] || isIArray(tpe) ||
22602260
tpe <:< TypeRepr.of[mutable.ArraySeq[_]]) withDecoderFor(methodKey, default, in) { (in, default) =>
22612261
val tpe1 = typeArg1(tpe)
22622262
val newArrayOnChange = tpe1 match
@@ -2300,7 +2300,7 @@ object JsonCodecMaker {
23002300
if ($i != $l) ${Assign(x.asTerm, shrinkArray(x, i).asTerm).asExprOf[Unit]}
23012301
mutable.ArraySeq.make[t1]($x)
23022302
}.asExprOf[mutable.ArraySeq[t1]], in).asExprOf[T]
2303-
} else if (tpe.typeSymbol.fullName == "scala.IArray$package$.IArray") {
2303+
} else if (isIArray(tpe)) {
23042304
genReadArray(l => genNewArray[t1](l), (x, i, l) => '{
23052305
if ($i == $l) {
23062306
${Assign(l.asTerm, '{ $l << 1 }.asTerm).asExprOf[Unit]}
@@ -2701,7 +2701,7 @@ object JsonCodecMaker {
27012701
${genWriteVal('v, fTpe :: types, fieldInfo.isStringified, None, out)}
27022702
}
27032703
}
2704-
} else if (fTpe.typeSymbol.fullName == "scala.IArray$package$.IArray") {
2704+
} else if (isIArray(fTpe)) {
27052705
val fTpe1 = typeArg1(fTpe)
27062706
fTpe1.asType match
27072707
case '[ft1] => {
@@ -2756,7 +2756,7 @@ object JsonCodecMaker {
27562756
${genWriteConstantKey(fieldInfo.mappedName, out)}
27572757
${genWriteVal('v, fTpe :: types, fieldInfo.isStringified, None, out)}
27582758
}
2759-
} else if (cfg.transientEmpty && fTpe.typeSymbol.fullName == "scala.IArray$package$.IArray") {
2759+
} else if (cfg.transientEmpty && isIArray(fTpe)) {
27602760
val fTpe1 = typeArg1(fTpe)
27612761
fTpe1.asType match
27622762
case '[ft1] => '{
@@ -2894,8 +2894,7 @@ object JsonCodecMaker {
28942894
else $out.writeNull()
28952895
}
28962896
} else if (encodeMethodSym.isDefined) Apply(Ref(encodeMethodSym.get), List(m.asTerm, out.asTerm)).asExprOf[Unit]
2897-
else if (tpe <:< TypeRepr.of[Array[_]] || tpe <:< TypeRepr.of[immutable.ArraySeq[_]] ||
2898-
tpe.typeSymbol.fullName == "scala.IArray$package$.IArray" ||
2897+
else if (tpe <:< TypeRepr.of[Array[_]] || tpe <:< TypeRepr.of[immutable.ArraySeq[_]] || isIArray(tpe) ||
28992898
tpe <:< TypeRepr.of[mutable.ArraySeq[_]]) withEncoderFor(methodKey, m, out) { (out, x) =>
29002899
val tpe1 = typeArg1(tpe)
29012900
tpe1.asType match
@@ -2926,7 +2925,7 @@ object JsonCodecMaker {
29262925
}
29272926
$out.writeArrayEnd()
29282927
}
2929-
} else if (tpe.typeSymbol.fullName == "scala.IArray$package$.IArray") {
2928+
} else if (isIArray(tpe)) {
29302929
val tx = x.asExprOf[IArray[t1]]
29312930
'{
29322931
$out.writeArrayStart()

0 commit comments

Comments
 (0)