utils

package
v0.0.11 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2025 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package utils provides common utility functions for general purpose tasks. It includes helpers for URL validation, zero value checking, slice manipulation, map normalization, path handling, and deep copying objects.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DeepCopy added in v0.0.11

func DeepCopy[T any](src T) (dst T, err error)

DeepCopyInto copies the contents of the object and returns it.

func DeepCopyWithMarshaling added in v0.0.11

func DeepCopyWithMarshaling[T any](src T) (dst T, err error)

DeepCopyWithMarshaling creates a deep copy of a source object using JSON marshaling.

func ExpandHome added in v0.0.9

func ExpandHome(path string) string

ExpandHome resolves home directory references in paths. Replaces leading ~ with the user's home directory path. Returns the original path if expansion fails or isn't needed.

func IsURL

func IsURL(str string) bool

IsURL validates a string as a properly formatted URL. Returns true if the string contains both a scheme and host.

func IsZeroValue added in v0.0.1

func IsZeroValue[S comparable](input S) bool

IsZeroValue checks if a value equals its type's zero value. Works with any comparable type (numbers, strings, etc.).

func MergeMaps added in v0.0.11

func MergeMaps[T ~map[string]any](primary, secondary T) (T, error)

MergeMaps constructs a merge between primary and secondary where primary values have priority.

func MergeMapsInPlace added in v0.0.11

func MergeMapsInPlace[T ~map[string]any](primary, secondary T) error

MergeMaps constructs a merge between primary and secondary where primary values have priority. The primary map is modified in place.

func NormalizeMap

func NormalizeMap(m map[string]any) map[string]any

NormalizeMap converts map keys to title case recursively. Creates a new map with all string keys converted to title case. Handles nested maps by recursively normalizing their keys as well.

func PickByIndices added in v0.0.11

func PickByIndices[T any](s []T, indices []int) []T

func SetIfZeroValue added in v0.0.1

func SetIfZeroValue[S comparable](input *S, value S)

SetIfZeroValue conditionally updates a pointer's value. Sets the pointed-to value to a new value only if the current value equals the type's zero value.

func SetSliceIfNil

func SetSliceIfNil[S ~[]T, T any](input *S, values ...T)

SetSliceIfNil initializes a nil slice pointer. Creates a new slice with the provided values only if the pointer is nil. Safe for any slice type.

func SetSliceIfZero added in v0.0.4

func SetSliceIfZero[S ~[]T, T any](input *S, values ...T)

SetSliceIfZero initializes an empty or nil slice pointer. Creates a new slice with the provided values if the current slice is nil or has zero length.

Types

This section is empty.

Jump to

Keyboard shortcuts

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