Skip to content

Commit 8939a87

Browse files
committed
Code clean up
1 parent f2e1ef8 commit 8939a87

File tree

1 file changed

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

1 file changed

+12
-11
lines changed

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

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -974,16 +974,16 @@ object JsonCodecMaker {
974974
isStringified: Boolean,
975975
nonTransientFieldIndex: Int)
976976

977-
case class ClassInfo(tpe: TypeRepr, primaryConstructor: Symbol, paramLists: List[List[FieldInfo]]) {
977+
case class ClassInfo(tpe: TypeRepr, tpeTypeArgs: List[TypeRepr], primaryConstructor: Symbol, paramLists: List[List[FieldInfo]]) {
978978
val fields: List[FieldInfo] = paramLists.flatten.filter(!_.isTransient)
979979

980980
def genNew(arg: Term): Term = genNew(List(List(arg)))
981981

982982
def genNew(argss: List[List[Term]]): Term =
983983
val constructorNoTypes = Select(New(Inferred(tpe)), primaryConstructor)
984-
val constructor = typeArgs(tpe) match
985-
case Nil => constructorNoTypes
986-
case typeArgs => TypeApply(constructorNoTypes, typeArgs.map(Inferred(_)))
984+
val constructor =
985+
if (tpeTypeArgs.isEmpty) constructorNoTypes
986+
else TypeApply(constructorNoTypes, tpeTypeArgs.map(Inferred(_)))
987987
argss.tail.foldLeft(Apply(constructor, argss.head))((acc, args) => Apply(acc, args))
988988
}
989989

@@ -1002,6 +1002,7 @@ object JsonCodecMaker {
10021002
if (cfg.scalaTransientSupport) s"'${Type.show[transient]}' (or '${Type.show[scala.transient]}')"
10031003
else s"'${Type.show[transient]}')"
10041004

1005+
val tpeTypeArgs = typeArgs(tpe)
10051006
val tpeClassSym = tpe.classSymbol.getOrElse(fail(s"Expected that ${tpe.show} has classSymbol"))
10061007
var annotations = Map.empty[String, FieldAnnotations]
10071008
tpeClassSym.fieldMembers.foreach {
@@ -1060,7 +1061,7 @@ object JsonCodecMaker {
10601061
val dvSelectNoTArgs = Ref(tpe.typeSymbol.companionModule).select(methodSymbol)
10611062
val dvSelect = methodSymbol.paramSymss match
10621063
case Nil => dvSelectNoTArgs
1063-
case List(params) if params.exists(_.isTypeParam) => typeArgs(tpe) match
1064+
case List(params) if params.exists(_.isTypeParam) => tpeTypeArgs match
10641065
case Nil => fail(s"Expected that ${tpe.show} is an applied type")
10651066
case typeArgs => TypeApply(dvSelectNoTArgs, typeArgs.map(Inferred(_)))
10661067
case paramss => fail(s"Default method for ${symbol.name} of class ${tpe.show} have a complex " +
@@ -1070,7 +1071,7 @@ object JsonCodecMaker {
10701071
val isStringified = annotationOption.exists(_.stringified)
10711072
val isTransient = annotationOption.exists(_.transient)
10721073
val originFieldType = tpe.memberType(symbol).dealias
1073-
val fieldType = typeArgs(tpe) match
1074+
val fieldType = tpeTypeArgs match
10741075
case Nil => originFieldType
10751076
case typeArgs => originFieldType.substituteTypes(typeParams, typeArgs)
10761077
fieldType match
@@ -1094,7 +1095,7 @@ object JsonCodecMaker {
10941095
if (!isTransient) i += 1
10951096
i
10961097
}
1097-
ClassInfo(tpe, primaryConstructor, primaryConstructor.paramSymss match {
1098+
ClassInfo(tpe, tpeTypeArgs, primaryConstructor, primaryConstructor.paramSymss match {
10981099
case tps :: pss if tps.exists(_.isTypeParam) => pss.map(ps => createFieldInfos(ps, tps, fieldIndex))
10991100
case pss => pss.map(ps => createFieldInfos(ps, Nil, fieldIndex))
11001101
})
@@ -1207,8 +1208,7 @@ object JsonCodecMaker {
12071208
else if (isValueClass(tpe)) {
12081209
val vtpe = valueClassValueType(tpe)
12091210
vtpe.asType match
1210-
case '[vt] =>
1211-
getClassInfo(tpe).genNew(genReadKey[vt](vtpe :: types, in).asTerm).asExprOf[T]
1211+
case '[vt] => getClassInfo(tpe).genNew(genReadKey[vt](vtpe :: types, in).asTerm).asExprOf[T]
12121212
} else if (tpe =:= TypeRepr.of[BigInt]) '{ $in.readKeyAsBigInt(${Expr(cfg.bigIntDigitsLimit)}) }.asExprOf[T]
12131213
else if (tpe =:= TypeRepr.of[BigDecimal]) {
12141214
val mc = withMathContextFor(cfg.bigDecimalPrecision)
@@ -1552,7 +1552,7 @@ object JsonCodecMaker {
15521552
} else s"No implicit '${TypeRepr.of[JsonValueCodec[_]].show}' defined for '${tpe.show}'.")
15531553

15541554
def namedValueOpt(namedAnnotation: Option[Term], tpe: TypeRepr): Option[String] = namedAnnotation.map {
1555-
case Apply(_, List(param)) => CompileTimeEval.evalExpr(param.asExprOf[String]).asTerm match // TODO: write testcase
1555+
case Apply(_, List(param)) => CompileTimeEval.evalExpr(param.asExprOf[String]).asTerm match
15561556
case Literal(StringConstant(s)) => s
15571557
case _ => fail(s"Cannot evaluate a parameter of the '@named' annotation in type '${tpe.show}': $param.")
15581558
case a => fail(s"Invalid named annotation ${a.show}")
@@ -1969,7 +1969,8 @@ object JsonCodecMaker {
19691969
else mappedNames
19701970
})
19711971
val required = fields.collect {
1972-
case fieldInfo if !((!cfg.requireDefaultFields && fieldInfo.symbol.flags.is(Flags.HasDefault)) || isOption(fieldInfo.resolvedTpe, types) || isNullable(fieldInfo.resolvedTpe) ||
1972+
case fieldInfo if !((!cfg.requireDefaultFields && fieldInfo.symbol.flags.is(Flags.HasDefault)) ||
1973+
isOption(fieldInfo.resolvedTpe, types) || isNullable(fieldInfo.resolvedTpe) ||
19731974
(!cfg.requireCollectionFields && isCollection(fieldInfo.resolvedTpe))) => fieldInfo.mappedName
19741975
}.toSet
19751976
val paramVarNum = fields.size

0 commit comments

Comments
 (0)