reactivemongo.bson

Macros

object Macros

Macros for generating BSONReader and SONWriter implementations for case at compile time. Invoking these macros is equivalent to writing anonymous class implementations by hand.

Example

case class Person(name: String, surname: String)
implicit val personHandler = Macros.handler[Person]

Use reader to generate the BSONReader and writer for BSONWriter or handler for a class that extends both. Respective methods with 'Opts' appended take additional options in form of type parameters.

The A type parameter defines case class that will be the basis for auto-generated implementation. Some other types with matching apply-unapply might work but behaviour is undefined. Since macros will match the apply-unapply pair you are free to overload these methods in the companion object.

Fields in the case class get mapped into BSON properties with respective names and BSON handlers are pulled from implicit scope to (de)serialize them. In order to use custom types inside case classes just make sure appropriate handlers are in scope. Note that companion objects are searched too. For example if you have case class Foo(bar: Bar) and want to create a handler for it is enough to put an implicit handler for Bar in it's companion object. That handler might be macro generated or written by hand.

Case classes can also be defined inside other classes, objects or traits but not inside functions(known limitation). In order to work you should have the case class in scope(where you call the macro) so you can refer to it by it's short name - without package. This is necessary because the generated implementations refer to it by the short name to support nested declarations. You can work around this with local imports.

Example

implicit val handler = {
import some.package.Foo
Macros.handler[Foo]
}

Option types are handled somewhat specially: a field of type Option[T] will only be appended to the document if it contains a value. Similarly if a document does not contain a value it will be read as None.

Also supported neat trick are 'union types' that make for easy work with algebraic data types. See the UnionType option for more details.

You can also create recursive structures by explicitly annotating types of the implicit handlers. (To let the compiler know they exist) Example

sealed trait Tree
case class Node(left: Tree, right: Tree) extends Tree
case class Leaf(data: String) extends Tree

object Tree {
  import Macros.Options._
  implicit val bson: Handler[Tree] = Macros.handlerOpts[Tree, UnionType[Node \/ Leaf]]
}
See also

Macros.Options for specific options

Linear Supertypes
AnyRef, Any
Ordering
  1. Alphabetic
  2. By inheritance
Inherited
  1. Macros
  2. AnyRef
  3. Any
  1. Hide All
  2. Show all
Learn more about member selection
Visibility
  1. Public
  2. All

Value Members

  1. final def !=(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  2. final def !=(arg0: Any): Boolean

    Definition Classes
    Any
  3. final def ##(): Int

    Definition Classes
    AnyRef → Any
  4. final def ==(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  5. final def ==(arg0: Any): Boolean

    Definition Classes
    Any
  6. object Annotations

    Annotations to use on case classes that are being processed by macros

  7. object Options

    Methods with 'Opts' postfix will take additional options in the form of type parameters that will customize behavior of the macros during compilation.

  8. final def asInstanceOf[T0]: T0

    Definition Classes
    Any
  9. def clone(): AnyRef

    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  10. final def eq(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  11. def equals(arg0: Any): Boolean

    Definition Classes
    AnyRef → Any
  12. def finalize(): Unit

    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  13. final def getClass(): Class[_]

    Definition Classes
    AnyRef → Any
  14. def handler[A]: BSONDocumentReader[A] with BSONDocumentWriter[A] with BSONHandler[BSONDocument, A]

    Creates an instance of BSONReader and BSONWriter for case class A

    Creates an instance of BSONReader and BSONWriter for case class A

    Annotations
    @macroImpl( ... )
  15. def handlerOpts[A, Opts <: Default]: BSONDocumentReader[A] with BSONDocumentWriter[A] with BSONHandler[BSONDocument, A]

    Creates an instance of BSONReader and BSONWriter for case class A and takes additional options

    Creates an instance of BSONReader and BSONWriter for case class A and takes additional options

    Annotations
    @macroImpl( ... )
  16. def hashCode(): Int

    Definition Classes
    AnyRef → Any
  17. final def isInstanceOf[T0]: Boolean

    Definition Classes
    Any
  18. final def ne(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  19. final def notify(): Unit

    Definition Classes
    AnyRef
  20. final def notifyAll(): Unit

    Definition Classes
    AnyRef
  21. def reader[A]: BSONDocumentReader[A]

    Creates an instance of BSONReader for case class A

    Creates an instance of BSONReader for case class A

    Annotations
    @macroImpl( ... )
    See also

    Macros

  22. def readerOpts[A, Opts <: Default]: BSONDocumentReader[A]

    Creates an instance of BSONReader for case class A and takes additional options

    Creates an instance of BSONReader for case class A and takes additional options

    Annotations
    @macroImpl( ... )
  23. final def synchronized[T0](arg0: ⇒ T0): T0

    Definition Classes
    AnyRef
  24. def toString(): String

    Definition Classes
    AnyRef → Any
  25. final def wait(): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  26. final def wait(arg0: Long, arg1: Int): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  27. final def wait(arg0: Long): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  28. def writer[A]: BSONDocumentWriter[A]

    Creates an instance of BSONWriter for case class A

    Creates an instance of BSONWriter for case class A

    Annotations
    @macroImpl( ... )
  29. def writerOpts[A, Opts <: Default]: BSONDocumentWriter[A]

    Creates an instance of BSONWriter for case class A and takes additional options

    Creates an instance of BSONWriter for case class A and takes additional options

    Annotations
    @macroImpl( ... )

Inherited from AnyRef

Inherited from Any

Ungrouped