xbson

package
v2.5.6 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2024 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultBsonRegistryBuilder = NewBsonRegistryBuilder()

Functions

func BuildDefaultRegistry

func BuildDefaultRegistry()

BuildDefaultRegistry builds the default registry to be used by the mongo driver Previous registries are discarded

func BuildRegistry

func BuildRegistry() *bsoncodec.Registry

BuildRegistry creates a new registry configured with the default encoders and decoders from the bsoncodec.DefaultValueEncoders and bsoncodec.DefaultValueDecoders types, the PrimitiveCodecs type in this package, and 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 *bsoncodec.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 *bsoncodec.Registry, bs []byte, value interface{}) error

Types

type BsonCodecsRegistrant

type BsonCodecsRegistrant func(builder *BsonRegistryBuilder)

type BsonRegistryBuilder

type BsonRegistryBuilder struct {
	*bsoncodec.RegistryBuilder
	// contains filtered or unexported fields
}

BsonRegistryBuilder initializes the mongo driver registry to encode/decode honoring the JSON struct tags. For example, a struct with the following JSON tags:

type Sample struct {
       FirstName string `json:"first_name"`
}

Will serialize the field to BSON as "first_name" instead of "firstname" (default naming strategy).

func NewBsonRegistryBuilder

func NewBsonRegistryBuilder() *BsonRegistryBuilder

func (*BsonRegistryBuilder) Build

func (b *BsonRegistryBuilder) Build()

Build sets this registry as the BSON default

func (*BsonRegistryBuilder) Register

Register a custom codec to the default BSON registry

func (*BsonRegistryBuilder) RegisterAll

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

RegisterAll register all the custom codecs to the default BSON registry

func (*BsonRegistryBuilder) RegisterTypeDecoder

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

func (*BsonRegistryBuilder) RegisterTypeEncoder

func (b *BsonRegistryBuilder) RegisterTypeEncoder(t reflect.Type, dec bsoncodec.ValueEncoder)

func (*BsonRegistryBuilder) StructCodec

func (b *BsonRegistryBuilder) StructCodec() *bsoncodec.StructCodec

StructCodec provides the configured bsoncodec.StructCodec in registry

type EncoderDecoder

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

EncoderDecoder is a bsoncodec.ValueDecoder and bsoncodec.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 bsoncodec.ValueEncoder)
	RegisterTypeDecoder(valueType reflect.Type, enc bsoncodec.ValueDecoder)
	RegisterInterfaceEncoder(t reflect.Type, enc bsoncodec.ValueEncoder)
	RegisterInterfaceDecoder(t reflect.Type, enc bsoncodec.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 bsoncodec.DecodeContext, reader bsonrw.ValueReader, value reflect.Value) error

DecodeValue implements the bsoncodec.ValueDecoder interface

func (*TypedGenericCodex[Typed]) EncodeValue

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

EncodeValue implements the bsoncodec.ValueEncoder interface

func (*TypedGenericCodex[Typed]) Register

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

Register implements the bsoncodec.RegistryBuilder interface It allows the decoderEncoder to be registered with a bsoncodec.RegistryBuilder

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