Skip to content

Commit 27ccd04

Browse files
committed
Fixed redundant '$' character in the end of ADT leaf class names for non-singleton case object types
1 parent e04fa70 commit 27ccd04

File tree

1 file changed

+21
-18
lines changed
  • jsoniter-scala-macros/shared/src/main/scala-3/com/github/plokhotnyuk/jsoniter_scala/macros

1 file changed

+21
-18
lines changed

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

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -781,8 +781,9 @@ object JsonCodecMaker {
781781
case ConstantType(_) => true
782782
case _ => false
783783

784-
def isEnumOrModuleValue(tpe: TypeRepr): Boolean =
785-
(tpe.typeSymbol.flags.is(Flags.Module) || tpe.termSymbol.flags.is(Flags.Enum))
784+
def isEnumValue(tpe: TypeRepr): Boolean = tpe.termSymbol.flags.is(Flags.Enum)
785+
786+
def isEnumOrModuleValue(tpe: TypeRepr): Boolean = isEnumValue(tpe) || tpe.typeSymbol.flags.is(Flags.Module)
786787

787788
def isOption(tpe: TypeRepr, types: List[TypeRepr]): Boolean = tpe <:< TypeRepr.of[Option[_]] &&
788789
(cfg.skipNestedOptionValues || !types.headOption.exists(_ <:< TypeRepr.of[Option[_]]))
@@ -1130,7 +1131,7 @@ object JsonCodecMaker {
11301131
def adtLeafClasses(adtBaseTpe: TypeRepr): Seq[TypeRepr] = {
11311132
def collectRecursively(tpe: TypeRepr): Seq[TypeRepr] =
11321133
val leafTpes = adtChildren(tpe).flatMap { subTpe =>
1133-
if (isEnumOrModuleValue(subTpe) || subTpe =:= TypeRepr.of[None.type]) Seq(subTpe)
1134+
if (isEnumOrModuleValue(subTpe)) Seq(subTpe)
11341135
else if (isSealedClass(subTpe)) collectRecursively(subTpe)
11351136
else if (isValueClass(subTpe)) {
11361137
fail("'AnyVal' and one value classes with 'CodecMakerConfig.withInlineOneValueClasses(true)' are not " +
@@ -1531,15 +1532,19 @@ object JsonCodecMaker {
15311532
else '{ $in.unexpectedKeyError($l) }
15321533

15331534
def discriminatorValue(tpe: TypeRepr): String =
1534-
val isEnum = tpe.termSymbol.flags.is(Flags.Enum)
1535-
var named = (if (isEnum) tpe.termSymbol else tpe.typeSymbol).annotations.filter(_.tpe =:= TypeRepr.of[named])
1535+
val named =
1536+
(if (isEnumValue(tpe)) tpe.termSymbol else tpe.typeSymbol).annotations.filter(_.tpe =:= TypeRepr.of[named])
15361537
if (named.nonEmpty) {
15371538
if (named.size > 1) fail(s"Duplicated '${TypeRepr.of[named].show}' defined for '${tpe.show}'.")
15381539
namedValueOpt(named.headOption, tpe).get
15391540
} else cfg.adtLeafClassNameMapper({
1540-
if (tpe =:= TypeRepr.of[None.type]) "scala.None"
1541-
else if (isEnum || tpe.typeSymbol.flags.is(Flags.Module)) tpe.termSymbol.fullName
1542-
else tpe.typeSymbol.fullName
1541+
if (isEnumOrModuleValue(tpe)) {
1542+
if (tpe.isSingleton) tpe.termSymbol.fullName
1543+
else {
1544+
val name = tpe.typeSymbol.fullName
1545+
name.substring(0, name.length - 1)
1546+
}
1547+
} else tpe.typeSymbol.fullName
15431548
}).getOrElse(fail(s"Discriminator is not defined for ${tpe.show}"))
15441549

15451550
def checkFieldNameCollisions(tpe: TypeRepr, names: Seq[String]): Unit =
@@ -1788,7 +1793,7 @@ object JsonCodecMaker {
17881793
case ConstantType(c) => Literal(c).asExprOf[T]
17891794
case _ => cannotFindValueCodecError(tpe)
17901795
} else if (isEnumOrModuleValue(tpe)) {
1791-
Ref(if (tpe.termSymbol.flags.is(Flags.Enum)) tpe.termSymbol else tpe.typeSymbol.companionModule).asExprOf[T]
1796+
Ref(if (isEnumValue(tpe)) tpe.termSymbol else tpe.typeSymbol.companionModule).asExprOf[T]
17921797
} else if (isValueClass(tpe)) {
17931798
val tpe1 = valueClassValueType(tpe)
17941799
tpe1.asType match
@@ -1830,9 +1835,8 @@ object JsonCodecMaker {
18301835
$in.rollbackToMark()
18311836
${genReadLeafClass[st](subTpe)}
18321837
} else if (!cfg.circeLikeObjectEncoding && isEnumOrModuleValue(subTpe)) {
1833-
Ref(if (subTpe.termSymbol.flags.is(Flags.Enum)) subTpe.termSymbol else subTpe.typeSymbol.companionModule).asExprOf[T]
1834-
} else if (!cfg.circeLikeObjectEncoding && subTpe =:= TypeRepr.of[None.type]) '{ None }.asExprOf[st]
1835-
else genReadLeafClass[st](subTpe)
1838+
Ref(if (isEnumValue(subTpe)) subTpe.termSymbol else subTpe.typeSymbol.companionModule).asExprOf[T]
1839+
} else genReadLeafClass[st](subTpe)
18361840
} else $acc
18371841
}.asExprOf[T]
18381842
}
@@ -1902,7 +1906,7 @@ object JsonCodecMaker {
19021906

19031907
if (currentDiscriminator.isEmpty) {
19041908
val (leafModuleClasses, leafCaseClasses) = leafClasses.partition { x =>
1905-
!cfg.circeLikeObjectEncoding && (isEnumOrModuleValue(x) || x =:= TypeRepr.of[None.type])
1909+
!cfg.circeLikeObjectEncoding && isEnumOrModuleValue(x)
19061910
}
19071911
if (leafModuleClasses.nonEmpty && leafCaseClasses.nonEmpty) {
19081912
'{
@@ -2630,8 +2634,8 @@ object JsonCodecMaker {
26302634
if ($in.isNextToken('[')) ${readCreateBlock.asExprOf[T]}
26312635
else $in.readNullOrTokenError($default, '[')
26322636
}
2633-
} else if (isEnumOrModuleValue(tpe) || tpe =:= TypeRepr.of[None.type]) withDecoderFor(methodKey, default, in) { (in, default) =>
2634-
val x = Ref(if (tpe.termSymbol.flags.is(Flags.Enum)) tpe.termSymbol else tpe.typeSymbol.companionModule).asExprOf[T]
2637+
} else if (isEnumOrModuleValue(tpe)) withDecoderFor(methodKey, default, in) { (in, default) =>
2638+
val x = Ref(if (isEnumValue(tpe)) tpe.termSymbol else tpe.typeSymbol.companionModule).asExprOf[T]
26352639
'{
26362640
if ($in.isNextToken('{')) {
26372641
$in.rollbackToken()
@@ -3067,8 +3071,7 @@ object JsonCodecMaker {
30673071
}
30683072
if (writeFields.isEmpty) fail(s"Expected that ${tpe.show} should be an applied type")
30693073
Block('{ $out.writeArrayStart() }.asTerm :: writeFields, '{ $out.writeArrayEnd() }.asTerm).asExprOf[Unit]
3070-
} else if ((isEnumOrModuleValue(tpe) || tpe =:= TypeRepr.of[None.type]) &&
3071-
!(cfg.alwaysEmitDiscriminator && hasSealedParent(tpe))) withEncoderFor(methodKey, m, out) { (out, x) =>
3074+
} else if (isEnumOrModuleValue(tpe) && !(cfg.alwaysEmitDiscriminator && hasSealedParent(tpe))) withEncoderFor(methodKey, m, out) { (out, x) =>
30723075
'{
30733076
$out.writeObjectStart()
30743077
${optWriteDiscriminator.fold('{})(_.write(out))}
@@ -3084,7 +3087,7 @@ object JsonCodecMaker {
30843087
val leafClasses = adtLeafClasses(tpe)
30853088
val writeSubclasses = cfg.discriminatorFieldName.fold {
30863089
leafClasses.map { subTpe =>
3087-
if (!cfg.circeLikeObjectEncoding && (isEnumOrModuleValue(subTpe) || subTpe =:= TypeRepr.of[None.type])) {
3090+
if (!cfg.circeLikeObjectEncoding && isEnumOrModuleValue(subTpe)) {
30883091
CaseDef(Typed(x.asTerm, Inferred(subTpe)), None,
30893092
genWriteConstantVal(discriminatorValue(subTpe), out).asTerm)
30903093
} else {

0 commit comments

Comments
 (0)