snowflake

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2022 License: MIT Imports: 7 Imported by: 0

README

snowflake

GoDoc

Package snowflake provides a simple snowflake ID generator along with interface implementations to make it easy to use with database/sql and encoding/json.

Functions

func Init

func Init(e time.Time, w, p int)

Init initializes the Snowflake generator. This MUST be called before any calls to Generate.

Types

type NullSnowflake

type NullSnowflake struct { ... }

NullSnowflake is a nullable Snowflake

func NewNullSnowflake

func NewNullSnowflake(s Snowflake, valid bool) NullSnowflake

NewNullSnowflake creates a new NullSnowflake

type Snowflake

type Snowflake uint64

Snowflake represents a single Snowflake ID.

func Generate

func Generate() Snowflake

Generate generates a new Snowflake. This function is thread-safe.

package main

import (
	"fmt"
	"time"

	"github.com/Project-Sparrow/snowflake"
)

func main() {
	epoch := time.Date(2020, 1, 1, 1, 0, 0, 0, time.UTC)

	snowflake.Init(epoch, 1, 1)

	fmt.Println(snowflake.Generate())
}

func SnowflakeFromString

func SnowflakeFromString(s string) (Snowflake, error)

SnowflakeFromString attempts to parse a Snowflake from a string.

Documentation

Overview

Package snowflake provides a simple snowflake ID generator along with interface implementations to make it easy to use with database/sql and encoding/json.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Init

func Init(e time.Time, w, p int)

Init initializes the Snowflake generator. This MUST be called before any calls to Generate.

Types

type NullSnowflake added in v1.0.2

type NullSnowflake struct {
	Snowflake Snowflake
	Valid     bool
}

NullSnowflake is a nullable Snowflake

func NewNullSnowflake added in v1.0.2

func NewNullSnowflake(s Snowflake, valid bool) NullSnowflake

NewNullSnowflake creates a new NullSnowflake

func NullSnowflakeFromPtr added in v1.0.3

func NullSnowflakeFromPtr(s *Snowflake) NullSnowflake

NullSnowflakeFromPtr creates a new NullSnowflake from a Snowflake pointer

func NullSnowflakeFromStringPtr added in v1.0.3

func NullSnowflakeFromStringPtr(s *string) NullSnowflake

NullSnowflakeFromStringPtr creates a new NullSnowflake from a string pointer Always returns a NullSnowflake, will be invalid if the string cannot be converted to an int

func (NullSnowflake) MarshalJSON added in v1.0.2

func (s NullSnowflake) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (*NullSnowflake) Scan added in v1.0.2

func (s *NullSnowflake) Scan(value any) error

Scan implements sql.Scanner interface

func (*NullSnowflake) UnmarshalJSON added in v1.0.2

func (s *NullSnowflake) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler.

func (NullSnowflake) Value added in v1.0.2

func (s NullSnowflake) Value() (driver.Value, error)

Value implements driver.Valuer interface

func (NullSnowflake) ValueOrZero added in v1.0.2

func (s NullSnowflake) ValueOrZero() Snowflake

ValueOrZero returns the inner value if valid, otherwise zero.

type Snowflake

type Snowflake uint64

Snowflake represents a single Snowflake ID.

func Generate

func Generate() Snowflake

Generate generates a new Snowflake. This function is thread-safe.

Example
package main

import (
	"fmt"
	"time"

	"github.com/Project-Sparrow/snowflake"
)

func main() {
	epoch := time.Date(2020, 1, 1, 1, 0, 0, 0, time.UTC)

	snowflake.Init(epoch, 1, 1)

	fmt.Println(snowflake.Generate())
}
Output:

func SnowflakeFromString

func SnowflakeFromString(s string) (Snowflake, error)

SnowflakeFromString attempts to parse a Snowflake from a string.

func (Snowflake) CreatedAt

func (s Snowflake) CreatedAt() time.Time

CreatedAt returns the time component of the Snowflake as a time.Time

func (Snowflake) MarshalJSON

func (s Snowflake) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler interface

func (*Snowflake) Scan

func (s *Snowflake) Scan(value interface{}) error

Scan implements sql.Scanner interface

func (Snowflake) String

func (s Snowflake) String() string

String implements fmt.Stringer interface

func (*Snowflake) UnmarshalJSON

func (s *Snowflake) UnmarshalJSON(bytes []byte) error

MarshalJSON implements json.Unmarshaler interface

func (Snowflake) Value

func (s Snowflake) Value() (driver.Value, error)

Value implements driver.Valuer interface

Jump to

Keyboard shortcuts

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