Skip to content

Commit e230c3b

Browse files
committed
Code clean up
1 parent 0e96eb7 commit e230c3b

File tree

1 file changed

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

1 file changed

+117
-121
lines changed

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

Lines changed: 117 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -928,7 +928,7 @@ private class JsonCodecMakerInstance(cfg: CodecMakerConfig)(using Quotes) {
928928

929929
@tailrec
930930
private def opaqueDealias(tpe: TypeRepr): TypeRepr = tpe match
931-
case trTpe @ TypeRef(_, _) if trTpe.isOpaqueAlias => opaqueDealias(trTpe.translucentSuperType.dealias)
931+
case trTpe: TypeRef if trTpe.isOpaqueAlias => opaqueDealias(trTpe.translucentSuperType.dealias)
932932
case _ => tpe
933933

934934
private def isSealedClass(tpe: TypeRepr): Boolean = tpe.typeSymbol.flags.is(Flags.Sealed)
@@ -937,7 +937,7 @@ private class JsonCodecMakerInstance(cfg: CodecMakerConfig)(using Quotes) {
937937
isSealedClass(tpe) || tpe.baseClasses.exists(_.flags.is(Flags.Sealed))
938938

939939
private def isConstType(tpe: TypeRepr): Boolean = tpe match
940-
case ConstantType(_) => true
940+
case _: ConstantType => true
941941
case _ => false
942942

943943
private def isEnumValue(tpe: TypeRepr): Boolean = tpe.termSymbol.flags.is(Flags.Enum)
@@ -1126,10 +1126,9 @@ private class JsonCodecMakerInstance(cfg: CodecMakerConfig)(using Quotes) {
11261126
val primaryConstructor = tpeClassSym.primaryConstructor
11271127
var annotations = Map.empty[String, FieldAnnotations]
11281128
val caseFields = tpeClassSym.caseFields
1129-
var companionClassMethodMembers: List[Symbol] = null
1129+
var companionRefAndMembers: (Ref, List[Symbol]) = null
11301130
var fieldMembers: List[Symbol] = null
11311131
var methodMembers: List[Symbol] = null
1132-
val companionModuleRef = Ref(tpe.typeSymbol.companionModule)
11331132

11341133
tpeClassSym.fieldMembers.foreach {
11351134
case m: Symbol if hasSupportedAnnotation(m) =>
@@ -1157,38 +1156,36 @@ private class JsonCodecMakerInstance(cfg: CodecMakerConfig)(using Quotes) {
11571156
var fieldTpe = tpe.memberType(symbol).dealias
11581157
if (tpeTypeArgs ne Nil) fieldTpe = fieldTpe.substituteTypes(typeParams, tpeTypeArgs)
11591158
fieldTpe match
1160-
case TypeLambda(_, _, _) =>
1159+
case _: TypeLambda =>
11611160
fail(s"Type lambdas are not supported for type '${tpe.show}' with field type for $name '${fieldTpe.show}'")
1162-
case TypeBounds(_, _) =>
1161+
case _: TypeBounds =>
11631162
fail(s"Type bounds are not supported for type '${tpe.show}' with field type for $name '${fieldTpe.show}'")
11641163
case _ =>
11651164
val defaultValue = if (!cfg.requireDefaultFields && symbol.flags.is(Flags.HasDefault)) {
11661165
val dvMemberName = "$lessinit$greater$default$" + i
1167-
if (companionClassMethodMembers eq null) {
1168-
companionClassMethodMembers = tpe.typeSymbol.companionClass.methodMembers
1166+
if (companionRefAndMembers eq null) {
1167+
val typeSymbol = tpe.typeSymbol
1168+
companionRefAndMembers = (Ref(typeSymbol.companionModule), typeSymbol.companionClass.methodMembers)
11691169
}
1170-
companionClassMethodMembers.collectFirst { case methodSymbol if methodSymbol.name == dvMemberName =>
1171-
val dvSelectNoTypes = Select(companionModuleRef, methodSymbol)
1170+
companionRefAndMembers._2.collectFirst { case methodSymbol if methodSymbol.name == dvMemberName =>
1171+
val dvSelectNoTypes = Select(companionRefAndMembers._1, methodSymbol)
11721172
methodSymbol.paramSymss match
11731173
case Nil => dvSelectNoTypes
11741174
case List(params) if params.exists(_.isTypeParam) => TypeApply(dvSelectNoTypes, tpeTypeArgs.map(Inferred(_)))
11751175
case paramss => fail(s"Default method for $name of class ${tpe.show} have a complex parameter list: $paramss")
11761176
}
11771177
} else None
1178-
val getterOrField = caseFields.find(_.name == name) match {
1178+
val getterOrField = caseFields.find(_.name == name) match
11791179
case Some(caseField) => caseField
11801180
case _ =>
11811181
if (fieldMembers eq null) fieldMembers = tpeClassSym.fieldMembers
1182-
fieldMembers.find(_.name == name) match {
1182+
fieldMembers.find(_.name == name) match
11831183
case Some(fieldMember) => fieldMember
11841184
case _ =>
11851185
if (methodMembers eq null) methodMembers = tpeClassSym.methodMembers
1186-
methodMembers.find(x => x.flags.is(Flags.FieldAccessor) && x.name == name) match {
1186+
methodMembers.find(x => x.flags.is(Flags.FieldAccessor) && x.name == name) match
11871187
case Some(methodMember) => methodMember
11881188
case _ => Symbol.noSymbol
1189-
}
1190-
}
1191-
}
11921189
if (!getterOrField.exists || getterOrField.flags.is(Flags.PrivateLocal)) {
11931190
fail(s"Getter or field '$name' of '${tpe.show}' is private. It should be defined as 'val' or 'var' in the primary constructor.")
11941191
}
@@ -1252,7 +1249,7 @@ private class JsonCodecMakerInstance(cfg: CodecMakerConfig)(using Quotes) {
12521249
val nudeSubtype = sym.typeRef
12531250
val tpeArgsFromChild = typeArgs(nudeSubtype.baseType(typeSymbol))
12541251
nudeSubtype.memberType(sym.primaryConstructor) match
1255-
case MethodType(_, _, _) => nudeSubtype
1252+
case _: MethodType => nudeSubtype
12561253
case PolyType(names, _, resPolyTp) =>
12571254
val tpBinding = resolveParentTypeArgs(sym, tpeArgsFromChild, typeArgs(tpe), Map.empty)
12581255
val ctArgs = names.map { name =>
@@ -1283,7 +1280,7 @@ private class JsonCodecMakerInstance(cfg: CodecMakerConfig)(using Quotes) {
12831280
fail("'AnyVal' and one value classes with 'CodecMakerConfig.withInlineOneValueClasses(true)' are not " +
12841281
s"supported as leaf classes for ADT with base '${adtBaseTpe.show}'.")
12851282
} else if (isNonAbstractScalaClass(subTpe)) Seq(subTpe)
1286-
else fail((if (subTpe.typeSymbol.flags.is(Flags.Abstract) || subTpe.typeSymbol.flags.is(Flags.Trait) ) {
1283+
else fail((if (subTpe.typeSymbol.flags.is(Flags.Abstract) || subTpe.typeSymbol.flags.is(Flags.Trait)) {
12871284
"Only sealed intermediate traits or abstract classes are supported."
12881285
} else {
12891286
"Only concrete (no free type parameters) Scala classes & objects are supported for ADT leaf classes."
@@ -2372,7 +2369,7 @@ private class JsonCodecMakerInstance(cfg: CodecMakerConfig)(using Quotes) {
23722369
val tpe1 = typeArg1(tpe)
23732370
val types1 = tpe1 :: types
23742371
val newArrayOnChange = tpe1 match
2375-
case AppliedType(_, _) => true
2372+
case _: AppliedType => true
23762373
case _ => isValueClass(tpe1) || isOpaque(tpe1)
23772374
tpe1.asType match
23782375
case '[t1] =>
@@ -2818,123 +2815,122 @@ private class JsonCodecMakerInstance(cfg: CodecMakerConfig)(using Quotes) {
28182815
} else Select.unique(valRef, s"_${i + 1}")
28192816
case _ => Select(valRef, fieldInfo.getterOrField)
28202817
}.asExpr
2821-
(fTpe.asType match {
2822-
case '[ft] =>
2823-
fDefault match {
2824-
case Some(d) =>
2825-
if (cfg.transientEmpty && fTpe <:< TypeRepr.of[Iterable[?]]) '{
2826-
val v = ${getter.asInstanceOf[Expr[ft & Iterable[?]]]}
2827-
if (!v.isEmpty && v != ${d.asExpr.asInstanceOf[Expr[ft]]}) {
2828-
${genWriteConstantKey(fieldInfo.mappedName, out)}
2829-
${genWriteVal('v, allTypes, fieldInfo.isStringified, None, out)}
2830-
}
2831-
} else if (cfg.transientEmpty && fTpe <:< TypeRepr.of[Iterator[?]]) '{
2832-
val v = ${getter.asInstanceOf[Expr[ft & Iterator[?]]]}
2833-
if (v.hasNext && v != ${d.asExpr.asInstanceOf[Expr[ft]]}) {
2834-
${genWriteConstantKey(fieldInfo.mappedName, out)}
2835-
${genWriteVal('v, allTypes, fieldInfo.isStringified, None, out)}
2836-
}
2837-
} else if (cfg.transientNone && isOption(fTpe, types)) {
2838-
val tpe1 = typeArg1(fTpe)
2839-
tpe1.asType match
2840-
case '[t1] => '{
2841-
val v = ${getter.asInstanceOf[Expr[Option[t1]]]}
2842-
if ((v ne None) && v != ${d.asExpr.asInstanceOf[Expr[ft]]}) {
2843-
${genWriteConstantKey(fieldInfo.mappedName, out)}
2844-
${genWriteVal('{v.get}, tpe1 :: allTypes, fieldInfo.isStringified, None, out)}
2845-
}
2846-
}
2847-
} else if (cfg.transientNull && isNullable(fTpe)) '{
2848-
val v = ${getter.asInstanceOf[Expr[ft]]}
2849-
if ((v != null) && v != ${d.asExpr.asInstanceOf[Expr[ft]]}) {
2850-
${genWriteConstantKey(fieldInfo.mappedName, out)}
2851-
${genWriteVal('v, allTypes, fieldInfo.isStringified, None, out)}
2852-
}
2853-
} else if (fTpe <:< TypeRepr.of[Array[?]]) {
2854-
def cond(v: Expr[Array[?]])(using Quotes): Expr[Boolean] =
2855-
val da = d.asExpr.asInstanceOf[Expr[Array[?]]]
2856-
if (cfg.transientEmpty) '{ $v.length != 0 && !${withEqualsFor(fTpe, v, da)((x1, x2) => genArrayEquals(fTpe, x1, x2))} }
2857-
else '{ !${withEqualsFor(fTpe, v, da)((x1, x2) => genArrayEquals(fTpe, x1, x2))} }
2858-
2859-
'{
2860-
val v = ${getter.asInstanceOf[Expr[ft & Array[?]]]}
2861-
if (${cond('v)}) {
2818+
(fTpe.asType match { case '[ft] =>
2819+
fDefault match {
2820+
case Some(d) =>
2821+
if (cfg.transientEmpty && fTpe <:< TypeRepr.of[Iterable[?]]) '{
2822+
val v = ${getter.asInstanceOf[Expr[ft & Iterable[?]]]}
2823+
if (!v.isEmpty && v != ${d.asExpr.asInstanceOf[Expr[ft]]}) {
2824+
${genWriteConstantKey(fieldInfo.mappedName, out)}
2825+
${genWriteVal('v, allTypes, fieldInfo.isStringified, None, out)}
2826+
}
2827+
} else if (cfg.transientEmpty && fTpe <:< TypeRepr.of[Iterator[?]]) '{
2828+
val v = ${getter.asInstanceOf[Expr[ft & Iterator[?]]]}
2829+
if (v.hasNext && v != ${d.asExpr.asInstanceOf[Expr[ft]]}) {
2830+
${genWriteConstantKey(fieldInfo.mappedName, out)}
2831+
${genWriteVal('v, allTypes, fieldInfo.isStringified, None, out)}
2832+
}
2833+
} else if (cfg.transientNone && isOption(fTpe, types)) {
2834+
val tpe1 = typeArg1(fTpe)
2835+
tpe1.asType match
2836+
case '[t1] => '{
2837+
val v = ${getter.asInstanceOf[Expr[Option[t1]]]}
2838+
if ((v ne None) && v != ${d.asExpr.asInstanceOf[Expr[ft]]}) {
28622839
${genWriteConstantKey(fieldInfo.mappedName, out)}
2863-
${genWriteVal('v, allTypes, fieldInfo.isStringified, None, out)}
2864-
}
2865-
}
2866-
} else if (isIArray(fTpe)) {
2867-
typeArg1(fTpe).asType match
2868-
case '[ft1] => {
2869-
def cond(v: Expr[IArray[ft1]])(using Quotes): Expr[Boolean] =
2870-
val da = d.asExpr.asInstanceOf[Expr[IArray[ft1]]]
2871-
if (cfg.transientEmpty) '{ $v.length != 0 && !${withEqualsFor(fTpe, v, da)((x1, x2) => genArrayEquals(fTpe, x1, x2))} }
2872-
else '{ !${withEqualsFor(fTpe, v, da)((x1, x2) => genArrayEquals(fTpe, x1, x2))} }
2873-
2874-
'{
2875-
val v = ${getter.asInstanceOf[Expr[IArray[ft1]]]}
2876-
if (${cond('v)}) {
2877-
${genWriteConstantKey(fieldInfo.mappedName, out)}
2878-
${genWriteVal('v, allTypes, fieldInfo.isStringified, None, out)}
2879-
}
2880-
}
2840+
${genWriteVal('{v.get}, tpe1 :: allTypes, fieldInfo.isStringified, None, out)}
28812841
}
2882-
} else '{
2883-
val v = ${getter.asInstanceOf[Expr[ft]]}
2884-
if (v != ${d.asExpr.asInstanceOf[Expr[ft]]}) {
2885-
${genWriteConstantKey(fieldInfo.mappedName, out)}
2886-
${genWriteVal('v, allTypes, fieldInfo.isStringified, None, out)}
28872842
}
2843+
} else if (cfg.transientNull && isNullable(fTpe)) '{
2844+
val v = ${getter.asInstanceOf[Expr[ft]]}
2845+
if ((v != null) && v != ${d.asExpr.asInstanceOf[Expr[ft]]}) {
2846+
${genWriteConstantKey(fieldInfo.mappedName, out)}
2847+
${genWriteVal('v, allTypes, fieldInfo.isStringified, None, out)}
28882848
}
2889-
case None =>
2890-
if (cfg.transientEmpty && fTpe <:< TypeRepr.of[Iterable[?]]) '{
2891-
val v = ${getter.asInstanceOf[Expr[ft & Iterable[?]]]}
2892-
if (!v.isEmpty) {
2893-
${genWriteConstantKey(fieldInfo.mappedName, out)}
2894-
${genWriteVal('v, allTypes, fieldInfo.isStringified, None, out)}
2895-
}
2896-
} else if (cfg.transientEmpty && fTpe <:< TypeRepr.of[Iterator[?]]) '{
2897-
val v = ${getter.asInstanceOf[Expr[ft & Iterator[?]]]}
2898-
if (v.hasNext) {
2899-
${genWriteConstantKey(fieldInfo.mappedName, out)}
2900-
${genWriteVal('v, allTypes, fieldInfo.isStringified, None, out)}
2901-
}
2902-
} else if (cfg.transientNone && isOption(fTpe, types)) {
2903-
val tpe1 = typeArg1(fTpe)
2904-
tpe1.asType match
2905-
case '[tf] => '{
2906-
val v = ${getter.asInstanceOf[Expr[Option[tf]]]}
2907-
if (v ne None) {
2908-
${genWriteConstantKey(fieldInfo.mappedName, out)}
2909-
${genWriteVal('{ v.get }, tpe1 :: allTypes, fieldInfo.isStringified, None, out)}
2910-
}
2911-
}
2912-
} else if (cfg.transientNull && isNullable(fTpe)) '{
2913-
val v = ${getter.asInstanceOf[Expr[ft]]}
2914-
if (v != null) {
2915-
${genWriteConstantKey(fieldInfo.mappedName, out)}
2916-
${genWriteVal('v, allTypes, fieldInfo.isStringified, None, out)}
2917-
}
2918-
} else if (cfg.transientEmpty && fTpe <:< TypeRepr.of[Array[?]]) '{
2849+
} else if (fTpe <:< TypeRepr.of[Array[?]]) {
2850+
def cond(v: Expr[Array[?]])(using Quotes): Expr[Boolean] =
2851+
val da = d.asExpr.asInstanceOf[Expr[Array[?]]]
2852+
if (cfg.transientEmpty) '{ $v.length != 0 && !${withEqualsFor(fTpe, v, da)((x1, x2) => genArrayEquals(fTpe, x1, x2))} }
2853+
else '{ !${withEqualsFor(fTpe, v, da)((x1, x2) => genArrayEquals(fTpe, x1, x2))} }
2854+
2855+
'{
29192856
val v = ${getter.asInstanceOf[Expr[ft & Array[?]]]}
2920-
if (v.length != 0) {
2857+
if (${cond('v)}) {
29212858
${genWriteConstantKey(fieldInfo.mappedName, out)}
29222859
${genWriteVal('v, allTypes, fieldInfo.isStringified, None, out)}
29232860
}
2924-
} else if (cfg.transientEmpty && isIArray(fTpe)) {
2925-
typeArg1(fTpe).asType match
2926-
case '[ft1] => '{
2861+
}
2862+
} else if (isIArray(fTpe)) {
2863+
typeArg1(fTpe).asType match
2864+
case '[ft1] => {
2865+
def cond(v: Expr[IArray[ft1]])(using Quotes): Expr[Boolean] =
2866+
val da = d.asExpr.asInstanceOf[Expr[IArray[ft1]]]
2867+
if (cfg.transientEmpty) '{ $v.length != 0 && !${withEqualsFor(fTpe, v, da)((x1, x2) => genArrayEquals(fTpe, x1, x2))} }
2868+
else '{ !${withEqualsFor(fTpe, v, da)((x1, x2) => genArrayEquals(fTpe, x1, x2))} }
2869+
2870+
'{
29272871
val v = ${getter.asInstanceOf[Expr[IArray[ft1]]]}
2928-
if (v.length != 0) {
2872+
if (${cond('v)}) {
29292873
${genWriteConstantKey(fieldInfo.mappedName, out)}
29302874
${genWriteVal('v, allTypes, fieldInfo.isStringified, None, out)}
29312875
}
29322876
}
2933-
} else '{
2877+
}
2878+
} else '{
2879+
val v = ${getter.asInstanceOf[Expr[ft]]}
2880+
if (v != ${d.asExpr.asInstanceOf[Expr[ft]]}) {
29342881
${genWriteConstantKey(fieldInfo.mappedName, out)}
2935-
${genWriteVal(getter.asInstanceOf[Expr[ft]], allTypes, fieldInfo.isStringified, None, out)}
2882+
${genWriteVal('v, allTypes, fieldInfo.isStringified, None, out)}
29362883
}
2937-
}
2884+
}
2885+
case None =>
2886+
if (cfg.transientEmpty && fTpe <:< TypeRepr.of[Iterable[?]]) '{
2887+
val v = ${getter.asInstanceOf[Expr[ft & Iterable[?]]]}
2888+
if (!v.isEmpty) {
2889+
${genWriteConstantKey(fieldInfo.mappedName, out)}
2890+
${genWriteVal('v, allTypes, fieldInfo.isStringified, None, out)}
2891+
}
2892+
} else if (cfg.transientEmpty && fTpe <:< TypeRepr.of[Iterator[?]]) '{
2893+
val v = ${getter.asInstanceOf[Expr[ft & Iterator[?]]]}
2894+
if (v.hasNext) {
2895+
${genWriteConstantKey(fieldInfo.mappedName, out)}
2896+
${genWriteVal('v, allTypes, fieldInfo.isStringified, None, out)}
2897+
}
2898+
} else if (cfg.transientNone && isOption(fTpe, types)) {
2899+
val tpe1 = typeArg1(fTpe)
2900+
tpe1.asType match
2901+
case '[tf] => '{
2902+
val v = ${getter.asInstanceOf[Expr[Option[tf]]]}
2903+
if (v ne None) {
2904+
${genWriteConstantKey(fieldInfo.mappedName, out)}
2905+
${genWriteVal('{ v.get }, tpe1 :: allTypes, fieldInfo.isStringified, None, out)}
2906+
}
2907+
}
2908+
} else if (cfg.transientNull && isNullable(fTpe)) '{
2909+
val v = ${getter.asInstanceOf[Expr[ft]]}
2910+
if (v != null) {
2911+
${genWriteConstantKey(fieldInfo.mappedName, out)}
2912+
${genWriteVal('v, allTypes, fieldInfo.isStringified, None, out)}
2913+
}
2914+
} else if (cfg.transientEmpty && fTpe <:< TypeRepr.of[Array[?]]) '{
2915+
val v = ${getter.asInstanceOf[Expr[ft & Array[?]]]}
2916+
if (v.length != 0) {
2917+
${genWriteConstantKey(fieldInfo.mappedName, out)}
2918+
${genWriteVal('v, allTypes, fieldInfo.isStringified, None, out)}
2919+
}
2920+
} else if (cfg.transientEmpty && isIArray(fTpe)) {
2921+
typeArg1(fTpe).asType match
2922+
case '[ft1] => '{
2923+
val v = ${getter.asInstanceOf[Expr[IArray[ft1]]]}
2924+
if (v.length != 0) {
2925+
${genWriteConstantKey(fieldInfo.mappedName, out)}
2926+
${genWriteVal('v, allTypes, fieldInfo.isStringified, None, out)}
2927+
}
2928+
}
2929+
} else '{
2930+
${genWriteConstantKey(fieldInfo.mappedName, out)}
2931+
${genWriteVal(getter.asInstanceOf[Expr[ft]], allTypes, fieldInfo.isStringified, None, out)}
2932+
}
2933+
}
29382934
}).asTerm
29392935
}
29402936
if (optDiscriminator.isDefined) writeFields = optDiscriminator.get.write(out).asTerm :: writeFields

0 commit comments

Comments
 (0)