Documentation
¶
Overview ¶
Package nid implements named unique sortable identifiers.
It's designed to provide a way to work with unique identifiers that are more human-readable. The identifiers are sortable and can be used in the database. The package also provides a way to convert the identifiers to and from the JSON, Text, and database values.
Example ¶
Example demonstrates how to use the go.wamod.io/nid package.
package main import ( "fmt" "go.wamod.dev/nid" ) func main() { // Create a new [nid.Naming] for books bookIDN := nid.MustNaming("book") // Create one new [nid.NID] for bookID := bookIDN.New() fmt.Printf("Book ID: %s", bookID) }
Output:
Index ¶
- Variables
- func Compare(a, b NID) int
- func CompareBase(a, b Base) int
- func Sort(ids []NID)
- func SortBase(ids []Base)
- type Base
- func (base Base) Bytes() []byte
- func (base Base) Empty() bool
- func (base Base) MarshalJSON() ([]byte, error)
- func (base Base) MarshalText() ([]byte, error)
- func (base *Base) Scan(src any) (err error)
- func (base Base) String() string
- func (base Base) Time() time.Time
- func (base Base) UnixMilli() int64
- func (base *Base) UnmarshalJSON(src []byte) error
- func (base *Base) UnmarshalText(src []byte) error
- func (base Base) Value() (driver.Value, error)
- type NID
- func (id NID) Base() Base
- func (id NID) Empty() bool
- func (id NID) MarshalJSON() ([]byte, error)
- func (id NID) MarshalText() ([]byte, error)
- func (id NID) Name() string
- func (id *NID) Scan(src any) error
- func (id NID) String() string
- func (id *NID) UnmarshalJSON(src []byte) error
- func (id *NID) UnmarshalText(data []byte) error
- func (id NID) Value() (driver.Value, error)
- type Naming
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrFailedParse = fmt.Errorf("nid: failed to parse") ErrInvalidName = fmt.Errorf("nid: invalid name") )
Functions ¶
Types ¶
type Base ¶
type Base [baseLen]byte
Base of the NID with a time and random part. The time part is 8 bytes and the random part is 8 bytes. The total length is 16 bytes. The Base is sortable and unique.
func MustParseBase ¶
MustParseBase is a helper to parse Base. It panics if the base is invalid.
func ParseBaseBytes ¶
ParseBaseBytes parses the Base from the bytes.
func (Base) MarshalJSON ¶
MarshalJSON returns the JSON representation of the Base.
func (Base) MarshalText ¶
MarshalText returns the text representation of the Base.
func (*Base) UnmarshalJSON ¶
UnmarshalJSON parses the Base from the JSON.
func (*Base) UnmarshalText ¶
UnmarshalText parses the Base from the text.
type NID ¶
type NID struct {
// contains filtered or unexported fields
}
NID is a named identifier. It consists of the name and the base identifier that is sortable and unique.
func (NID) MarshalJSON ¶
MarshalJSON returns the JSON representation of the ID.
func (NID) MarshalText ¶
MarshalText returns the text representation of the ID.
func (NID) String ¶
String returns the string representation of the ID. The format is "<name>_<id>".
func (*NID) UnmarshalJSON ¶
UnmarshalJSON parses the ID from the JSON.
func (*NID) UnmarshalText ¶
UnmarshalText parses the ID from the text.
type Naming ¶
type Naming struct {
// contains filtered or unexported fields
}
Naming provides a way to create, update and validate the [NID]s.
func MustNaming ¶
MustNaming is a helper to create Namer from the name. It panics if the name is invalid.
func NewNaming ¶
NewNaming creates a new Naming from the name. It returns an error if the name is invalid. The name must be a non-empty snake_case string, e.g. "user" or "user_profile".