
com.avsystem.commons.mongo.Doc.scala Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of commons-mongo_2.11 Show documentation
Show all versions of commons-mongo_2.11 Show documentation
AVSystem commons library for Scala
The newest version!
package com.avsystem.commons
package mongo
import org.bson.{BsonDocument, BsonValue}
/**
* @author MKej
*/
class Doc(private val doc: BsonDocument) extends AnyVal {
def get[A, BSON <: BsonValue](key: DocKey[A, BSON]): Option[A] =
Option(doc.get(key.key).asInstanceOf[BSON]).map(key.codec.fromBson)
def require[A](key: DocKey[A, _ <: BsonValue]): A = get(key).get
def put[A](key: DocKey[A, _ <: BsonValue], value: A): Doc = {
doc.put(key.key, key.codec.toBson(value))
this
}
def putOpt[A](key: DocKey[A, _ <: BsonValue], optValue: Option[A]): Doc = optValue match {
case Some(value) => put(key, value)
case _ => this
}
def toBson: BsonDocument = doc
}
object Doc {
def apply(): Doc = new Doc(new BsonDocument())
def apply[A](key: DocKey[A, _ <: BsonValue], value: A): Doc = apply().put(key, value)
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy