utils

package
v0.0.13-beta Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2025 License: MIT Imports: 14 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 BytesToScalar

func BytesToScalar(data []byte, out *string) error

BytesToScalar attempts to unmarshal YAML bytes as a scalar value. Returns an error if the bytes cannot be unmarshaled as a scalar.

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 MatchEnvToFlag

func MatchEnvToFlag(name Prefix) func(string) string

MatchEnvToFlag allows the env provider of koanf to property match stuff like: APP_REGISTRY_TOKEN -> registry-token when name is for example "APP_"

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 NodeToBytes

func NodeToBytes(node ast.Node) ([]byte, error)

NodeToBytes converts an ast.Node to bytes.

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

type Prefix

type Prefix string

func (Prefix) Lower

func (p Prefix) Lower() Prefix

func (Prefix) RemovePrefix

func (p Prefix) RemovePrefix(prefix string) Prefix

func (Prefix) Scoped

func (p Prefix) Scoped() Prefix

func (Prefix) String

func (p Prefix) String() string

func (Prefix) Upper

func (p Prefix) Upper() Prefix

func (Prefix) WithUnderscores

func (p Prefix) WithUnderscores() Prefix

Jump to

Keyboard shortcuts

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