medeia is a BSON library for Scala.
The goal of medeia is to make bson decoding / encoding as easy and fun as JSON decoding / encoding is with circe or argonaut.
medeia includes decoders and encoders for common data structures as well as automatic derivation of decoders and encoders for case classes using shapeless.
libraryDependencies += "de.megaera" %% "medeia" % "0.14.0"
Currently no complete documentation is present, additional examples(including Scala 3 derives syntax) can be found in the test suites. If you have questions: don't hesitate to ask via github issues.
import org.mongodb.scala.bson._
import medeia.syntax._
val stringList = List("a", "b")
val encoded = stringList.toBson
// BsonArray{values=[BsonString{value='a'}, BsonString{value='b'}]}
val bsonArray = BsonArray("a", "b")
val decoded = bsonArray.fromBson[List[String]]
// Right(List(a,b)
import org.mongodb.scala.bson._
import medeia.encoder.BsonEncoder
import medeia.decoder.BsonDecoder
import medeia.codec._
import medeia.syntax._
case class Simple(int: Int, string: Option[String])
implicit val simpleEncoder: BsonEncoder[Simple] = BsonDocumentEncoder.derived
val encoded = Simple(1, Some("a")).toBson
// {"string": "a", "int": 1}
implicit val simpleDecoder: BsonDecoder[Simple] = BsonDocumentDecoder.derived
val doc = BsonDocument("int" -> 1, "string" -> "string")
val decoded = doc.fromBson[Simple]
// Right(Simple(1,Some(string)))
sealed trait Trait
case class Foo(answer: Int) extends Trait
case class Bar(bar: String) extends Trait
implicit val fooCodec: BsonDocumentCodec[Foo] = BsonDocumentCodec.derived
implicit val barCodec: BsonDocumentCodec[Bar] = BsonDocumentCodec.derived
implicit val traitCodec: BsonDocumentCodec[Trait] = BsonDocumentCodec.derived
val encoded = Foo(42).toBson
// {"answer": 42, "type": "Foo"}
A transformation function for keynames can be provided as follows:
import medeia.generic.GenericDerivationOptions
import medeia.encoder.BsonEncoder
import medeia.syntax._
case class Simple(fieldInScala: Int)
implicit val genericDerivationOptions: GenericDerivationOptions[Simple] =
GenericDerivationOptions { case "fieldInScala" => "fieldInBson" }
implicit val simpleEncoder: BsonDocumentEncoder[Simple] = BsonDocumentEncoder.derived
val encoded = Simple(1).toBson
// {"fieldInBson": 1}
GenericDerivationOptions works for encoding and decoding. If the provided partial function is not defined for a key no transformation is used.
A separate module exists for encoding and decoding enumeratum enums:
libraryDependencies += "de.megaera" %% "medeia-enumeratum" % "0.14.0"
import medeia.syntax._
import medeia.enumeratum.Enumeratum
import enumeratum.Enum
import enumeratum.EnumEntry
import scala.collection.immutable
sealed abstract class TestEnum(override val entryName: String) extends EnumEntry
object TestEnum extends Enum[TestEnum] {
override val values: immutable.IndexedSeq[TestEnum] = findValues
case object A extends TestEnum("A")
case object B extends TestEnum("B")
case object C extends TestEnum("C")
implicit val codec: BsonCodec[TestEnum] = Enumeratum.codec(TestEnum)
}
TestEnum.A.toBson
// "A"
BsonEncoder/BsonDecoder for eu.timepit:refined
can be found in the medeia-refined
module
libraryDependencies += "de.megaera" %% "medeia-refined" % "0.14.0"
import eu.timepit.refined.api.Refined
import eu.timepit.refined.auto._
import eu.timepit.refined.collection._
import medeia.syntax._
import medeia.refined._
val refinedString: String Refined NonEmpty = "test"
refinedString.toBson
// BsonString{value='test'}
medeia is licensed under the Apache License, Version 2.0 (the “License”); you may not use this software except in compliance with the License.
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.