@@ -887,35 +887,37 @@ object JsonCodecMaker {
887
887
def getClassInfo (tpe : TypeRepr ): ClassInfo = classInfos.getOrElseUpdate(tpe, {
888
888
case class FieldAnnotations (partiallyMappedName : String , transient : Boolean , stringified : Boolean )
889
889
890
- def getPrimaryConstructor (tpe : TypeRepr ): Symbol = tpe.classSymbol match
891
- case Some (sym) if sym.primaryConstructor.exists => sym.primaryConstructor
892
- case _ => fail(s " Cannot find a primary constructor for ' $tpe' " )
893
-
894
- def hasSupportedAnnotation (m : Symbol ): Boolean =
895
- m.annotations.exists(a => a.tpe =:= TypeRepr .of[named] || a.tpe =:= TypeRepr .of[transient] ||
896
- a.tpe =:= TypeRepr .of[stringified] || (cfg.scalaTransientSupport && a.tpe =:= TypeRepr .of[scala.transient]))
890
+ def hasSupportedAnnotation (m : Symbol ): Boolean = m.annotations.exists { a =>
891
+ val tpe = a.tpe
892
+ tpe =:= TypeRepr .of[named] || tpe =:= TypeRepr .of[transient] || tpe =:= TypeRepr .of[stringified] ||
893
+ (cfg.scalaTransientSupport && tpe =:= TypeRepr .of[scala.transient])
894
+ }
897
895
898
896
def supportedTransientTypeNames : String =
899
897
if (cfg.scalaTransientSupport) s " ' ${Type .show[transient]}' (or ' ${Type .show[scala.transient]}') "
900
898
else s " ' ${Type .show[transient]}') "
901
899
902
900
val tpeClassSym = tpe.classSymbol.getOrElse(fail(s " Expected that ${tpe.show} has classSymbol " ))
903
- val annotations = tpeClassSym.fieldMembers.collect { case m : Symbol if hasSupportedAnnotation(m) =>
904
- val name = m.name
905
- val named = m.annotations.filter(_.tpe =:= TypeRepr .of[named])
906
- if (named.size > 1 ) fail(s " Duplicated ' ${TypeRepr .of[named].show}' defined for ' $name' of ' ${tpe.show}'. " )
907
- val trans = m.annotations.filter(a => a.tpe =:= TypeRepr .of[transient] ||
908
- (cfg.scalaTransientSupport && a.tpe =:= TypeRepr .of[scala.transient]))
909
- if (trans.size > 1 ) warn(s " Duplicated $supportedTransientTypeNames defined for ' $name' of ' ${tpe.show}'. " )
910
- val strings = m.annotations.filter(_.tpe =:= TypeRepr .of[stringified])
911
- if (strings.size > 1 ) warn(s " Duplicated ' ${TypeRepr .of[stringified].show}' defined for ' $name' of ' ${tpe.show}'. " )
912
- if ((named.nonEmpty || strings.nonEmpty) && trans.nonEmpty)
913
- warn(s " Both $supportedTransientTypeNames and ' ${Type .show[named]}' or " +
914
- s " $supportedTransientTypeNames and ' ${Type .show[stringified]}' defined for ' $name' of ' ${tpe.show}'. " )
915
- val partiallyMappedName = namedValueOpt(named.headOption, tpe).getOrElse(name)
916
- (name, FieldAnnotations (partiallyMappedName, trans.nonEmpty, strings.nonEmpty))
917
- }.toMap
918
- val primaryConstructor = getPrimaryConstructor(tpe)
901
+ var annotations = Map .empty[String , FieldAnnotations ]
902
+ tpeClassSym.fieldMembers.foreach {
903
+ case m : Symbol if hasSupportedAnnotation(m) =>
904
+ val name = m.name
905
+ val named = m.annotations.count(_.tpe =:= TypeRepr .of[named])
906
+ if (named > 1 ) fail(s " Duplicated ' ${TypeRepr .of[named].show}' defined for ' $name' of ' ${tpe.show}'. " )
907
+ val trans = m.annotations.count(a => a.tpe =:= TypeRepr .of[transient] ||
908
+ (cfg.scalaTransientSupport && a.tpe =:= TypeRepr .of[scala.transient]))
909
+ if (trans > 1 ) warn(s " Duplicated $supportedTransientTypeNames defined for ' $name' of ' ${tpe.show}'. " )
910
+ val strings = m.annotations.count(_.tpe =:= TypeRepr .of[stringified])
911
+ if (strings > 1 ) warn(s " Duplicated ' ${TypeRepr .of[stringified].show}' defined for ' $name' of ' ${tpe.show}'. " )
912
+ if ((named > 0 || strings > 0 ) && trans > 0 )
913
+ warn(s " Both $supportedTransientTypeNames and ' ${Type .show[named]}' or " +
914
+ s " $supportedTransientTypeNames and ' ${Type .show[stringified]}' defined for ' $name' of ' ${tpe.show}'. " )
915
+ val partiallyMappedName = namedValueOpt(m.annotations.find(_.tpe =:= TypeRepr .of[named]), tpe).getOrElse(name)
916
+ annotations = annotations.updated(name, FieldAnnotations (partiallyMappedName, trans > 0 , strings > 0 ))
917
+ case _ =>
918
+ }
919
+ val primaryConstructor = tpeClassSym.primaryConstructor
920
+ if (! primaryConstructor.exists) fail(s " Cannot find a primary constructor for ' $tpe' " )
919
921
920
922
def createFieldInfos (params : List [Symbol ], typeParams : List [Symbol ],
921
923
fieldIndex : Boolean => Int ): List [FieldInfo ] = params.map {
@@ -978,7 +980,7 @@ object JsonCodecMaker {
978
980
s " is ${fieldType.show}" )
979
981
}
980
982
FieldInfo (symbol, mappedName, getterOrField, defaultValue, fieldType, isTransient, isStringified, fieldIndex(isTransient))
981
- }.toList
983
+ }
982
984
983
985
val fieldIndex : Boolean => Int = {
984
986
var i = - 1
@@ -1032,8 +1034,7 @@ object JsonCodecMaker {
1032
1034
nudeSubtype.memberType(sym.primaryConstructor) match
1033
1035
case MethodType (_, _, _) => nudeSubtype
1034
1036
case PolyType (names, bounds, resPolyTp) =>
1035
- val targs = typeArgs(tpe)
1036
- val tpBinding = resolveParentTypeArgs(sym, tpeArgsFromChild, targs, Map .empty)
1037
+ val tpBinding = resolveParentTypeArgs(sym, tpeArgsFromChild, typeArgs(tpe), Map .empty)
1037
1038
val ctArgs = names.map { name =>
1038
1039
tpBinding.getOrElse(name, fail(s " Type parameter $name of $sym can't be deduced from " +
1039
1040
s " type arguments of ${tpe.show}. Please provide a custom implicitly accessible codec for it. " ))
0 commit comments