snowflake

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2020 License: MIT Imports: 5 Imported by: 0

README

Snowflake-Go

I will provide a high level overview shortly.

Documentation

Overview

Package snowflake implements methods to generate and unmarshal unique identifiers in the style of Twitters snowflake network ID tool.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option func(*Snowflake)

Option is the interface for all Snowflake Options to follow in order to modify a new Snowflake via the NewWithOptions function.

func WithEpoch

func WithEpoch(e uint64) Option

WithEpoch is a function to set a custom epoch for generating or parsing a Snowflake with a custom Epoch (such as Discord.)

Arguments:

e (uint64): The value of the epoch

Returns:

(Option): A Snowflake Option instance to be consumed
          by NewWithOptions.

func WithIncrimentID

func WithIncrimentID(id uint64) Option

WithIncrimentID is a function to set a custom IncrimentID for generating a Snowflake with a specific IncrimentID.

This Option has no bearing on how an IncrimentID would be parsed by the Unmarshal function.

Arguments:

id (uint64): The value of the IncrimentID

Returns:

(Option): A Snowflake Option instance to be consumed
          by NewWithOptions.

func WithProcessID

func WithProcessID(id uint64) Option

WithProcessID is a function to set a custom ProcessID for generating a Snowflake with a specific ProcessID.

This Option has no bearing on how a ProcessID would be parsed by the Unmarshal function.

Arguments:

id (uint64): The value of the ProcessID

Returns:

(Option): A Snowflake Option instance to be consumed
          by NewWithOptions.

func WithTime

func WithTime(t time.Time) Option

WithTime is a function to set a custom time for generating or parsing a Snowflake.

Arguments:

t (time.Time): The value of the custom time

Returns:

(Option): A Snowflake Option instance to be consumed
          by NewWithOptions.

func WithWorkerID

func WithWorkerID(id uint64) Option

WithWorkerID is a function to set a custom WorkerID for generating a Snowflake with a specific WorkerID.

This Option has no bearing on how a WorkerID would be parsed by the Unmarshal function.

Arguments:

id (uint64): The value of the WorkerID

Returns:

(Option): A Snowflake Option instance to be consumed
          by NewWithOptions.

type Snowflake

type Snowflake struct {

	// Timestamp is used to generate the last 42 bits of the
	// snowflake.
	Timestamp time.Time

	// InternalWorkerID is used to identify the worker that
	// is generating this snowflake. This field makes up
	// the 17th - 21st bits of the snowflake.
	InternalWorkerID uint64

	// InternalProcessID is used to identify the process of
	// the worker that is generating this snowflake. This
	// field makes up the 12th - 16th bits of the snowflake.
	InternalProcessID uint64

	// IncrimentID is incremented each time a worker/process
	// combo creates another identifier. This field makes up
	// the first 11 bits of the snowflake.
	IncrimentID uint64
	// contains filtered or unexported fields
}

Snowflake is the core structure to held data used to create and unmarshal new snowflake identifiers.

func New

func New() *Snowflake

New generates a new Snowflake with default values:

  • The standard UNIX epoch
  • The current time
  • A WorkerID of 0
  • A ProcessID of 0
  • An IncrimentID of 0

Arguments:

None

Returns:

(*Snowflake): A pointer to the new instance of Snowflake.

func NewWithOptions

func NewWithOptions(opts ...Option) *Snowflake

NewWithOptions generates a new Snowflake with custom values, or uses the default value if an option is not specified. Default values are:

  • The standard UNIX epoch
  • The current time
  • A WorkerID of 0
  • A ProcessID of 0
  • An IncrimentID of 0

Arguments:

None

Returns:

(*Snowflake): A pointer to the new instance of Snowflake.

func (Snowflake) Int

func (S Snowflake) Int() int

Int returns the value of this snowflake as in int type.

Argumenets:

None

Returns:

(int): The int value of the snowflake.

func (Snowflake) Int64

func (S Snowflake) Int64() int64

Int64 returns the value of this snowflake as an int64 type.

Argumenets:

None

Returns:

(int64): The int64 value of the snowflake.

func (Snowflake) String

func (S Snowflake) String() string

String returns the string value of this snowflake.

Argumenets:

None

Returns:

(string): The string value of the snowflake.

func (Snowflake) Uint

func (S Snowflake) Uint() uint

Uint returns the value of this snowflake as a uint type.

Argumenets:

None

Returns:

(uint): The uint value of the snowflake.

func (Snowflake) Uint64

func (S Snowflake) Uint64() uint64

Uint64 returns the value of this snowflake as a uint64 type.

Argumenets:

None

Returns:

(uint64): The uint64 value of the snowflake.

func (*Snowflake) Unmarshal

func (S *Snowflake) Unmarshal(value interface{}) error

Unmarshal takes a value and inserts the relevant values into the parent instance of Snowflake.

Arguments:

value (interface{}): The value to unmarshal.

Returns:

(error): An error if one exists, nil otherwise.

Jump to

Keyboard shortcuts

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