CodecProviderMacro

io.github.mbannour.mongo.codecs.CodecProviderMacro

CodecProviderMacro is a utility object that provides inline macros for generating MongoDB CodecProvider instances for Scala case classes.

A CodecProvider wraps a generated Codec[T] so it can be plugged into the MongoDB driver's CodecRegistry, allowing seamless serialization and deserialization of your domain models.

==Quick Start== In your case class companion: {{ import io.github.mbannour.mongo.codecs.{CodecProviderMacro, CodecConfig, NoneHandling} import org.bson.codecs.configuration.CodecRegistries import org.mongodb.scala.MongoClient

case class Person(name: String, age: Int, nickname: Option[String])

object Person: private val config = CodecConfig(noneHandling = NoneHandling.Encode) private val provider = CodecProviderMacro.createCodecProvider[Person]

given registry: CodecRegistry = CodecRegistries.fromRegistries( MongoClient.DEFAULT_CODEC_REGISTRY, CodecRegistries.fromProviders(provider) ) end Person }}

Attributes

See also

CaseClassCodecGenerator.generateCodec for the underlying codec generator.

Graph
Supertypes
class Object
trait Matchable
class Any
Self type

Members list

Value members

Concrete methods

inline def createCodecProvider[T](using classTag: ClassTag[T], config: CodecConfig, codecRegistry: CodecRegistry): CodecProvider

Creates a CodecProvider for type T using the specified configuration.

Creates a CodecProvider for type T using the specified configuration.

Type parameters

T

The case class type for which to generate the provider.

Value parameters

classTag

Runtime ClassTag for T (injected implicitly).

codecRegistry

The base CodecRegistry used for nested type lookups.

config

Configuration for codec generation behavior.

Attributes

Returns

A CodecProvider that will supply a BSON Codec[T].

Example

{{ val provider = CodecProviderMacro.createCodecProvider[Person](using classTag, CodecConfig(), registry) }}

inline def createCodecProviderEncodeNone[T](using classTag: ClassTag[T], codecRegistry: CodecRegistry): CodecProvider

Creates a CodecProvider for type T that encodes None values as BSON null.

Creates a CodecProvider for type T that encodes None values as BSON null.

Type parameters

T

The case class type for which to generate the provider.

Value parameters

classTag

Runtime ClassTag for T (injected implicitly).

codecRegistry

The base CodecRegistry used for nested type lookups.

Attributes

Returns

A CodecProvider that will supply a BSON Codec[T] including None fields.

inline def createCodecProviderIgnoreNone[T](using classTag: ClassTag[T], codecRegistry: CodecRegistry): CodecProvider

Creates a CodecProvider for type T that ignores None values during serialization.

Creates a CodecProvider for type T that ignores None values during serialization.

Type parameters

T

The case class type for which to generate the provider.

Value parameters

classTag

Runtime ClassTag for T (injected implicitly).

codecRegistry

The base CodecRegistry used for nested type lookups.

Attributes

Returns

A CodecProvider that will supply a BSON Codec[T] omitting fields with None values.