Documentation
¶
Index ¶
- Variables
- type ID
- func (id ID) Bytes() []byte
- func (ID) GormDataType() string
- func (id ID) Int64Value() (pgtype.Int8, error)
- func (id ID) IsOfType(typ byte) (bool, error)
- func (id ID) IsTyped() bool
- func (id ID) MarshalJSON() ([]byte, error)
- func (id ID) MarshalText() ([]byte, error)
- func (id *ID) Scan(value any) error
- func (id *ID) ScanInt64(v pgtype.Int8) error
- func (id ID) String() string
- func (id ID) Time() time.Time
- func (id ID) Type() (byte, error)
- func (id ID) Uint64() uint64
- func (id *ID) UnmarshalJSON(data []byte) error
- func (id *ID) UnmarshalText(text []byte) error
- func (id ID) Value() (driver.Value, error)
- func (id ID) Version() int
Constants ¶
This section is empty.
Variables ¶
var ErrInvalidType = fmt.Errorf("invalid type id: must be less than or equal to %d", v1TypeSize)
var ErrUntyped = fmt.Errorf("no type id is embedded")
Functions ¶
This section is empty.
Types ¶
type ID ¶
type ID struct {
// contains filtered or unexported fields
}
ID is a 64-bit (8-byte) value intended to be
- URL-Friendly; short and unobtrusive in its default unpadded base32 string encoding
- temporally sortable with strong index locality
- fast-enough and unique-enough for most use cases
Field Definitions
0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | time_high | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | time_low |ver|t| rand | type or rand| rand | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Field Descriptions:
- Timestamp (41 bits): The most significant 41 bits represent a millisecond-precision timestamp The allowed timestamp range is 2025-01-01 00:00:00 - 2094-09-07 15:47:35
- Version (2 bits): Bits 41-42 are reserved for versioning. v1 is `01`
- Type Flag (1 bit): Bit 43 serves as a boolean flag. If set, the "Type/Rand" field is an embedded type identifier.
- Random (4 bits): The remaining 4 bits of the 6th byte are populated with pseudo-random data.
- Type/Random (7 bits): If the Type Flag is set, this field contains the Type Identifier. Otherwise, it is populated with pseudo-random data.
- Random (9 bits): The remaining byte is dedicated to pseudo-random data to _reasonably_ ensure uniqueness.
String Format
The string format is base32 with no padding. Canonically the string is lowercased. This decision is purely for aesthetics, but the parser is case-insensitive and will accept uppercase base32 strings.
func FromString ¶
FromString parses a smolid.ID from a string. While the canonical representation is all-lowercase, the parser is case-insensitive and will accept uppercase or mixed case without problems.
func Must ¶
Must is a convenience function that panics if the given error is not nil. Useful for testing or scenarios where you know fully that an ID is valid, but don't want to deal with ignoring the error.
Stolen from gofrs/uuid
func NewWithType ¶
NewWithType returns a new smolid.ID v1 with the given type identifier embedded into the ID.
func (ID) GormDataType ¶
GormDataType returns the data type for GORM.
func (ID) Int64Value ¶
Int64Value implements the pgtype.Int8Valuer interface (PostgreSQL BIGINT).
func (ID) IsOfType ¶ added in v1.0.1
IsOfType returns true if the ID is typed and matches the given type identifier.It will return an error if the ID was not created by NewWithType.
func (ID) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface for ID.
func (ID) MarshalText ¶
MarshalText implements the encoding.TextMarshaler interface for ID.
func (ID) Type ¶
Type returns the type identifier embedded in the ID, if any. It will return an error if the ID was not created by NewWithType.
func (ID) Uint64 ¶ added in v1.0.1
Uint64 returns the raw 64-bit integer representation of the ID. Use this instead of Int64Value for most cases. The Int64Value method is provided for compatibility with the pgtype.Int8Valuer interface.
func (*ID) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface for ID.
func (*ID) UnmarshalText ¶
UnmarshalText implements the encoding.TextUnmarshaler interface for ID.