types

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2024 License: BSD-3-Clause Imports: 8 Imported by: 0

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

Constants

This section is empty.

Variables

View Source
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

func (b Binary) MarshalBSONValue() (bsontype.Type, []byte, error)

MarshalBSONValue serializes the Binary receiver into a BSON value.

func (Binary) MarshalJSON

func (b Binary) MarshalJSON() ([]byte, error)

MarshalJSON serializes the Binary receiver as a base64-encoded string or null if empty.

func (*Binary) UnmarshalBSONValue

func (b *Binary) UnmarshalBSONValue(t bsontype.Type, data []byte) error

UnmarshalBSONValue decodes a BSON value into the Binary receiver, ensuring it is of the correct BSON type and subtype.

func (*Binary) UnmarshalJSON

func (b *Binary) UnmarshalJSON(data []byte) error

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

func (v NullInt32) MarshalBSONValue() (bsontype.Type, []byte, error)

MarshalBSONValue serializes the NullInt32 value into BSON format, encoding zero values as null.

func (NullInt32) MarshalJSON

func (v NullInt32) MarshalJSON() ([]byte, error)

MarshalJSON serializes the NullInt32 value into JSON, encoding zero values as null.

type NullInt64

type NullInt64 int64

func (NullInt64) MarshalBSONValue

func (v NullInt64) MarshalBSONValue() (bsontype.Type, []byte, error)

MarshalBSONValue serializes NullInt64 into a BSON value. Returns BSON null type if the value is zero.

func (NullInt64) MarshalJSON

func (v NullInt64) MarshalJSON() ([]byte, error)

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

func ObjectIdFromHex(s string) (ObjectId, error)

ObjectIdFromHex converts a hexadecimal string representation of an ObjectId to an ObjectId type, returning an error if invalid.

func (ObjectId) IsZero

func (o ObjectId) IsZero() bool

IsZero checks if the ObjectId is zero (an empty string) and returns true if it is, otherwise false.

func (ObjectId) MarshalBSONValue

func (o ObjectId) MarshalBSONValue() (bsontype.Type, []byte, error)

MarshalBSONValue serializes the ObjectId to BSON. It returns BSON null type for zero values or in case of invalid ObjectId.

func (ObjectId) MarshalJSON

func (o ObjectId) MarshalJSON() ([]byte, error)

MarshalJSON serializes the ObjectId to JSON, rendering as null if the ObjectId is zero or equals NilObjectID.

func (ObjectId) String

func (o ObjectId) String() string

String returns the string representation of the ObjectId. If the ObjectId is zero, it returns "null".

func (*ObjectId) UnmarshalBSONValue

func (o *ObjectId) UnmarshalBSONValue(t bsontype.Type, data []byte) error

UnmarshalBSONValue deserializes BSON value into ObjectId type. Returns an error if the BSON type is invalid.

func (*ObjectId) UnmarshalJSON

func (o *ObjectId) UnmarshalJSON(data []byte) error

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

func UuidFromString(id string) (UUID, error)

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

func (u UUID) IsZero() bool

IsZero checks if the UUID is empty, returning true if it is and false otherwise.

func (UUID) MarshalBSONValue

func (u UUID) MarshalBSONValue() (bsontype.Type, []byte, error)

MarshalBSONValue marshals the UUID into a BSON value. Returns BSON type, byte slice, and an error if any.

func (UUID) MarshalJSON

func (u UUID) MarshalJSON() ([]byte, error)

MarshalJSON serializes the UUID into a JSON string. If the UUID is empty, it serializes it as null.

func (UUID) String

func (u UUID) String() string

String converts the UUID to its string representation.

func (*UUID) UnmarshalBSONValue

func (u *UUID) UnmarshalBSONValue(t bsontype.Type, data []byte) error

UnmarshalBSONValue deserializes a BSON value into a UUID. Returns an error if the BSON type or subtype is incorrect.

func (*UUID) UnmarshalJSON

func (u *UUID) UnmarshalJSON(data []byte) error

UnmarshalJSON deserializes JSON data into the UUID. It handles both non-null and null cases appropriately.

Jump to

Keyboard shortcuts

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