Documentation
¶
Overview ¶
Package types provides a binary datatype. The binary datatype stores any arbitrary value as binary, the binary subtype is `bson.TypeBinaryGeneric`. The JSON representation of the binary is base64.
Package types provides various number datatypes, they are treated as BSON-null if their value is 0 oder 0.0 and vice versa.
Package types provides an ObjectId replacement of the mongodb drivers primitive.ObjectId. The original ObjectId has two disadvantages:
* an empty ObjectId is stored as "000000000000000000000000", instead of null, which is kind of weird.
* every conversion between a string and an ObjectId has to be done using ObjectIDFromHex(), which adds a lot of extra code.
Package types provides the NullString datatype, which encodes empty strings to null and vice versa.
Package types provides the UUID datatype, which derives from string for easy conversion, it's BSON represenation is primitive.Binary with the subtype of bson.TypeBinaryUUID. This means it is store as native UUID into the database. An empty UUID is treated as null when converting to BSON.
Index ¶
- Variables
- func SetObjectIdGenerator(g func() string)
- func SetUuidGenerator(g func() string)
- type Binary
- type NullFloat32
- type NullFloat64
- type NullInt32
- type NullInt64
- type NullString
- type ObjectId
- func (o ObjectId) IsZero() bool
- func (o ObjectId) MarshalBSONValue() (bsontype.Type, []byte, error)
- func (o ObjectId) MarshalJSON() ([]byte, error)
- func (o ObjectId) String() string
- func (o *ObjectId) UnmarshalBSONValue(t bsontype.Type, data []byte) error
- func (o *ObjectId) UnmarshalJSON(data []byte) error
- type UUID
Constants ¶
This section is empty.
Variables ¶
var NilObjectID = ObjectId("000000000000000000000000")
Functions ¶
func SetObjectIdGenerator ¶
func SetObjectIdGenerator(g func() string)
SetObjectIdGenerator sets a custom function for generating ObjectId strings. This is mainly used for testing purposes.
func SetUuidGenerator ¶
func SetUuidGenerator(g func() string)
SetUuidGenerator sets a custom function for generating UUID strings. This is mainly used for testing purposes.
Types ¶
type Binary ¶
type Binary []byte
func (Binary) MarshalBSONValue ¶
MarshalBSONValue serializes the Binary receiver into a BSON value.
func (Binary) MarshalJSON ¶
MarshalJSON serializes the Binary receiver as a base64-encoded string or null if empty.
func (*Binary) UnmarshalBSONValue ¶
UnmarshalBSONValue decodes a BSON value into the Binary receiver, ensuring it is of the correct BSON type and subtype.
func (*Binary) UnmarshalJSON ¶
UnmarshalJSON decodes a JSON-encoded byte slice as a base64-encoded string and stores the result in the Binary receiver.
type NullFloat32 ¶
type NullFloat32 float64
func (NullFloat32) MarshalBSONValue ¶
func (v NullFloat32) MarshalBSONValue() (bsontype.Type, []byte, error)
MarshalBSONValue serializes the NullFloat32 value to BSON. If the value is 0, it marshals to BSON null; otherwise, as float32.
func (NullFloat32) MarshalJSON ¶
func (v NullFloat32) MarshalJSON() ([]byte, error)
MarshalJSON serializes the NullFloat32 value to JSON. If the value is 0, it marshals to JSON null. Otherwise, it marshals as float32.
type NullFloat64 ¶
type NullFloat64 float64
func (NullFloat64) MarshalBSONValue ¶
func (v NullFloat64) MarshalBSONValue() (bsontype.Type, []byte, error)
MarshalBSONValue customizes the BSON marshaling process for NullFloat64. It returns BSON Null type
func (NullFloat64) MarshalJSON ¶
func (v NullFloat64) MarshalJSON() ([]byte, error)
MarshalJSON customizes the JSON marshaling process for NullFloat64. It returns nil if the value is 0, otherwise it returns the float64 value.
type NullInt32 ¶
type NullInt32 int32
func (NullInt32) MarshalBSONValue ¶
MarshalBSONValue serializes the NullInt32 value into BSON format, encoding zero values as null.
func (NullInt32) MarshalJSON ¶
MarshalJSON serializes the NullInt32 value into JSON, encoding zero values as null.
type NullInt64 ¶
type NullInt64 int64
func (NullInt64) MarshalBSONValue ¶
MarshalBSONValue serializes NullInt64 into a BSON value. Returns BSON null type if the value is zero.
func (NullInt64) MarshalJSON ¶
MarshalJSON marshals the NullInt64 value into JSON. If the value is zero, it marshals as null.
type NullString ¶
type NullString string
func (NullString) MarshalBSONValue ¶
func (v NullString) MarshalBSONValue() (bsontype.Type, []byte, error)
MarshalBSONValue serializes the NullString value to BSON. If the value is empty, it returns BSON null.
func (NullString) MarshalJSON ¶
func (v NullString) MarshalJSON() ([]byte, error)
MarshalJSON serializes the NullString value to JSON. If the value is empty, it returns JSON null.
type ObjectId ¶
type ObjectId string
ObjectId holds a mongodb objectid renders to null if zero
func NewObjectId ¶
func NewObjectId() ObjectId
NewObjectId generates and returns a new ObjectId using the objectIdGenerator function.
func ObjectIdFromHex ¶
ObjectIdFromHex converts a hexadecimal string representation of an ObjectId to an ObjectId type, returning an error if invalid.
func (ObjectId) IsZero ¶
IsZero checks if the ObjectId is zero (an empty string) and returns true if it is, otherwise false.
func (ObjectId) MarshalBSONValue ¶
MarshalBSONValue serializes the ObjectId to BSON. It returns BSON null type for zero values or in case of invalid ObjectId.
func (ObjectId) MarshalJSON ¶
MarshalJSON serializes the ObjectId to JSON, rendering as null if the ObjectId is zero or equals NilObjectID.
func (ObjectId) String ¶
String returns the string representation of the ObjectId. If the ObjectId is zero, it returns "null".
func (*ObjectId) UnmarshalBSONValue ¶
UnmarshalBSONValue deserializes BSON value into ObjectId type. Returns an error if the BSON type is invalid.
func (*ObjectId) UnmarshalJSON ¶
UnmarshalJSON unmarshals a JSON-encoded string into an ObjectId. Handles null values by setting the ObjectId to an empty string.
type UUID ¶
type UUID string
func NewUuid ¶
func NewUuid() UUID
NewUuid generates a new UUID using the configured uuidGenerator function and returns it as a UUID type.
func UuidFromString ¶
UuidFromString converts a string representation of a UUID to a UUID type. Returns an error if the string is not a valid UUID.
func (UUID) IsZero ¶
IsZero checks if the UUID is empty, returning true if it is and false otherwise.
func (UUID) MarshalBSONValue ¶
MarshalBSONValue marshals the UUID into a BSON value. Returns BSON type, byte slice, and an error if any.
func (UUID) MarshalJSON ¶
MarshalJSON serializes the UUID into a JSON string. If the UUID is empty, it serializes it as null.
func (*UUID) UnmarshalBSONValue ¶
UnmarshalBSONValue deserializes a BSON value into a UUID. Returns an error if the BSON type or subtype is incorrect.
func (*UUID) UnmarshalJSON ¶
UnmarshalJSON deserializes JSON data into the UUID. It handles both non-null and null cases appropriately.