Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶ added in v0.0.54
type Config struct {
// Session provides user session numbers in WebAssembly environments
Session userSessionNumber // e.g., userSessionNumber() string = "1","4","4000" etc.
// TimeProvider provides time utilities including nanosecond timestamps and date formatting
TimeProvider tinytime.TimeProvider // Provides UnixNano(), UnixSecondsToDate(), and UnixNanoToTime()
// contains filtered or unexported fields
}
Config holds the configuration and dependencies for a UnixID instance
type UnixID ¶
type UnixID struct {
// Config holds the external dependencies for the UnixID
*Config
// contains filtered or unexported fields
}
UnixID is the main struct for ID generation and handling It contains all configuration and state needed for ID generation
func NewUnixID ¶ added in v0.0.59
NewUnixID creates a new UnixID handler with appropriate configuration based on the runtime environment.
For WebAssembly environments (client-side): - Requires a userSessionNumber handler to be passed as a parameter - Creates IDs with format: "[timestamp].[user_number]" (e.g., "1624397134562544800.42") - No mutex is used as JavaScript is single-threaded
For non-WebAssembly environments (server-side): - Does not require any parameters - Creates IDs with format: "[timestamp]" (e.g., "1624397134562544800") - Uses a sync.Mutex for thread safety
IMPORTANT: When integrating with other libraries that also use sync.Mutex, you can pass an existing mutex as a parameter to avoid potential deadlocks. When an external mutex is provided, this library will use a no-op mutex internally to prevent deadlocks when GetNewID is called within a context that has already acquired the same mutex. This assumes that external synchronization is being handled by the caller.
Parameters:
- handlerUserSessionNumber: Optional userSessionNumber implementation (required for WebAssembly)
- sync.Mutex or *sync.Mutex: Optional mutex to use instead of creating a new one (server-side only)
Returns:
- A configured *UnixID instance
- An error if the configuration is invalid
Usage examples:
// Server-side usage:
idHandler, err := unixid.NewUnixID()
// WebAssembly usage:
type sessionHandler struct{}
func (sessionHandler) userSessionNumber() string { return "42" }
idHandler, err := unixid.NewUnixID(&sessionHandler{})
// Server-side usage with existing mutex to avoid deadlocks:
var mu sync.Mutex
idHandler, err := unixid.NewUnixID(&mu)
// With external mutex, when calling within a locked context:
var mu sync.Mutex
idHandler, err := unixid.NewUnixID(&mu)
mu.Lock()
defer mu.Unlock()
// This won't deadlock because NewUnixID uses a no-op mutex internally
// when an external mutex is provided
id := idHandler.GetNewID()
func (*UnixID) GetNewID ¶
GetNewID generates a new unique ID based on Unix nanosecond timestamp. In WebAssembly environments, this appends a user session number to the timestamp. In server environments, this returns just the Unix nanosecond timestamp. Returns a string representation of the unique ID.
func (*UnixID) Parse ¶ added in v0.2.9
Parse parses an ID string and extracts its components. It first validates the ID format, then extracts the timestamp and optional user number.
Parameters:
- id: The ID string to parse (e.g., "1624397134562544800" or "1624397134562544800.42")
Returns:
- timestamp: The timestamp portion as int64
- userNum: The user number portion as string (empty if not present)
- err: An error if the ID format is invalid or parsing fails
func (*UnixID) SetNewID ¶ added in v0.0.62
SetNewID sets a new unique ID value to various types of targets. It generates a new unique ID based on Unix nanosecond timestamp and assigns it to the provided target. This function can work with multiple target types including reflect.Value, string pointers, and byte slices.
In WebAssembly environments, IDs include a user session number as a suffix (e.g., "1624397134562544800.42"). In server environments, IDs are just the timestamp (e.g., "1624397134562544800").
Parameters:
- target: The target to receive the new ID. Can be:
- reflect.Value or *reflect.Value: For setting struct field values via reflection.
- *string: For setting a string variable directly.
- []byte: For appending the ID to a byte slice.
This function is thread-safe in server-side environments.
Examples:
// Setting a struct field using reflection v := reflect.ValueOf(&myStruct) elem := v.Elem() field := elem.Field(0) // Get field by index idHandler.SetNewID(field) // Setting a string variable var id string idHandler.SetNewID(&id) // Appending to a byte slice buf := make([]byte, 0, 64) idHandler.SetNewID(buf)
func (*UnixID) Validate ¶ added in v0.2.9
Validate validates the format of an ID string without parsing it. It handles IDs in both server format (just timestamp) and client format (timestamp.userNumber).
Parameters:
- id: The ID string to validate (e.g., "1624397134562544800" or "1624397134562544800.42")
Returns:
- error: nil if valid, error describing the issue if invalid
Validation rules:
- The ID must not be empty
- The ID must contain only digits and at most one decimal point
- The ID must not start or end with a decimal point
- The timestamp portion (before the decimal point) must be valid