@@ -493,6 +493,7 @@ class CirceProtocolGenerator private (circeVersion: CirceModelGenerator, applyVa
493
493
}
494
494
495
495
private [this ] def renderIntermediate (
496
+ clsName : NonEmptyList [String ],
496
497
model : Tracker [Schema [_]],
497
498
dtoName : String ,
498
499
concreteTypes : List [PropMeta [ScalaLanguage ]],
@@ -510,12 +511,12 @@ class CirceProtocolGenerator private (circeVersion: CirceModelGenerator, applyVa
510
511
): Target [(PropMeta [ScalaLanguage ], (Option [Defn .Val ], Option [Defn .Val ], Defn .Class ))] =
511
512
for {
512
513
prefixes <- Cl .vendorPrefixes()
513
- customTpe = CustomTypeName (model, prefixes).getOrElse(dtoName)
514
- tpe <- Sc .pureTypeName(customTpe)
514
+ customTpe = NonEmptyList .of( CustomTypeName (model, prefixes).getOrElse(dtoName) )
515
+ tpe <- Sc .pureTypeName(customTpe.last )
515
516
props <- extractProperties(model)
516
517
requiredFields = getRequiredFieldsRec(model)
517
518
(params, nestedDefinitions) <- prepareProperties(
518
- NonEmptyList .of( customTpe) ,
519
+ customTpe,
519
520
propertyToTypeLookup = Map .empty,
520
521
props,
521
522
requiredFields,
@@ -526,10 +527,10 @@ class CirceProtocolGenerator private (circeVersion: CirceModelGenerator, applyVa
526
527
defaultPropertyRequirement,
527
528
components
528
529
)
529
- encoder <- encodeModel(customTpe, dtoPackage, params, parents = List .empty)
530
- decoder <- decodeModel(customTpe, dtoPackage, supportPackage, params, parents = List .empty)
531
- defn <- renderDTOClass(customTpe, supportPackage, params, parents = List .empty)
532
- } yield (PropMeta [ScalaLanguage ](customTpe, tpe), (encoder, decoder, defn))
530
+ encoder <- encodeModel(clsName ::: customTpe, dtoPackage, params, parents = List .empty)
531
+ decoder <- decodeModel(clsName ::: customTpe, dtoPackage, supportPackage, params, parents = List .empty)
532
+ defn <- renderDTOClass(customTpe.last , supportPackage, params, parents = List .empty)
533
+ } yield (PropMeta [ScalaLanguage ](customTpe.last , tpe), (encoder, decoder, defn))
533
534
534
535
private [this ] def fromModel (
535
536
clsName : NonEmptyList [String ],
@@ -565,8 +566,8 @@ class CirceProtocolGenerator private (circeVersion: CirceModelGenerator, applyVa
565
566
defaultPropertyRequirement,
566
567
components
567
568
)
568
- encoder <- encodeModel(clsName.last , dtoPackage, params, parents)
569
- decoder <- decodeModel(clsName.last , dtoPackage, supportPackage, params, parents)
569
+ encoder <- encodeModel(clsName, dtoPackage, params, parents)
570
+ decoder <- decodeModel(clsName, dtoPackage, supportPackage, params, parents)
570
571
tpe <- parseTypeName(clsName.last)
571
572
fullType <- selectType(dtoPackage.foldRight(clsName)((x, xs) => xs.prepend(x)))
572
573
nestedClasses <- nestedDefinitions.flatTraverse {
@@ -652,6 +653,7 @@ class CirceProtocolGenerator private (circeVersion: CirceModelGenerator, applyVa
652
653
case other => Target .raiseUserError(s " Unexpected type ${other}" )
653
654
}
654
655
(pm, defns) <- renderIntermediate(
656
+ clsName,
655
657
model,
656
658
dtoName,
657
659
concreteTypes,
@@ -867,7 +869,7 @@ class CirceProtocolGenerator private (circeVersion: CirceModelGenerator, applyVa
867
869
paramsAndNestedDefinitions <- props.traverse[Target , (Tracker [ProtocolParameter [ScalaLanguage ]], Option [NestedProtocolElems [ScalaLanguage ]])] {
868
870
case (name, schema) =>
869
871
for {
870
- typeName <- formatTypeName(name).map(formattedName => getClsName(name).append(formattedName))
872
+ typeName <- formatTypeName(name).map(formattedName => getClsName(name).prependList(dtoPackage). append(formattedName))
871
873
tpe <- selectType(typeName)
872
874
maybeNestedDefinition <- processProperty(name, schema)
873
875
resolvedType <- ModelResolver .propMetaWithName[ScalaLanguage , Target ](() => Target .pure(tpe), schema, components)
@@ -1407,19 +1409,21 @@ class CirceProtocolGenerator private (circeVersion: CirceModelGenerator, applyVa
1407
1409
} yield names.flatMap(n => reduced.get(n))
1408
1410
1409
1411
private def encodeModel (
1410
- clsName : String ,
1412
+ clsName : NonEmptyList [ String ] ,
1411
1413
dtoPackage : List [String ],
1412
1414
selfParams : List [ProtocolParameter [ScalaLanguage ]],
1413
1415
parents : List [SuperClass [ScalaLanguage ]] = Nil
1414
- ) =
1416
+ )(implicit Lt : LanguageTerms [ScalaLanguage , Target ]) = {
1417
+ import Lt ._
1415
1418
for {
1416
1419
() <- Target .pure(())
1417
1420
discriminators = parents.flatMap(_.discriminators)
1418
1421
discriminatorNames = discriminators.map(_.propertyName).toSet
1419
- allParams <- finalizeParams(parents.reverse.flatMap(_.params) ++ selfParams)
1422
+ allParams <- finalizeParams(parents.reverse.flatMap(_.params) ++ selfParams)
1423
+ qualifiedClsType <- selectType(clsName.prependList(dtoPackage))
1420
1424
(discriminatorParams, params) = allParams.partition(param => discriminatorNames.contains(param.name.value))
1421
1425
readOnlyKeys : List [String ] = params.flatMap(_.readOnlyKey).toList
1422
- typeName = Type . Name (clsName)
1426
+
1423
1427
encVal = {
1424
1428
def encodeStatic (param : ProtocolParameter [ScalaLanguage ], clsName : String ) =
1425
1429
q """ ( ${Lit .String (param.name.value)}, _root_.io.circe.Json.fromString( ${Lit .String (clsName)})) """
@@ -1448,14 +1452,14 @@ class CirceProtocolGenerator private (circeVersion: CirceModelGenerator, applyVa
1448
1452
}
1449
1453
}
1450
1454
1451
- val pairsWithStatic = pairs ++ discriminatorParams.map(encodeStatic(_, clsName))
1455
+ val pairsWithStatic = pairs ++ discriminatorParams.map(encodeStatic(_, clsName.last ))
1452
1456
val simpleCase = q " _root_.scala.Vector(.. ${pairsWithStatic}) "
1453
1457
val allFields = optional.foldLeft[Term ](simpleCase) { (acc, field) =>
1454
1458
q " $acc ++ $field"
1455
1459
}
1456
1460
1457
1461
q """
1458
- ${circeVersion.encoderObjectCompanion}.instance[ ${Type . Name (clsName) }](a => _root_.io.circe.JsonObject.fromIterable( $allFields))
1462
+ ${circeVersion.encoderObjectCompanion}.instance[ ${qualifiedClsType }](a => _root_.io.circe.JsonObject.fromIterable( $allFields))
1459
1463
"""
1460
1464
}
1461
1465
(readOnlyDefn, readOnlyFilter) = NonEmptyList .fromList(readOnlyKeys).fold((List .empty[Stat ], identity[Term ] _)) { roKeys =>
@@ -1466,24 +1470,28 @@ class CirceProtocolGenerator private (circeVersion: CirceModelGenerator, applyVa
1466
1470
}
1467
1471
1468
1472
} yield Option (q """
1469
- implicit val ${suffixClsName(" encode" , clsName)}: ${circeVersion.encoderObject}[ ${Type . Name (clsName) }] = {
1473
+ implicit val ${suffixClsName(" encode" , clsName.last )}: ${circeVersion.encoderObject}[ ${qualifiedClsType }] = {
1470
1474
.. ${readOnlyDefn};
1471
1475
${readOnlyFilter(encVal)}
1472
1476
}
1473
1477
""" )
1478
+ }
1474
1479
1475
1480
private def decodeModel (
1476
- clsName : String ,
1481
+ clsName : NonEmptyList [ String ] ,
1477
1482
dtoPackage : List [String ],
1478
1483
supportPackage : List [String ],
1479
1484
selfParams : List [ProtocolParameter [ScalaLanguage ]],
1480
1485
parents : List [SuperClass [ScalaLanguage ]] = Nil
1481
1486
)(implicit Lt : LanguageTerms [ScalaLanguage , Target ]): Target [Option [Defn .Val ]] = {
1487
+ import Lt ._
1482
1488
for {
1483
1489
() <- Target .pure(())
1484
1490
discriminators = parents.flatMap(_.discriminators)
1485
1491
discriminatorNames = discriminators.map(_.propertyName).toSet
1486
- allParams <- finalizeParams(parents.reverse.flatMap(_.params) ++ selfParams)
1492
+ allParams <- finalizeParams(parents.reverse.flatMap(_.params) ++ selfParams)
1493
+ qualifiedClsType <- selectType(clsName.prependList(dtoPackage))
1494
+ qualifiedClsTerm <- selectTerm(clsName.prependList(dtoPackage))
1487
1495
params = allParams.filterNot(param => discriminatorNames.contains(param.name.value))
1488
1496
needsEmptyToNull : Boolean = params.exists(_.emptyToNull == EmptyIsNull )
1489
1497
paramCount = params.length
@@ -1492,9 +1500,9 @@ class CirceProtocolGenerator private (circeVersion: CirceModelGenerator, applyVa
1492
1500
if (paramCount == 0 ) {
1493
1501
Target .pure(
1494
1502
q """
1495
- new _root_.io.circe.Decoder[ ${Type . Name (clsName) }] {
1496
- final def apply(c: _root_.io.circe.HCursor): _root_.io.circe.Decoder.Result[ ${Type . Name (clsName) }] =
1497
- _root_.scala.Right( ${Term . Name (clsName) }())
1503
+ new _root_.io.circe.Decoder[ ${qualifiedClsType }] {
1504
+ final def apply(c: _root_.io.circe.HCursor): _root_.io.circe.Decoder.Result[ ${qualifiedClsType }] =
1505
+ _root_.scala.Right( ${qualifiedClsTerm }())
1498
1506
}
1499
1507
"""
1500
1508
)
@@ -1569,17 +1577,17 @@ class CirceProtocolGenerator private (circeVersion: CirceModelGenerator, applyVa
1569
1577
.map { pairs =>
1570
1578
val (terms, enumerators) = pairs.unzip
1571
1579
q """
1572
- new _root_.io.circe.Decoder[ ${Type . Name (clsName) }] {
1573
- final def apply(c: _root_.io.circe.HCursor): _root_.io.circe.Decoder.Result[ ${Type . Name (clsName) }] =
1580
+ new _root_.io.circe.Decoder[ ${qualifiedClsType }] {
1581
+ final def apply(c: _root_.io.circe.HCursor): _root_.io.circe.Decoder.Result[ ${qualifiedClsType }] =
1574
1582
for {
1575
1583
.. ${enumerators}
1576
- } yield ${Term . Name (clsName) }(.. ${terms})
1584
+ } yield ${qualifiedClsTerm }(.. ${terms})
1577
1585
}
1578
1586
"""
1579
1587
}
1580
1588
}
1581
1589
} yield Option (q """
1582
- implicit val ${suffixClsName(" decode" , clsName)}: _root_.io.circe.Decoder[ ${Type . Name (clsName) }] = $decVal
1590
+ implicit val ${suffixClsName(" decode" , clsName.last )}: _root_.io.circe.Decoder[ ${qualifiedClsType }] = $decVal
1583
1591
""" )
1584
1592
}
1585
1593
0 commit comments