Documentation
¶
Index ¶
- func ValidateID(new_id_in string) (id int64, err error)
- type Config
- type UnixID
- func (u *UnixID) FieldType(tableName, fieldName string) (ID, PK bool)
- func (id *UnixID) GetNewID() (string, error)
- func (id *UnixID) SetValue(rv *reflect.Value, valueOut *string, sizeOut []byte) error
- func (u *UnixID) UnixNanoToStringDate(unixNanoStr string) (string, error)
- func (u UnixID) UnixSecondsToTime(input any) string
- type UserSessionNumber
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ValidateID ¶ added in v0.0.57
ValidateID validates and parses a Unix timestamp ID string. It handles IDs in both server format (just timestamp) and client format (timestamp.userNumber). This function extracts the timestamp portion from the ID and returns it as an int64.
Parameters:
- new_id_in: The ID string to validate (e.g., "1624397134562544800" or "1624397134562544800.42")
Returns:
- id: The timestamp portion of the ID as an int64 value
- err: An error if the ID format is invalid
Validation rules:
- The ID must contain only digits and at most one decimal point
- The timestamp portion (before the decimal point) must be a valid int64
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.
// 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
Parameters:
- handlerUserSessionNumber: Optional UserSessionNumber implementation (required for WebAssembly)
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, error) { return "42", nil }
idHandler, err := unixid.NewUnixID(&sessionHandler{})
func (*UnixID) FieldType ¶ added in v0.0.55
FieldType determines if a field is an ID field and/or a primary key field. This function analyzes field names to identify ID fields and primary keys based on naming conventions.
Parameters:
- tableName: The name of the table or entity that the field belongs to
- fieldName: The name of the field to analyze
Returns:
- ID: true if the field is an ID field (starts with "id")
- PK: true if the field is a primary key (is named "id" or matches the pattern "id{tableName}" or "id_{tableName}")
Examples:
- FieldType("user", "id") returns (true, true)
- FieldType("user", "iduser") returns (true, true)
- FieldType("user", "id_user") returns (true, true)
- FieldType("user", "idaddress") returns (true, false)
func (*UnixID) GetNewID ¶
GetNewID generates a new unique ID based on Unix nanosecond timestamp. In server environments, this returns just the Unix nanosecond timestamp value. The method is thread-safe and handles concurrent access through a mutex lock. Returns a string representation of the unique ID.
func (*UnixID) SetValue ¶ added in v0.0.55
SetValue sets a unique ID value to a struct field using reflection. This is used internally to populate struct fields with unique IDs. Parameters:
- rv: A reflect.Value pointer to the struct field that will receive the ID
- valueOut: A pointer to a string that will store the generated ID
- sizeOut: A byte slice that will track the size of the generated value
Returns nil on success or an error if the operation fails.
func (*UnixID) UnixNanoToStringDate ¶ added in v0.0.54
UnixNanoToStringDate converts a Unix nanosecond timestamp ID to a human-readable date string. This function accepts a string representation of a Unix nanosecond timestamp (with or without a user number suffix) and returns a formatted date string in the format "2006-01-02 15:04" (year-month-day hour:minute).
Parameters:
- unixNanoStr: String representation of a Unix timestamp in nanoseconds (e.g. "1624397134562544800" or "1624397134562544800.42")
Returns:
- A formatted date string (e.g. "2021-06-22 15:32")
- An error if the input is invalid or the conversion fails
Example:
dateStr, err := handler.UnixNanoToStringDate("1624397134562544800")
if err != nil {
// handle error
}
fmt.Println(dateStr) // e.g. "2021-06-22 15:32"
func (UnixID) UnixSecondsToTime ¶ added in v0.0.60
UnixSecondsToTime converts a Unix timestamp in seconds to a formatted time string. Format: "15:04:05" (hour:minute:second) It accepts a parameter of type any and attempts to convert it to an int64 Unix timestamp. eg: 1624397134 -> "15:32:14" supported types: int64, int, float64, string
type UserSessionNumber ¶ added in v0.0.61
type UserSessionNumber interface {
// UserSessionNumber returns a unique identifier for the current user session
// e.g., "1" or "2" or "34" or "400" etc.
UserSessionNumber() (number string, err error)
}
UserSessionNumber is an interface to obtain the current user's session number This is primarily used in WebAssembly environments to uniquely identify client sessions