@@ -928,7 +928,7 @@ private class JsonCodecMakerInstance(cfg: CodecMakerConfig)(using Quotes) {
928
928
929
929
@ tailrec
930
930
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)
932
932
case _ => tpe
933
933
934
934
private def isSealedClass (tpe : TypeRepr ): Boolean = tpe.typeSymbol.flags.is(Flags .Sealed )
@@ -937,7 +937,7 @@ private class JsonCodecMakerInstance(cfg: CodecMakerConfig)(using Quotes) {
937
937
isSealedClass(tpe) || tpe.baseClasses.exists(_.flags.is(Flags .Sealed ))
938
938
939
939
private def isConstType (tpe : TypeRepr ): Boolean = tpe match
940
- case ConstantType (_) => true
940
+ case _ : ConstantType => true
941
941
case _ => false
942
942
943
943
private def isEnumValue (tpe : TypeRepr ): Boolean = tpe.termSymbol.flags.is(Flags .Enum )
@@ -1126,10 +1126,9 @@ private class JsonCodecMakerInstance(cfg: CodecMakerConfig)(using Quotes) {
1126
1126
val primaryConstructor = tpeClassSym.primaryConstructor
1127
1127
var annotations = Map .empty[String , FieldAnnotations ]
1128
1128
val caseFields = tpeClassSym.caseFields
1129
- var companionClassMethodMembers : List [Symbol ] = null
1129
+ var companionRefAndMembers : ( Ref , List [Symbol ]) = null
1130
1130
var fieldMembers : List [Symbol ] = null
1131
1131
var methodMembers : List [Symbol ] = null
1132
- val companionModuleRef = Ref (tpe.typeSymbol.companionModule)
1133
1132
1134
1133
tpeClassSym.fieldMembers.foreach {
1135
1134
case m : Symbol if hasSupportedAnnotation(m) =>
@@ -1157,38 +1156,36 @@ private class JsonCodecMakerInstance(cfg: CodecMakerConfig)(using Quotes) {
1157
1156
var fieldTpe = tpe.memberType(symbol).dealias
1158
1157
if (tpeTypeArgs ne Nil ) fieldTpe = fieldTpe.substituteTypes(typeParams, tpeTypeArgs)
1159
1158
fieldTpe match
1160
- case TypeLambda (_, _, _) =>
1159
+ case _ : TypeLambda =>
1161
1160
fail(s " Type lambdas are not supported for type ' ${tpe.show}' with field type for $name ' ${fieldTpe.show}' " )
1162
- case TypeBounds (_, _) =>
1161
+ case _ : TypeBounds =>
1163
1162
fail(s " Type bounds are not supported for type ' ${tpe.show}' with field type for $name ' ${fieldTpe.show}' " )
1164
1163
case _ =>
1165
1164
val defaultValue = if (! cfg.requireDefaultFields && symbol.flags.is(Flags .HasDefault )) {
1166
1165
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)
1169
1169
}
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)
1172
1172
methodSymbol.paramSymss match
1173
1173
case Nil => dvSelectNoTypes
1174
1174
case List (params) if params.exists(_.isTypeParam) => TypeApply (dvSelectNoTypes, tpeTypeArgs.map(Inferred (_)))
1175
1175
case paramss => fail(s " Default method for $name of class ${tpe.show} have a complex parameter list: $paramss" )
1176
1176
}
1177
1177
} else None
1178
- val getterOrField = caseFields.find(_.name == name) match {
1178
+ val getterOrField = caseFields.find(_.name == name) match
1179
1179
case Some (caseField) => caseField
1180
1180
case _ =>
1181
1181
if (fieldMembers eq null ) fieldMembers = tpeClassSym.fieldMembers
1182
- fieldMembers.find(_.name == name) match {
1182
+ fieldMembers.find(_.name == name) match
1183
1183
case Some (fieldMember) => fieldMember
1184
1184
case _ =>
1185
1185
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
1187
1187
case Some (methodMember) => methodMember
1188
1188
case _ => Symbol .noSymbol
1189
- }
1190
- }
1191
- }
1192
1189
if (! getterOrField.exists || getterOrField.flags.is(Flags .PrivateLocal )) {
1193
1190
fail(s " Getter or field ' $name' of ' ${tpe.show}' is private. It should be defined as 'val' or 'var' in the primary constructor. " )
1194
1191
}
@@ -1252,7 +1249,7 @@ private class JsonCodecMakerInstance(cfg: CodecMakerConfig)(using Quotes) {
1252
1249
val nudeSubtype = sym.typeRef
1253
1250
val tpeArgsFromChild = typeArgs(nudeSubtype.baseType(typeSymbol))
1254
1251
nudeSubtype.memberType(sym.primaryConstructor) match
1255
- case MethodType (_, _, _) => nudeSubtype
1252
+ case _ : MethodType => nudeSubtype
1256
1253
case PolyType (names, _, resPolyTp) =>
1257
1254
val tpBinding = resolveParentTypeArgs(sym, tpeArgsFromChild, typeArgs(tpe), Map .empty)
1258
1255
val ctArgs = names.map { name =>
@@ -1283,7 +1280,7 @@ private class JsonCodecMakerInstance(cfg: CodecMakerConfig)(using Quotes) {
1283
1280
fail(" 'AnyVal' and one value classes with 'CodecMakerConfig.withInlineOneValueClasses(true)' are not " +
1284
1281
s " supported as leaf classes for ADT with base ' ${adtBaseTpe.show}'. " )
1285
1282
} 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 )) {
1287
1284
" Only sealed intermediate traits or abstract classes are supported."
1288
1285
} else {
1289
1286
" 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) {
2372
2369
val tpe1 = typeArg1(tpe)
2373
2370
val types1 = tpe1 :: types
2374
2371
val newArrayOnChange = tpe1 match
2375
- case AppliedType (_, _) => true
2372
+ case _ : AppliedType => true
2376
2373
case _ => isValueClass(tpe1) || isOpaque(tpe1)
2377
2374
tpe1.asType match
2378
2375
case ' [t1] =>
@@ -2818,123 +2815,122 @@ private class JsonCodecMakerInstance(cfg: CodecMakerConfig)(using Quotes) {
2818
2815
} else Select .unique(valRef, s " _ ${i + 1 }" )
2819
2816
case _ => Select (valRef, fieldInfo.getterOrField)
2820
2817
}.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]]}) {
2862
2839
$ {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)}
2881
2841
}
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)}
2887
2842
}
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)}
2888
2848
}
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
+ ' {
2919
2856
val v = $ {getter.asInstanceOf [Expr [ft & Array [? ]]]}
2920
- if (v.length != 0 ) {
2857
+ if ($ {cond( ' v )} ) {
2921
2858
$ {genWriteConstantKey(fieldInfo.mappedName, out)}
2922
2859
$ {genWriteVal(' v , allTypes, fieldInfo.isStringified, None , out)}
2923
2860
}
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
+ ' {
2927
2871
val v = $ {getter.asInstanceOf [Expr [IArray [ft1]]]}
2928
- if (v.length != 0 ) {
2872
+ if ($ {cond( ' v )} ) {
2929
2873
$ {genWriteConstantKey(fieldInfo.mappedName, out)}
2930
2874
$ {genWriteVal(' v , allTypes, fieldInfo.isStringified, None , out)}
2931
2875
}
2932
2876
}
2933
- } else ' {
2877
+ }
2878
+ } else ' {
2879
+ val v = $ {getter.asInstanceOf [Expr [ft]]}
2880
+ if (v != $ {d.asExpr.asInstanceOf [Expr [ft]]}) {
2934
2881
$ {genWriteConstantKey(fieldInfo.mappedName, out)}
2935
- $ {genWriteVal(getter. asInstanceOf [ Expr [ft]] , allTypes, fieldInfo.isStringified, None , out)}
2882
+ $ {genWriteVal(' v , allTypes, fieldInfo.isStringified, None , out)}
2936
2883
}
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
+ }
2938
2934
}).asTerm
2939
2935
}
2940
2936
if (optDiscriminator.isDefined) writeFields = optDiscriminator.get.write(out).asTerm :: writeFields
0 commit comments