Skip to content

Commit

Permalink
feat: Added Scala Duration to Converter
Browse files Browse the repository at this point in the history
  • Loading branch information
QuadStingray committed Nov 2, 2023
1 parent d432083 commit c0833d7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name" : "mongodb-driver",
"organization" : "dev.mongocamp",
"version" : "2.6.6.snapshot",
"version" : "2.6.6",
"author" : "[email protected]",
"license" : "Apache-2.0",
"repository" : {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package dev.mongocamp.driver.mongodb.bson

import org.mongodb.scala.Document
import org.mongodb.scala.bson.BsonArray.fromIterable
import org.mongodb.scala.bson.{ ObjectId, _ }
import org.mongodb.scala.bson.{ObjectId, _}

import java.math.BigInteger
import java.time.{ LocalDate, LocalDateTime, ZoneId }
import java.time.{LocalDate, LocalDateTime, ZoneId}
import java.util.Date
import scala.collection.mutable
import scala.concurrent.duration.Duration
import scala.jdk.CollectionConverters._
import scala.util.matching.Regex

Expand All @@ -22,7 +23,7 @@ object BsonConverter {

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

def documentValueOption(document: Document, key: String): Option[Any] =
def documentValueOption(document: Document, key: String): Option[Any] = {
if (hasRelation(key)) {
val newKey = newKeyFromRelation(key)
val relation = relationKey(key)
Expand All @@ -36,13 +37,17 @@ object BsonConverter {
None
}
}
else
else {
None
}
}
else if (document.contains(key))
else if (document.contains(key)) {
Some(fromBson(document(key)))
else
}
else {
None
}
}

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

0 comments on commit c0833d7

Please sign in to comment.