objectuuid

package
v1.3.4 Latest Latest
Warning

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

Go to latest
Published: Dec 11, 2025 License: AGPL-3.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidHex = errors.New("the provided hex string is not a valid ObjectID")

ErrInvalidHex indicates that a hex string cannot be converted to an ObjectID.

Functions

func MarshalJSON

func MarshalJSON(val any) ([]byte, error)

func UnmarshalJSON

func UnmarshalJSON(data []byte, val any) error

func Validate

func Validate(s string) error

Types

type ObjectID

type ObjectID [12]byte

ObjectID is the BSON ObjectID type.

var NilObjectID ObjectID

NilObjectID is the zero value for ObjectID.

func ObjectIDFromHex

func ObjectIDFromHex(s string) (ObjectID, error)

ObjectIDFromHex creates a new ObjectID from a hex string. It returns an error if the hex string is not a valid ObjectID.

func (ObjectID) Hex

func (id ObjectID) Hex() string

Hex returns the hex encoding of the ObjectID as a string.

func (ObjectID) IsZero

func (id ObjectID) IsZero() bool

IsZero returns true if id is the empty ObjectID.

func (ObjectID) MarshalJSON

func (id ObjectID) MarshalJSON() ([]byte, error)

MarshalJSON returns the ObjectID as a string

func (ObjectID) MarshalText

func (id ObjectID) MarshalText() ([]byte, error)

MarshalText returns the ObjectID as UTF-8-encoded text. Implementing this allows us to use ObjectID as a map key when marshalling JSON. See https://pkg.go.dev/encoding#TextMarshaler

func (ObjectID) String

func (id ObjectID) String() string

func (ObjectID) Timestamp

func (id ObjectID) Timestamp() time.Time

Timestamp extracts the time part of the ObjectId.

func (*ObjectID) UnmarshalJSON

func (id *ObjectID) UnmarshalJSON(b []byte) error

UnmarshalJSON populates the byte slice with the ObjectID. If the byte slice is 24 bytes long, it will be populated with the hex representation of the ObjectID. If the byte slice is twelve bytes long, it will be populated with the BSON representation of the ObjectID. This method also accepts empty strings and decodes them as NilObjectID. For any other inputs, an error will be returned.

func (*ObjectID) UnmarshalText

func (id *ObjectID) UnmarshalText(b []byte) error

UnmarshalText populates the byte slice with the ObjectID. Implementing this allows us to use ObjectID as a map key when unmarshalling JSON. See https://pkg.go.dev/encoding#TextUnmarshaler

type ObjectUUID

type ObjectUUID uuid.UUID

ObjectUUID will transform MongoDB's ObjectID (12 bytes) into UUID format (16 bytes). The last 4 bytes are always zero.

var (
	Nil ObjectUUID
)

func FromObjectID

func FromObjectID(oid ObjectID) ObjectUUID

FromObjectID converts MongoDB's ObjectID to ObjectUUID

func FromObjectIDHex

func FromObjectIDHex(oidHex string) (ObjectUUID, error)

func New

func New() ObjectUUID

func (ObjectUUID) Hex

func (cu ObjectUUID) Hex() string

Hex returns objectID hex string

func (ObjectUUID) IsZero

func (cu ObjectUUID) IsZero() bool

func (ObjectUUID) MarshalJSON

func (cu ObjectUUID) MarshalJSON() ([]byte, error)

MarshalJSON outputs ObjectUUID as a JSON string (ObjectID hex).

func (ObjectUUID) MarshalText

func (cu ObjectUUID) MarshalText() ([]byte, error)

MarshalText returns the ObjectUUID as text (hex of ObjectID part).

func (*ObjectUUID) Scan

func (c *ObjectUUID) Scan(value any) error

Scan implements sql.Scanner (for reading from DB)

func (ObjectUUID) String

func (c ObjectUUID) String() string

func (ObjectUUID) ToObjectID

func (cu ObjectUUID) ToObjectID() ObjectID

ToObjectID converts ObjectUUID back to MongoDB's ObjectID

func (*ObjectUUID) UnmarshalJSON

func (cu *ObjectUUID) UnmarshalJSON(b []byte) error

UnmarshalJSON parses JSON into ObjectUUID. Supports:

  • null → leave unchanged
  • "507f1f77bcf86cd799439011" (24 hex chars)
  • {"$oid": "..."} (Mongo Extended JSON)
  • "" → Nil

func (*ObjectUUID) UnmarshalText

func (cu *ObjectUUID) UnmarshalText(b []byte) error

UnmarshalText parses ObjectUUID from hex text.

func (ObjectUUID) Value

func (c ObjectUUID) Value() (driver.Value, error)

Value implements driver.Valuer (for writing into DB)

type ObjectUUIDs

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

func NewObjectUUIDs

func NewObjectUUIDs(ids ...ObjectUUID) *ObjectUUIDs

func (*ObjectUUIDs) Add

func (m *ObjectUUIDs) Add(ids ...ObjectUUID)

func (*ObjectUUIDs) Contains

func (m *ObjectUUIDs) Contains(id ObjectUUID) bool

func (*ObjectUUIDs) IsEmpty

func (m *ObjectUUIDs) IsEmpty() bool

func (*ObjectUUIDs) Len

func (m *ObjectUUIDs) Len() int

func (*ObjectUUIDs) Remove

func (m *ObjectUUIDs) Remove(ids ...ObjectUUID)

func (*ObjectUUIDs) UnsafeAdd

func (m *ObjectUUIDs) UnsafeAdd(ids ...ObjectUUID)

func (*ObjectUUIDs) UnsafeRemove

func (m *ObjectUUIDs) UnsafeRemove(ids ...ObjectUUID)

func (*ObjectUUIDs) UnsafeValue

func (m *ObjectUUIDs) UnsafeValue() []ObjectUUID

Jump to

Keyboard shortcuts

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