objectid

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2023 License: MIT Imports: 12 Imported by: 3

README

ObjectID

GoDoc Go Report Card

ObjectID is a MongoDB ObjectID implementation for Go. See the MongoDB ObjectID documentation for more information.

Installation

go get github.com/pkg-id/objectid

Usage

package main

import (
	"fmt"
	"github.com/pkg-id/objectid"
)

func main() {
	id := objectid.New() 
	fmt.Println(id)
}

Documentation

Overview

Package objectid provides a way to generate MongoDB-style ObjectID. ref: https://www.mongodb.com/docs/manual/reference/method/ObjectId

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetCounter

func SetCounter(c Counter)

SetCounter sets the global counter value for generating ObjectIDs.

func SetMachineAndProcessID

func SetMachineAndProcessID(pid MachineProcessID)

SetMachineAndProcessID sets the machine and process ID for generating ObjectIDs.

Types

type Counter

type Counter uint32

Counter is the implementation of the counter in ObjectID.

func NewSecureCounter

func NewSecureCounter(reader io.Reader) (Counter, error)

NewSecureCounter generates a new secure counter value for generating ID.

func (*Counter) Next

func (c *Counter) Next() uint32

Next returns the next value of the counter.

type ID

type ID [timestampSize + processSize + counterSize]byte

ID is the implementation of MongoDB ObjectID.

var Nil ID

Nil is a zero value of the ID.

func Decode

func Decode(s string) (ID, error)

Decode decodes the string representation and returns the corresponding ID.

func New

func New() ID

New generates a new ID using the current time epochs, machine and process ids, and the global counter value.

func NewEpochs

func NewEpochs(epochs int64) ID

NewEpochs same as New but with given epochs.

func (ID) Count

func (id ID) Count() uint32

Count returns the counter value portion of the ID.

func (ID) IsZero

func (id ID) IsZero() bool

IsZero returns true if the ObjectID is the Nil value.

func (ID) MarshalJSON

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

MarshalJSON implements the json.Marshaler interface.

func (ID) MarshalText

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

MarshalText implements the encoding.TextMarshaler interface. This is useful when using the ID as a map key during JSON marshalling.

func (*ID) Scan added in v0.2.0

func (id *ID) Scan(src any) error

Scan implements the sql.Scanner

func (ID) String

func (id ID) String() string

String returns a string representation of the ID.

func (ID) Timestamp

func (id ID) Timestamp() time.Time

Timestamp returns the timestamp portion of the ID as a time.Time object.

func (*ID) UnmarshalJSON

func (id *ID) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler.

func (*ID) UnmarshalText

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

UnmarshalText implements the encoding.TextUnmarshaler interface. This is useful when using the ID as a map key during JSON unmarshalling.

func (ID) Value added in v0.2.0

func (id ID) Value() (driver.Value, error)

Value implements the driver.Valuer.

type MachineProcessID

type MachineProcessID [processSize]byte

MachineProcessID is the implementation of the machine and process id portion for the ObjectID.

func NewMachineProcessID

func NewMachineProcessID(reader io.Reader) (MachineProcessID, error)

NewMachineProcessID generates a new machine and process ids for generating ID.

Jump to

Keyboard shortcuts

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