utils

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package utils provides common utility functions shared across all service layer services

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildURL

func BuildURL(base string, path string, params map[string]string) string

BuildURL constructs a URL from base and path components

func Coalesce

func Coalesce(strs ...string) string

Coalesce returns the first non-empty string

func Contains

func Contains(slice []string, target string) bool

Contains checks if a slice contains a string

func ContainsAny

func ContainsAny(slice []string, targets []string) bool

ContainsAny checks if a slice contains any of the targets

func Deref

func Deref[T any](p *T) T

Deref returns the value pointed to, or zero value if nil

func DerefDefault

func DerefDefault[T any](p *T, defaultVal T) T

DerefDefault returns the value pointed to, or default if nil

func Filter

func Filter(slice []string, predicate func(string) bool) []string

Filter filters a slice based on a predicate

func FormatDuration

func FormatDuration(d time.Duration) string

FormatDuration formats a duration in a human-readable way

func GetEnv

func GetEnv(key, defaultValue string) string

GetEnv retrieves an environment variable with optional default

func GetEnvBool

func GetEnvBool(key string, defaultValue bool) bool

GetEnvBool retrieves a boolean environment variable

func GetEnvInt

func GetEnvInt(key string, defaultValue int) int

GetEnvInt retrieves an integer environment variable

func GetEnvOptional

func GetEnvOptional(key string) string

GetEnvOptional retrieves an environment variable without default

func GoSafeGo

func GoSafeGo(fn func())

GoSafeGo starts a goroutine with default panic recovery (logs error)

func IsEmpty

func IsEmpty(s string) bool

IsEmpty checks if a string is empty or whitespace-only

func JSONMarshal

func JSONMarshal(v interface{}) string

JSONMarshal converts an interface to JSON string with error handling

func JSONMarshalIndent

func JSONMarshalIndent(v interface{}) string

JSONMarshalIndent converts an interface to indented JSON string

func JSONParse

func JSONParse(jsonStr string) (interface{}, error)

JSONParse parses JSON string into an interface

func JoinPath

func JoinPath(parts ...string) string

JoinPath joins path components with proper separators

func Map

func Map(slice []string, fn func(string) string) []string

Map applies a function to all elements of a slice

func MapKeys

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

MapKeys extracts keys from a map as a slice

func MapValues

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

MapValues extracts values from a map as a slice

func MergeMaps

func MergeMaps[K comparable, V any](maps ...map[K]V) map[K]V

MergeMaps merges multiple maps, later maps override earlier ones

func Must

func Must[T any](v T, err error) T

Must panics if error is not nil, otherwise returns value

func MustJSONParse

func MustJSONParse(jsonStr string) interface{}

MustJSONParse parses JSON string or panics

func MustParseDuration

func MustParseDuration(s string) time.Duration

MustParseDuration parses a duration string or panics

func MustRetry

func MustRetry(fn func() error, opts ...RetryOpts)

MustRetry executes a function until it succeeds or panics

func NewWrapError

func NewWrapError(message string, err error) error

NewWrapError creates a new wrapped error

func Now

func Now() string

Now returns the current time as a string

func NowFormatted

func NowFormatted(format string) string

NowFormatted returns current time in specified format

func ParseDuration

func ParseDuration(s string) (time.Duration, error)

ParseDuration parses a duration string (e.g., "1h", "30m", "500ms")

func Ptr

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

Ptr returns a pointer to the given value

func PtrZero

func PtrZero[T comparable](v T) *T

PtrZero returns a nil pointer if value is zero, otherwise pointer to value

func Retry

func Retry(fn func() error, opts ...RetryOpts) error

Retry executes a function until it succeeds or max attempts reached

func SafeGo

func SafeGo(fn func(), recoveryFn func(error))

SafeGo starts a goroutine that recovers from panics

func SliceToMap

func SliceToMap[T any, K comparable](slice []T, keyFn func(T) K) map[K]T

SliceToMap converts a slice to map using key function

func SplitTrim

func SplitTrim(s, delimiter string) []string

SplitTrim splits a string by delimiter and trims each part

func ToBool

func ToBool(v interface{}, defaultValue bool) bool

ToBool converts various types to bool

func ToInt

func ToInt(v interface{}, defaultValue int) int

ToInt converts various types to int

func ToSlice

func ToSlice(s string) []string

ToSlice converts a single string to a slice

func ToString

func ToString(v interface{}) string

ToString converts various types to string

func TrimEmpty

func TrimEmpty(strs []string) []string

TrimEmpty removes all whitespace-only strings from a slice

func Truncate

func Truncate(s string, maxLen int) string

Truncate truncates a string to max length, adding "..." if needed

func Unique

func Unique(slice []string) []string

Unique removes duplicate strings from a slice while preserving order

func ValidateOneOf

func ValidateOneOf(fields map[string]string) error

ValidateOneOf checks that at least one field is non-empty

func ValidateRequired

func ValidateRequired(fields map[string]string) error

ValidateRequired checks if required fields are non-empty

func Wrapf

func Wrapf(err error, format string, args ...interface{}) error

Wrapf wraps an error with formatted message

Types

type RetryOpts

type RetryOpts struct {
	MaxAttempts   int
	InitialDelay  time.Duration
	MaxDelay      time.Duration
	BackoffFactor float64
}

RetryOpts specifies options for Retry

func DefaultRetryOpts

func DefaultRetryOpts() RetryOpts

DefaultRetryOpts returns default retry options

type WrapError

type WrapError struct {
	Message string
	Err     error
}

WrapError wraps an error with additional context

func (*WrapError) Error

func (e *WrapError) Error() string

func (*WrapError) Unwrap

func (e *WrapError) Unwrap() error

Jump to

Keyboard shortcuts

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