Skip to content
This repository was archived by the owner on Jul 16, 2024. It is now read-only.

Commit feb4fb9

Browse files
committed
Support BsonProperty for ADT
Only supports non conflicting names across the classes in the ADT SCALA-485
1 parent ee37f8c commit feb4fb9

File tree

3 files changed

+9
-6
lines changed

3 files changed

+9
-6
lines changed

bson/src/main/scala/org/mongodb/scala/bson/codecs/macrocodecs/CaseClassCodec.scala

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,11 @@ private[codecs] object CaseClassCodec {
8686
if (isSealed(mainType) && subClasses.isEmpty) {
8787
c.abort(c.enclosingPosition, s"No known subclasses of the sealed ${if (mainType.typeSymbol.asClass.isTrait) "trait" else "class"}")
8888
}
89-
val knownTypes = (mainType +: subClasses).filterNot(_.typeSymbol.isAbstract).reverse
89+
val knownTypes: List[Type] = (mainType +: subClasses).filterNot(_.typeSymbol.isAbstract).reverse
9090

91-
val terms = {
92-
if (!isAbstractSealed(mainType)) {
93-
val constructor = mainType.decl(termNames.CONSTRUCTOR)
91+
def createTerms(t: Type): List[TermSymbol] = {
92+
if (!isAbstractSealed(t)) {
93+
val constructor = t.decl(termNames.CONSTRUCTOR)
9494
if (!constructor.isMethod) c.abort(c.enclosingPosition, "No constructor, unsupported class type")
9595
constructor.asMethod.paramLists match {
9696
case h :: _ => h.map(_.asTerm)
@@ -101,6 +101,8 @@ private[codecs] object CaseClassCodec {
101101
}
102102
}
103103

104+
val terms = knownTypes.flatMap(t => createTerms(t))
105+
104106
val fields: Map[Type, List[(TermName, Type)]] = {
105107
knownTypes.map(
106108
t => (

bson/src/test/scala/org/mongodb/scala/bson/codecs/MacrosSpec.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class MacrosSpec extends FlatSpec with Matchers {
8787
case class OptionalRecursive(name: String, value: Option[OptionalRecursive])
8888

8989
sealed class Tree
90-
case class Branch(b1: Tree, b2: Tree, value: Int) extends Tree
90+
case class Branch(@BsonProperty("l1") b1: Tree, @BsonProperty("r1") b2: Tree, value: Int) extends Tree
9191
case class Leaf(value: Int) extends Tree
9292

9393
case class ContainsADT(name: String, tree: Tree)
@@ -529,7 +529,7 @@ class MacrosSpec extends FlatSpec with Matchers {
529529
def createTreeJson(tree: Tree): String = {
530530
tree match {
531531
case l: Leaf => s"""{_t: "Leaf", value: ${l.value}}"""
532-
case b: Branch => s"""{_t: "Branch", b1: ${createTreeJson(b.b1)}, b2: ${createTreeJson(b.b2)}, value: ${b.value}}"""
532+
case b: Branch => s"""{_t: "Branch", l1: ${createTreeJson(b.b1)}, r1: ${createTreeJson(b.b2)}, value: ${b.value}}"""
533533
case _ => "{}"
534534
}
535535
}

docs/reference/content/changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Changes between released versions
2121
* Added batchSize support for MongoIterable based Observables [SCALA-552](https://jira.mongodb.org/browse/SCALA-552)
2222
* Added support for sealed traits [SCALA-554](https://jira.mongodb.org/browse/SCALA-554)
2323
* Added caseclass support for Sets, Vectors and Streams [SCALA-346](https://jira.mongodb.org/browse/SCALA-346)
24+
* Improved support for BsonPropery annotations in ADTs [SCALA-485](https://jira.mongodb.org/browse/SCALA-485)
2425
* Deprecated BsonArray.apply(Iterable[BsonValue]) added BsonArray.fromIterable [SCALA-531](https://jira.mongodb.org/browse/SCALA-531)
2526
* Fix `UninitializedFieldError` in MacroCodecs under `-Xcheckinit` [SCALA-542](https://jira.mongodb.org/browse/SCALA-542)
2627

0 commit comments

Comments
 (0)