sid

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2024 License: MIT Imports: 7 Imported by: 0

README

SID

The main purpose of SID is to seamlessly convert between a JSON-formatted string representation of an ID and its uint64 counterpart when interacting with a database.

Installation

You can install SID using go get:

go get -u -v github.com/deloz/sid

Usage

Here's a basic example of how to use SID:

package main

import (
	"encoding/json"
	"fmt"

	"github.com/deloz/sid"
)

func main() {
	// Create a new ID
	id := sid.New(18283195028008204)

	// Print the ID as a string
	fmt.Println("ID:", id.String())

	// Check if the ID is zero
	fmt.Println("IsZero:", id.IsZero())

	// Marshal the ID to JSON
	jsonData, _ := json.Marshal(id)
	fmt.Println("JSON:", string(jsonData))

	// Unmarshal the ID from JSON
	var newID sid.ID
	_ = json.Unmarshal(jsonData, &newID)
	fmt.Println("Unmarshaled ID:", newID)
}

Features

  • Create IDs from uint64, int64, and string representations.
  • Marshal and unmarshal IDs to/from JSON and text.
  • Compare IDs.
  • Sort slices of IDs.

Contributing

Contributions are welcome! If you find a bug or want to propose a new feature, feel free to open an issue or submit a pull request.

License

SID is licensed under the MIT License. See LICENSE for details.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (

	// Zero is the zero value of the ID type.
	Zero = New(zero)
)

Functions

This section is empty.

Types

type ID

type ID uint64

ID represents a unique identifier. It is a wrapper around an uint64.

func New

func New(v uint64) ID

New creates a new ID from an uint64.

func NewFromInt64

func NewFromInt64(v int64) ID

NewFromInt64 creates a new ID from an int64.

func NewFromString

func NewFromString(s string) (ID, error)

NewFromString creates a new ID from a string.

func NewFromUint64

func NewFromUint64(v uint64) ID

NewFromUint64 creates a new ID from an uint64.

func (ID) Cmp

func (id ID) Cmp(v ID) int

Cmp compares the numbers represented by id and v and returns: -1 if id < v 0 if id == v +1 if id > v

func (ID) Compare

func (id ID) Compare(v ID) int

Compare compares the numbers represented by id and v and returns: -1 if id < v 0 if id == v +1 if id > v

func (ID) Equal

func (id ID) Equal(v ID) bool

Equal returns true if id is equal to v.

func (ID) GreaterThan

func (id ID) GreaterThan(v ID) bool

GreaterThan returns true if id is greater than v.

func (ID) GreaterThanOrEqual

func (id ID) GreaterThanOrEqual(v ID) bool

GreaterThanOrEqual returns true if id is greater than or equal to v.

func (ID) IsZero

func (id ID) IsZero() bool

IsZero returns true if the ID is the zero value.

func (ID) LessThan

func (id ID) LessThan(v ID) bool

LessThan returns true if id is less than i.

func (ID) LessThanOrEqual

func (id ID) LessThanOrEqual(v ID) bool

LessThanOrEqual returns true if id is less than or equal to v.

func (ID) MarshalJSON

func (id ID) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (ID) MarshalText

func (id ID) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface.

func (*ID) Scan

func (id *ID) Scan(value any) error

Scan implements the sql.Scanner interface.

func (ID) String

func (id ID) String() string

String returns the ID as a string.

func (ID) Uint64

func (id ID) Uint64() uint64

Uint64 returns the ID as a uint64.

func (*ID) UnmarshalJSON

func (id *ID) UnmarshalJSON(b []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

func (*ID) UnmarshalText

func (id *ID) UnmarshalText(b []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface.

func (ID) Value

func (id ID) Value() (driver.Value, error)

Value implements the driver.Valuer interface.

type IDs

type IDs []ID

IDs implements sort.Interface for converting a slice of ID.

func (IDs) Len

func (x IDs) Len() int

func (IDs) Less

func (x IDs) Less(i, j int) bool

func (IDs) Swap

func (x IDs) Swap(i, j int)

Jump to

Keyboard shortcuts

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