util

package
v0.0.0-...-5ed4770 Latest Latest
Warning

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

Go to latest
Published: Jun 30, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package util provides common utility functions shared across the codebase.

Index

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 Decode

func Decode(r io.Reader, v any) error

Decode reads JSON from r into v with strict mode (unknown fields rejected).

func Encode

func Encode(w io.Writer, v any) error

Encode writes v as JSON to w.

func Filter

func Filter[T any](s []T, fn func(T) bool) []T

Filter returns a new slice containing only elements where fn returns true.

func ForEach

func ForEach[T any](s []T, fn func(T))

ForEach applies fn to each element in s.

func FormatISO

func FormatISO(t time.Time) string

FormatISO converts time.Time to RFC3339 string (UTC).

func FormatMillis

func FormatMillis(ms int64) string

FormatMillis converts millisecond timestamp to RFC3339 string (UTC).

func GoWithRecovery

func GoWithRecovery(logger *slog.Logger, name string, fn func())

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 Keys

func Keys[K comparable, V any](m map[K]V) []K

Keys returns all keys from the map.

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 Marshal

func Marshal(v any) ([]byte, error)

Marshal serializes v to JSON bytes.

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 NowMillis

func NowMillis() int64

NowMillis returns current time as millisecond timestamp.

func ParseISO

func ParseISO(s string) (time.Time, error)

ParseISO parses RFC3339 string to time.Time.

func ParseMillis

func ParseMillis(ms int64) time.Time

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

func SanitizeDSN(dsn string) string

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

func Unmarshal(data []byte, v any) error

Unmarshal deserializes JSON bytes into v with strict mode (unknown fields rejected). Returns error for nil or empty data.

func Validate

func Validate(data []byte, v any) error

Validate deserializes JSON bytes into v and validates struct tags.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL