utils

package
v0.19.0 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2025 License: Apache-2.0 Imports: 6 Imported by: 20

Documentation

Index

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

func ConvertToSeconds(timeStr string, opts ...Option) (uint64, error)

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.

func EnumSliceToStringSlice added in v0.19.0

func EnumSliceToStringSlice[T ~string](inputSlice []T) []string

EnumSliceToStringSlice is a generic function to convert a slice of any type T that has the underlying type 'string' to a slice of string. The constraint ~string allows T to be any type whose underlying type is string (like the enum types from the generated STACKIT SDK modules).

func Ptr

func Ptr[T any](v T) *T

Ptr Returns the pointer to any type T

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