objectid

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2024 License: MIT Imports: 8 Imported by: 0

README

ObjectID

Library to generate MongoDB objectID's

Go Report Card PkgGoDev

Command usage

$ objectid -h
Usage of objectid:
  -format string
        format of objectid: hex, base64 (default "hex")
  -from-time string
        create a new objectid from a date time (RFC3339, $(date -I), $(date -Ihours), $(date -d -Iminutes), $(date -Iseconds)
  -n int
        number of objectid to generate (default 1)
  -separator string
        separator between objectids (default "\n")
  -to-time string
        convert objectid to time
Generate object id
$ objectid
6435f4800000000000000000
Generate object id in base64
$ objectid -format base64
ZiesvNpsbBz8QUGk
Generate multiple object id
$ objectid -n 3
6435f4800000000000000000
6435f4800000000000000001
6435f4800000000000000002
Object id from time
$ objectid -from-time 2023-04-12
6435f4800000000000000000%
Object id to time
$ objectid -to-time 6435f4800000000000000000
2023-04-12 00:00:00 +0000 UTC

Library Usage

go get -u github.com/Ajnasz/objectid
import github.com/Ajnasz/objectid


func main() {
    oid := objectid.New()
    fmt.Printf("%s", oid)
}

CLI

Install the cli tool
go install github.com/Ajnasz/objectid/cmd/objectid

or

make build
bin/objectid

Documentation

Overview

Package objectid package implements MongoDB's ObjectID type.

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrInvalidBase64 = fmt.Errorf("invalid base64")
View Source
var ErrInvalidHex = fmt.Errorf("invalid hex")

ErrInvalidHex is returned when an invalid hex string is provided.

View Source
var ErrInvalidTimeFormat = fmt.Errorf("invalid time format")

Functions

func GenerateTo

func GenerateTo(objectID *ObjectID)

GenerateTo generates a new ObjectID and writes it to the provided ObjectID

Example
counter.Store(0x6b5f9e) // as a user of this package, you don't need to do this, but it's done here to ensure the same output

var oid ObjectID
GenerateTo(&oid)
fmt.Println(oid.Hex())
Output:

5c2aad805f9e6b5f9e6b5f9f

Types

type ObjectID

type ObjectID [12]byte

ObjectID is a 12-byte unique identifier for a MongoDB document.

func FromBase64 added in v1.1.0

func FromBase64(str string) (ObjectID, error)

func FromHex

func FromHex(str string) (ObjectID, error)

FromHex creates a new ObjectID from a hex string.

Example
g, err := FromHex("5c2aad805f9e6b5f9e6b5fa3")

if err != nil {
	fmt.Println(err)
} else {
	fmt.Println(g.Hex())
}
Output:

5c2aad805f9e6b5f9e6b5fa3

func FromTime added in v1.1.0

func FromTime(str string) (ObjectID, error)

func New

func New() ObjectID

New generates a new ObjectID.

Example
counter.Store(0x6b5f9e) // as a user of this package, you don't need to do this, but it's done here to ensure the same output

g := New()
fmt.Println(g.Hex())
Output:

5c2aad805f9e6b5f9e6b5f9f

func (ObjectID) Base64 added in v1.1.0

func (id ObjectID) Base64() string

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

func (ObjectID) Hex

func (id ObjectID) Hex() string

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

func (ObjectID) String

func (id ObjectID) String() string

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

func (ObjectID) Time

func (id ObjectID) Time() time.Time

Time returns the timestamp part of the ObjectID.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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