Documentation
¶
Index ¶
- Constants
- Variables
- type DeconstructedSnowflake
- type ID
- func Getenv(key string) ID
- func LookupEnv(key string) (ID, bool)
- func MustParse(str string) ID
- func New(timestamp time.Time, workerID, processID uint8, sequence uint16) ID
- func NewFromTimestamp(timestamp time.Time) ID
- func NewRange(start, end time.Time) (min, max ID)
- func Parse(str string) (ID, error)
- func ParseStrict(str string) (ID, error)
- func (id ID) After(other ID) bool
- func (id ID) Before(other ID) bool
- func (id ID) Compare(other ID) int
- func (id ID) Deconstruct() DeconstructedSnowflake
- func (id ID) Equal(other ID) bool
- func (id ID) IsBetween(start, end ID) bool
- func (id ID) IsValid() bool
- func (id ID) IsZero() bool
- func (id ID) MarshalJSON() ([]byte, error)
- func (id ID) ProcessID() uint8
- func (id ID) Sequence() uint16
- func (id ID) String() string
- func (id ID) Time() time.Time
- func (id *ID) UnmarshalJSON(data []byte) error
- func (id ID) Valid() bool
- func (id ID) WorkerID() uint8
Constants ¶
const ( // Epoch is the custom epoch time (Unix time in milliseconds) used as the starting point // for the timestamp component of the snowflake ID (January 1, 2015 00:00:00.000 UTC). Epoch = 1420070400000 // TimestampShift is the number of bits to shift the timestamp component. TimestampShift = 22 // WorkerIDShift is the number of bits to shift the worker ID component. WorkerIDShift = 17 // ProcessIDShift is the number of bits to shift the process ID component. ProcessIDShift = 12 // WorkerIDMask is the bitmask for the 5-bit worker ID component. WorkerIDMask = 0x1F // ProcessIDMask is the bitmask for the 5-bit process ID component. ProcessIDMask = 0x1F // SequenceMask is the bitmask for the 12-bit sequence component. SequenceMask = 0xFFF )
const ( // MinID is the smallest possible snowflake ID (0). MinID = ID(0) // MaxID is the largest possible snowflake ID. MaxID = ID(^uint64(0)) // FirstDiscordSnowflake is the ID of the first-ever Discord snowflake. FirstDiscordSnowflake = ID(175928847299117063) )
Variables ¶
var ( // ErrInvalidSnowflake is returned when a string contains invalid characters during parsing. ErrInvalidSnowflake = errors.New("invalid snowflake") // ErrOutOfRange is returned when a parsed snowflake ID is outside the valid time range. ErrOutOfRange = errors.New("snowflake out of valid range") // AllowUnquoted determines if JSON unmarshaling should allow unquoted numeric strings. AllowUnquoted = false )
Functions ¶
This section is empty.
Types ¶
type DeconstructedSnowflake ¶
type DeconstructedSnowflake struct {
Time time.Time
WorkerID uint8
ProcessID uint8
Sequence uint16
}
DeconstructedSnowflake contains the properties used by Discord for each snowflake ID.
type ID ¶
type ID uint64
ID is a 64-bit unsigned integer representing a unique snowflake identifier.
func Getenv ¶
Getenv returns the value of the environment variable named by the key and parses it as a snowflake. If the key is not found or the value is unparsable, it returns ID(0).
func LookupEnv ¶
LookupEnv returns the value of the environment variable named by the key and parses it as a snowflake. It returns the ID and a boolean indicating if the key was found in the environment.
func New ¶
New creates a new snowflake ID from all components: timestamp, worker ID, process ID, and sequence.
func NewFromTimestamp ¶
NewFromTimestamp creates a new snowflake ID from timestamp with worker, process, and sequence set to 0.
func NewRange ¶
NewRange creates min and max snowflake IDs for a time range (inclusive). The max ID is generated by setting the worker, process, and sequence bits to their maximum value (0x3FFFFF).
func Parse ¶
Parse converts a string representation of a snowflake ID into an ID type. It accepts "null" as ID(0).
func ParseStrict ¶
ParseStrict parses and validates a snowflake is within Discord's valid range. It returns ErrOutOfRange if the ID's time component is outside expected boundaries.
func (ID) After ¶
After returns true if this snowflake ID was created after the other (i.e., numerically larger).
func (ID) Before ¶
Before returns true if this snowflake ID was created before the other (i.e., numerically smaller).
func (ID) Deconstruct ¶
func (id ID) Deconstruct() DeconstructedSnowflake
Deconstruct returns DeconstructedSnowflake with all components of the ID.
func (ID) IsValid ¶
IsValid returns true if the snowflake is within Discord's theoretical valid range. The range check ensures the ID is not before the Epoch and not unrealistically far into the future (24 hours from now).
func (ID) MarshalJSON ¶
MarshalJSON marshals the snowflake ID into a JSON string. Performance (~20ns, 1 heap allocation for the return slice). Note: Benchmarks may show 0 allocs due to compiler optimizations when the result is discarded, but real-world usage will have 1 allocation for the returned []byte.
func (ID) ProcessID ¶
ProcessID returns the id of the process the snowflake was created on, using a 5-bit mask.
func (ID) Time ¶
Time returns the time.Time the snowflake was created by extracting the timestamp component. It returns an empty time.Time{} for ID(0).
func (*ID) UnmarshalJSON ¶
UnmarshalJSON unmarshals the snowflake ID from a JSON string. It handles quoted strings and optionally unquoted strings if AllowUnquoted is true. It unmarshals "null" or "0" as ID(0).