Documentation
¶
Index ¶
- func Contains[T comparable](slice []T, element T) bool
- func ConvertToSeconds(timeStr string, opts ...Option) (uint64, error)
- func Ptr[T any](v T) *T
- type CalculationError
- type CalculationErrorType
- type DurationConverter
- type Option
- type ValidationError
- func NewAboveMaximumError(value, maximum uint64) *ValidationError
- func NewBelowMinimumError(value, minimum uint64) *ValidationError
- func NewInvalidFormatError(input, reason string) *ValidationError
- func NewInvalidUnitError(unit string, validUnits []string) *ValidationError
- func NewInvalidValueError(input, reason string) *ValidationError
- func NewValidationError(errorType ValidationErrorType, input, reason string, context map[string]any) *ValidationError
- type ValidationErrorType
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Contains ¶
func Contains[T comparable](slice []T, element T) bool
func ConvertToSeconds ¶ added in v0.18.0
ConvertToSeconds converts a time string in the form of <value><unit> (e.g., "30m", "1h") into a total number of seconds as a uint64.
The function uses a default set of units:
- "s": seconds
- "m": minutes
- "h": hours
- "d": days (24 hours)
- "M": months (calendar-aware)
Optional configurations can be provided using the Option type, such as setting minimum/maximum second boundaries (WithMinSeconds, WithMaxSeconds) or providing a custom set of units (WithUnits).
It returns an error if the format is invalid, the unit is unsupported, or if the calculated value violates any provided boundaries.
Types ¶
type CalculationError ¶ added in v0.18.0
type CalculationError struct {
Type CalculationErrorType // The specific type of calculation error
Value uint64 // The value that caused the error
Reason string // Human-readable reason
Context map[string]any // Additional context (multiplier, operation, etc.)
}
CalculationError represents errors that occur during duration calculations.
func NewCalculationError ¶ added in v0.18.0
func NewCalculationError(errorType CalculationErrorType, value uint64, reason string, context map[string]any) *CalculationError
NewCalculationError creates a new CalculationError with the specified type and details.
func (*CalculationError) Error ¶ added in v0.18.0
func (e *CalculationError) Error() string
func (*CalculationError) Is ¶ added in v0.18.0
func (e *CalculationError) Is(target error) bool
Is implements error matching for errors.Is()
type CalculationErrorType ¶ added in v0.18.0
type CalculationErrorType string
const ( CalculationErrorOutOfBounds CalculationErrorType = "out_of_bounds" CalculationErrorNegativeResult CalculationErrorType = "negative_result" CalculationErrorNegativeMultiplier CalculationErrorType = "negative_multiplier" )
type DurationConverter ¶ added in v0.18.0
type DurationConverter interface {
// ToDuration converts a numeric value into a time.Duration.
//
// The function takes the following parameters:
// - value: The numeric value to convert (e.g., 30 for 30 days).
// - now: The reference time for calendar-based calculations. It can be
// ignored by implementations that are not calendar-dependent.
//
// It returns the calculated time.Duration and a nil error on success. On
// failure, it returns an error, for example if the value is too large to
// be processed.
ToDuration(value uint64, now time.Time) (time.Duration, error)
}
DurationConverter defines the interface for converting a numeric value into a time.Duration. This abstraction supports both fixed-rate conversions (like seconds to minutes) and complex calendar-aware calculations (like adding months).
type Option ¶ added in v0.18.0
type Option func(*converterConfig)
An Option configures a ConvertToSeconds call.
func WithMaxSeconds ¶ added in v0.18.0
WithMaxSeconds sets a maximum duration in seconds. A value of 0 means no limit.
func WithMinSeconds ¶ added in v0.18.0
WithMinSeconds sets a minimum duration in seconds. A value of 0 means no limit.
func WithNow ¶ added in v0.18.0
WithNow sets the current time to be used by the DurationConverter interface's ToDuration function. Useful for determenistic when testing calendar-dependent implementations of DurationConverter.
func WithUnits ¶ added in v0.18.0
func WithUnits(units map[string]DurationConverter) Option
WithUnits provides a custom map of duration converters.
type ValidationError ¶ added in v0.18.0
type ValidationError struct {
Type ValidationErrorType // The specific type of validation error
Input string // The invalid input value
Reason string // Human-readable reason for the error
Context map[string]any // Additional context (min/max values, valid units, etc.)
}
ValidationError represents errors that occur during input validation. It uses a Type field to distinguish between different validation failures.
func NewAboveMaximumError ¶ added in v0.18.0
func NewAboveMaximumError(value, maximum uint64) *ValidationError
func NewBelowMinimumError ¶ added in v0.18.0
func NewBelowMinimumError(value, minimum uint64) *ValidationError
func NewInvalidFormatError ¶ added in v0.18.0
func NewInvalidFormatError(input, reason string) *ValidationError
Constructors for common error cases
func NewInvalidUnitError ¶ added in v0.18.0
func NewInvalidUnitError(unit string, validUnits []string) *ValidationError
func NewInvalidValueError ¶ added in v0.18.0
func NewInvalidValueError(input, reason string) *ValidationError
func NewValidationError ¶ added in v0.18.0
func NewValidationError(errorType ValidationErrorType, input, reason string, context map[string]any) *ValidationError
============================================================================= Public Functions ============================================================================= NewValidationError creates a new ValidationError with the specified type and details.
func (*ValidationError) Error ¶ added in v0.18.0
func (e *ValidationError) Error() string
func (*ValidationError) Is ¶ added in v0.18.0
func (e *ValidationError) Is(target error) bool
Is implements error matching for errors.Is()
type ValidationErrorType ¶ added in v0.18.0
type ValidationErrorType string
const ( ValidationErrorInvalidFormat ValidationErrorType = "invalid_format" ValidationErrorInvalidValue ValidationErrorType = "invalid_value" ValidationErrorInvalidUnit ValidationErrorType = "invalid_unit" ValidationErrorBelowMinimum ValidationErrorType = "below_minimum" ValidationErrorAboveMaximum ValidationErrorType = "above_maximum" )