identifier

package
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package identifier provides a unified interface for generating and validating unique identifiers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateNumber added in v1.1.0

func GenerateNumber() int64

GenerateNumber generates a number ID using the configured default provider.

func GenerateString added in v1.1.0

func GenerateString() string

GenerateString generates a string ID using the configured default provider.

func Register

func Register(p Provider)

Register registers a provider, overriding any existing provider with the same name.

func SetDefaultNumber added in v1.1.0

func SetDefaultNumber(name string)

SetDefaultNumber sets the global default for number-based identifiers.

func SetDefaultString added in v1.1.0

func SetDefaultString(name string)

SetDefaultString sets the global default for string-based identifiers.

Types

type Generator

type Generator[T ~int64 | ~string] interface {
	Identifier
	// Generate creates a new identifier of type T.
	Generate() T
	// Validate checks if the provided value is a valid identifier of type T.
	Validate(T) bool
}

Generator defines a unified, generic interface for generating identifiers of a specific type. This is the interface that consuming code will typically interact with after initialization.

func Get

func Get[T ~string | ~int64](name string) Generator[T]

Get retrieves a typed generator by name. It returns nil if no provider with the given name is registered.

type Identifier

type Identifier interface {
	Name() string // Name returns the name of the identifier.
	Size() int    // Size returns the size of the identifier in bits.
}

Identifier defines the basic interface for all identifier types.

type Provider

type Provider interface {
	Identifier
	// AsString returns a string-based generator. Returns nil if not supported.
	AsString() Generator[string]
	// AsNumber returns a number-based generator. Returns nil if not supported.
	AsNumber() Generator[int64]
}

Provider is an interface for an algorithm that can provide generators for different types. A single provider can vend either a string or a number generator, or both.

Jump to

Keyboard shortcuts

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