xbson

package
v2.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 19, 2026 License: MIT Imports: 8 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultBsonRegistryBuilder = NewBsonRegistryBuilder()
View Source
var (

	// DefaultRegistry holds the registry produced by BuildDefaultRegistry. In
	// mongo-driver v2 there is no global default registry on the bson package;
	// consumers must pass this registry explicitly via
	// options.Client().SetRegistry(...) or options.Database().SetRegistry(...).
	DefaultRegistry *bson.Registry
)

Functions

func BuildDefaultRegistry

func BuildDefaultRegistry()

BuildDefaultRegistry builds the registry and stores it in xbson.DefaultRegistry. In mongo-driver v2 there is no settable global default registry, so consumers must pass xbson.DefaultRegistry explicitly to their mongo client/database.

func BuildRegistry

func BuildRegistry() *bson.Registry

BuildRegistry creates a new registry configured with the default encoders and decoders, plus all registered registrars.

func Dump

func Dump(t *testing.T, title string, bytes []byte)

func IsAlreadyRegistered

func IsAlreadyRegistered(registrar Registrar) bool

IsAlreadyRegistered checks if a Registrar is already registered.

func MarshalWithRegistry

func MarshalWithRegistry(registry *bson.Registry, value interface{}) ([]byte, error)

func Register

func Register(registrar Registrar)

Register registers a Registrar to the list of registrars. If the Registrar is already registered, it will not be registered again. This function is not thread-safe, and it is typically called from init() functions.

func UnmarshalWithRegistry

func UnmarshalWithRegistry(registry *bson.Registry, bs []byte, value interface{}) error

Types

type BsonCodecsRegistrant

type BsonCodecsRegistrant func(builder *BsonRegistryBuilder)

type BsonRegistryBuilder

type BsonRegistryBuilder struct {
	// contains filtered or unexported fields
}

BsonRegistryBuilder wraps a *bson.Registry to ease registration of custom codecs.

Note: in mongo-driver v2 there is no longer a settable global default registry, nor a registry-level option to honor JSON struct tags. JSON-tag fallback is configured per encoder/decoder via *bson.Encoder.UseJSONStructTags() and *bson.Decoder.UseJSONStructTags(). Build() stores the registry in xbson.DefaultRegistry — consumers must pass it explicitly to their mongo client/database via options.Client().SetRegistry(...).

func NewBsonRegistryBuilder

func NewBsonRegistryBuilder() *BsonRegistryBuilder

func (*BsonRegistryBuilder) Build

func (b *BsonRegistryBuilder) Build()

Build stores the registry in xbson.DefaultRegistry. Mongo-driver v2 has no settable global default registry, so consumers must pass xbson.DefaultRegistry explicitly via options.Client().SetRegistry(...) or options.Database().SetRegistry(...).

func (*BsonRegistryBuilder) Register

Register a custom codec to the BSON registry

func (*BsonRegistryBuilder) RegisterAll

func (b *BsonRegistryBuilder) RegisterAll(registrants ...BsonCodecsRegistrant) *BsonRegistryBuilder

RegisterAll register all the custom codecs to the BSON registry

func (*BsonRegistryBuilder) RegisterInterfaceDecoder added in v2.7.0

func (b *BsonRegistryBuilder) RegisterInterfaceDecoder(t reflect.Type, dec bson.ValueDecoder)

func (*BsonRegistryBuilder) RegisterInterfaceEncoder added in v2.7.0

func (b *BsonRegistryBuilder) RegisterInterfaceEncoder(t reflect.Type, enc bson.ValueEncoder)

func (*BsonRegistryBuilder) RegisterTypeDecoder

func (b *BsonRegistryBuilder) RegisterTypeDecoder(t reflect.Type, dec bson.ValueDecoder)

func (*BsonRegistryBuilder) RegisterTypeEncoder

func (b *BsonRegistryBuilder) RegisterTypeEncoder(t reflect.Type, enc bson.ValueEncoder)

func (*BsonRegistryBuilder) Registry added in v2.7.0

func (b *BsonRegistryBuilder) Registry() *bson.Registry

Registry returns the underlying *bson.Registry for direct use.

type EncoderDecoder

type EncoderDecoder interface {
	Registrar
	bson.ValueDecoder
	bson.ValueEncoder
}

EncoderDecoder is a bson.ValueDecoder and bson.ValueEncoder for a given type

func NewDecoderEncoder

func NewDecoderEncoder[Entity, Dto, Base any](toDto func(Entity) Dto, fromDto func(Dto) Entity) EncoderDecoder

NewDecoderEncoder creates a new decoderEncoder backed by the provided conversion functions

type GetType

type GetType[Typed any] func(Typed) string

GetType provides the type for a family of types that can be encoded/decoded to/from a bson document. It allows a generic type to be encoded/decoded to/from a bson document.

type Registrant

type Registrant interface {
	RegisterTypeEncoder(valueType reflect.Type, enc bson.ValueEncoder)
	RegisterTypeDecoder(valueType reflect.Type, enc bson.ValueDecoder)
	RegisterInterfaceEncoder(t reflect.Type, enc bson.ValueEncoder)
	RegisterInterfaceDecoder(t reflect.Type, enc bson.ValueDecoder)
}

type Registrar

type Registrar interface {
	Register(builder Registrant)
}

type TypedGenericCodex

type TypedGenericCodex[Typed any] struct {
	// contains filtered or unexported fields
}

TypedGenericCodex is a generic encoder/decoder for a family of types that implement the Typed interface It allows a generic type to be encoded/decoded to/from a bson document. The getType function is used to determine the type of the underlying value.

func NewTypedGenericCodex

func NewTypedGenericCodex[Typed any](getType GetType[Typed]) *TypedGenericCodex[Typed]

func (*TypedGenericCodex[Typed]) DecodeValue

func (t *TypedGenericCodex[Typed]) DecodeValue(ctx bson.DecodeContext, reader bson.ValueReader, value reflect.Value) error

DecodeValue implements the bson.ValueDecoder interface

func (*TypedGenericCodex[Typed]) EncodeValue

func (t *TypedGenericCodex[Typed]) EncodeValue(ctx bson.EncodeContext, writer bson.ValueWriter, value reflect.Value) error

EncodeValue implements the bson.ValueEncoder interface

func (*TypedGenericCodex[Typed]) Register

func (t *TypedGenericCodex[Typed]) Register(builder Registrant)

Register implements the Registrar interface. It allows the decoderEncoder to be registered with a bson.Registry.

func (*TypedGenericCodex[Typed]) RegisterType

func (t *TypedGenericCodex[Typed]) RegisterType(
	factory func() Typed,
	toDto func(Typed) interface{},
	fromDto func(interface{}) Typed,
)

RegisterType registers a factory function for a given type name

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL