snowflake

package module
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2018 License: GPL-3.0 Imports: 3 Imported by: 19

README

Snowflake for Discord

forthebadgeforthebadge

Health

Branch Build status Code climate Go Report Card Codacy
master CircleCI Maintainability Go Report Card Codacy Badge

Does not hold functionality to connect a snowflake service, but rather parsing the snowflakes for Discord only(!).

Usage:

import . "github.com/andersfylling/snowflake"

type DiscordRole struct {
    ID          Snowflake    `json:"id"`
    Name        string       `json:"name"`
    Managed     bool         `json:"managed"`
    Mentionable bool         `json:"mentionable"`
    Hoist       bool         `json:"hoist"`
    Color       int          `json:"color"`
    Position    int          `json:"position"`
    Permissions uint64       `json:"permissions"`
}

If you're creating an API that sends JSON to a multiple different language clients, some might not be able to process uint64, such as javascript. To support both uint64 and string use the JSON struct included:

import . "github.com/andersfylling/snowflake"

type DiscordRole struct {
    *SnowflakeJSON           `json:"snowflake"`
    Name        string       `json:"name"`
    Managed     bool         `json:"managed"`
    Mentionable bool         `json:"mentionable"`
    Hoist       bool         `json:"hoist"`
    Color       int          `json:"color"`
    Position    int          `json:"position"`
    Permissions uint64       `json:"permissions"`
}

This adds two fields: ID and IDStr. Where the first is of a snowflake.ID(uint64), and the second is a string. This creates the JSON format (IDs only. Where the dots represents the remaining DiscordRole fields):

{
    "snowflake": {
          "id": 74895735435643,
          "id_str": "74895735435643",
    },
    ...
}

Now an alternative is to send only the string version by adding ,string to the json tag. Which I would recommend instead:

import . "github.com/andersfylling/snowflake"

type DiscordRole struct {
    ID          Snowflake    `json:"id,string"`
    Name        string       `json:"name"`
    Managed     bool         `json:"managed"`
    Mentionable bool         `json:"mentionable"`
    Hoist       bool         `json:"hoist"`
    Color       int          `json:"color"`
    Position    int          `json:"position"`
    Permissions uint64       `json:"permissions"`
}

This does fulfill the twitter snowflake use case described here: https://developer.twitter.com/en/docs/basics/twitter-ids

Remember that Discord has a different epoch. So when using the Date function, this will only function for Discord applications.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Snowflake

type Snowflake uint64

Snowflake Snowflake Snowflake created by twitter

func GetSnowflake added in v1.1.0

func GetSnowflake(v interface{}) (s Snowflake, err error)

func NewSnowflake

func NewSnowflake(id uint64) Snowflake

NewID creates a new Snowflake Snowflake from a uint64.

func ParseSnowflakeString

func ParseSnowflakeString(v string) Snowflake

ParseSnowflakeString interprets a string with a decimal number.

Note that in contrast to ParseUint, this function assumes the given string is
always valid and thus will panic rather than return an error.
This should only be used on checks that can be done at compile time,
unless you want to trust other modules to returns valid data.

func ParseSnowflakeUint

func ParseSnowflakeUint(v string, base int) (Snowflake, error)

ParseUint converts a string and given base to a Snowflake

func (Snowflake) Date added in v1.3.0

func (s Snowflake) Date() time.Time

func (Snowflake) Empty

func (s Snowflake) Empty() bool

Empty since snowflake exists of several parts, including a timestamp,

I assume a valid snowflake Snowflake is never 0.

func (Snowflake) HexPrettyString

func (s Snowflake) HexPrettyString() string

HexPrettyString converts the Snowflake into a hexadecimal string with the hex prefix 0x

func (Snowflake) HexString

func (s Snowflake) HexString() string

HexString converts the Snowflake into a hexadecimal string

func (Snowflake) JSONStruct

func (s Snowflake) JSONStruct() *SnowflakeJSON

JSONStruct returns a struct that can be embedded in other structs.

This is useful if you have a API server, since js can't parse uint64.
Therefore there must a snowflake Snowflake string.

func (Snowflake) MarshalBinary

func (s Snowflake) MarshalBinary() (data []byte, err error)

MarshalBinary create a binary literal representation as a string

func (Snowflake) MarshalText

func (s Snowflake) MarshalText() (text []byte, err error)

func (Snowflake) String

func (s Snowflake) String() string

String returns the decimal representation of the snowflake Snowflake.

func (*Snowflake) UnmarshalBinary

func (s *Snowflake) UnmarshalBinary(text []byte) (err error)

func (*Snowflake) UnmarshalText

func (s *Snowflake) UnmarshalText(text []byte) (err error)

type SnowflakeJSON

type SnowflakeJSON struct {
	ID    Snowflake `json:"id"`
	IDStr string    `json:"id_str"`
}

JSON can be useful when sending the snowflake Snowflake by a json API

Jump to

Keyboard shortcuts

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