Documentation
¶
Overview ¶
Package utils provides common utility functions shared across all service layer services
Index ¶
- func BuildURL(base string, path string, params map[string]string) string
- func Coalesce(strs ...string) string
- func Contains(slice []string, target string) bool
- func ContainsAny(slice []string, targets []string) bool
- func Deref[T any](p *T) T
- func DerefDefault[T any](p *T, defaultVal T) T
- func Filter(slice []string, predicate func(string) bool) []string
- func FormatDuration(d time.Duration) string
- func GetEnv(key, defaultValue string) string
- func GetEnvBool(key string, defaultValue bool) bool
- func GetEnvInt(key string, defaultValue int) int
- func GetEnvOptional(key string) string
- func GoSafeGo(fn func())
- func IsEmpty(s string) bool
- func JSONMarshal(v interface{}) string
- func JSONMarshalIndent(v interface{}) string
- func JSONParse(jsonStr string) (interface{}, error)
- func JoinPath(parts ...string) string
- func Map(slice []string, fn func(string) string) []string
- func MapKeys[K comparable, V any](m map[K]V) []K
- func MapValues[K comparable, V any](m map[K]V) []V
- func MergeMaps[K comparable, V any](maps ...map[K]V) map[K]V
- func Must[T any](v T, err error) T
- func MustJSONParse(jsonStr string) interface{}
- func MustParseDuration(s string) time.Duration
- func MustRetry(fn func() error, opts ...RetryOpts)
- func NewWrapError(message string, err error) error
- func Now() string
- func NowFormatted(format string) string
- func ParseDuration(s string) (time.Duration, error)
- func Ptr[T any](v T) *T
- func PtrZero[T comparable](v T) *T
- func Retry(fn func() error, opts ...RetryOpts) error
- func SafeGo(fn func(), recoveryFn func(error))
- func SliceToMap[T any, K comparable](slice []T, keyFn func(T) K) map[K]T
- func SplitTrim(s, delimiter string) []string
- func ToBool(v interface{}, defaultValue bool) bool
- func ToInt(v interface{}, defaultValue int) int
- func ToSlice(s string) []string
- func ToString(v interface{}) string
- func TrimEmpty(strs []string) []string
- func Truncate(s string, maxLen int) string
- func Unique(slice []string) []string
- func ValidateOneOf(fields map[string]string) error
- func ValidateRequired(fields map[string]string) error
- func Wrapf(err error, format string, args ...interface{}) error
- type RetryOpts
- type WrapError
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ContainsAny ¶
ContainsAny checks if a slice contains any of the targets
func DerefDefault ¶
func DerefDefault[T any](p *T, defaultVal T) T
DerefDefault returns the value pointed to, or default if nil
func FormatDuration ¶
FormatDuration formats a duration in a human-readable way
func GetEnvBool ¶
GetEnvBool retrieves a boolean environment variable
func GetEnvOptional ¶
GetEnvOptional retrieves an environment variable without default
func GoSafeGo ¶
func GoSafeGo(fn func())
GoSafeGo starts a goroutine with default panic recovery (logs error)
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 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 MustJSONParse ¶
func MustJSONParse(jsonStr string) interface{}
MustJSONParse parses JSON string or panics
func MustParseDuration ¶
MustParseDuration parses a duration string or panics
func NewWrapError ¶
NewWrapError creates a new wrapped error
func NowFormatted ¶
NowFormatted returns current time in specified format
func ParseDuration ¶
ParseDuration parses a duration string (e.g., "1h", "30m", "500ms")
func PtrZero ¶
func PtrZero[T comparable](v T) *T
PtrZero returns a nil pointer if value is zero, otherwise pointer to value
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 ValidateOneOf ¶
ValidateOneOf checks that at least one field is non-empty
func ValidateRequired ¶
ValidateRequired checks if required fields are non-empty