Documentation
¶
Overview ¶
Package util provides common utility functions shared across the codebase.
Index ¶
- func Contains[T comparable](s []T, v T) bool
- func Decode(r io.Reader, v any) error
- func Encode(w io.Writer, v any) error
- func Filter[T any](s []T, fn func(T) bool) []T
- func ForEach[T any](s []T, fn func(T))
- func FormatISO(t time.Time) string
- func FormatMillis(ms int64) string
- func GoWithRecovery(logger *slog.Logger, name string, fn func())
- func Keys[K comparable, V any](m map[K]V) []K
- func Map[T, R any](s []T, fn func(T) R) []R
- func Marshal(v any) ([]byte, error)
- func Merge[K comparable, V any](maps ...map[K]V) map[K]V
- func NowMillis() int64
- func ParseISO(s string) (time.Time, error)
- func ParseMillis(ms int64) time.Time
- func Reduce[T, R any](s []T, init R, fn func(R, T) R) R
- func Retry(ctx context.Context, fn func() error, opts ...RetryOption) error
- func SanitizeDSN(dsn string) string
- func Unmarshal(data []byte, v any) error
- func Validate(data []byte, v any) error
- func Values[K comparable, V any](m map[K]V) []V
- type RetryOption
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Contains ¶
func Contains[T comparable](s []T, v T) bool
Contains reports whether v is present in s.
func FormatMillis ¶
FormatMillis converts millisecond timestamp to RFC3339 string (UTC).
func GoWithRecovery ¶
GoWithRecovery launches fn in a new goroutine with panic recovery. If fn panics, the panic is logged with the goroutine name and stack trace instead of crashing the process.
func Map ¶
func Map[T, R any](s []T, fn func(T) R) []R
Map transforms each element using fn and returns a new slice.
func Merge ¶
func Merge[K comparable, V any](maps ...map[K]V) map[K]V
Merge combines multiple maps. Later maps override earlier ones for duplicate keys.
func ParseMillis ¶
ParseMillis converts millisecond timestamp to time.Time.
func Reduce ¶
func Reduce[T, R any](s []T, init R, fn func(R, T) R) R
Reduce reduces a slice to a single value. Parameter order: (slice, initial value, reducer function).
func Retry ¶
func Retry(ctx context.Context, fn func() error, opts ...RetryOption) error
Retry executes fn with exponential backoff retry logic. It respects context cancellation and returns immediately if the context is done.
func SanitizeDSN ¶
SanitizeDSN removes credentials (password) from a database connection string to prevent leakage in error messages and logs. Supports: - Redis URLs (redis://user:pass@host:port) - PostgreSQL DSNs (postgres://user:pass@host:port/db?params) - MySQL DSNs (user:pass@tcp(host:port)/db?params)
If the DSN cannot be parsed, a generic placeholder is returned to avoid accidentally exposing partial credential data.
func Unmarshal ¶
Unmarshal deserializes JSON bytes into v with strict mode (unknown fields rejected). Returns error for nil or empty data.
func Values ¶
func Values[K comparable, V any](m map[K]V) []V
Values returns all values from the map.
Types ¶
type RetryOption ¶
type RetryOption func(*retryConfig)
RetryOption configures retry behavior.
func WithBackoff ¶
func WithBackoff(d time.Duration) RetryOption
WithBackoff sets the initial backoff duration. Default: 1s.
func WithJitter ¶
func WithJitter(enabled bool) RetryOption
WithJitter enables or disables randomized jitter on backoff. Default: true.
func WithMaxBackoff ¶
func WithMaxBackoff(d time.Duration) RetryOption
WithMaxBackoff caps the backoff duration. Default: 30s.
func WithMaxRetries ¶
func WithMaxRetries(n int) RetryOption
WithMaxRetries sets the maximum number of retries. Default: 3.
func WithMultiplier ¶
func WithMultiplier(m float64) RetryOption
WithMultiplier sets the exponential backoff multiplier. Default: 2.0.