Skip to content

Commit c0833d7

Browse files
committed
feat: Added Scala Duration to Converter
1 parent d432083 commit c0833d7

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name" : "mongodb-driver",
33
"organization" : "dev.mongocamp",
4-
"version" : "2.6.6.snapshot",
4+
"version" : "2.6.6",
55
"author" : "[email protected]",
66
"license" : "Apache-2.0",
77
"repository" : {

src/main/scala/dev/mongocamp/driver/mongodb/bson/BsonConverter.scala

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ package dev.mongocamp.driver.mongodb.bson
22

33
import org.mongodb.scala.Document
44
import org.mongodb.scala.bson.BsonArray.fromIterable
5-
import org.mongodb.scala.bson.{ ObjectId, _ }
5+
import org.mongodb.scala.bson.{ObjectId, _}
66

77
import java.math.BigInteger
8-
import java.time.{ LocalDate, LocalDateTime, ZoneId }
8+
import java.time.{LocalDate, LocalDateTime, ZoneId}
99
import java.util.Date
1010
import scala.collection.mutable
11+
import scala.concurrent.duration.Duration
1112
import scala.jdk.CollectionConverters._
1213
import scala.util.matching.Regex
1314

@@ -22,7 +23,7 @@ object BsonConverter {
2223

2324
def lastKeyFromRelation(key: String): String = key.substring(key.lastIndexOf(DocumentKeyDivider) + 1)
2425

25-
def documentValueOption(document: Document, key: String): Option[Any] =
26+
def documentValueOption(document: Document, key: String): Option[Any] = {
2627
if (hasRelation(key)) {
2728
val newKey = newKeyFromRelation(key)
2829
val relation = relationKey(key)
@@ -36,13 +37,17 @@ object BsonConverter {
3637
None
3738
}
3839
}
39-
else
40+
else {
4041
None
42+
}
4143
}
42-
else if (document.contains(key))
44+
else if (document.contains(key)) {
4345
Some(fromBson(document(key)))
44-
else
46+
}
47+
else {
4548
None
49+
}
50+
}
4651

4752
def updateDocumentValue(document: Document, key: String, value: Any): Document = {
4853
val doc = org.mongodb.scala.bson.collection.mutable.Document(document.toJson())
@@ -110,6 +115,7 @@ object BsonConverter {
110115
case bytes: Array[Byte] => BsonBinary(bytes)
111116
case r: Regex => BsonRegularExpression(r)
112117
case d: Date => BsonDateTime(d)
118+
case d: Duration => BsonString(d.toString)
113119
case ld: LocalDate =>
114120
BsonDateTime(Date.from(ld.atStartOfDay(ZoneId.systemDefault()).toInstant))
115121
case ldt: LocalDateTime =>

0 commit comments

Comments
 (0)