helpers

package
v0.32.2 Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2024 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package helpers provides generic helper functions with no external imports

Package helpers provides generic helper functions with no external imports

Package helpers provides generic helper functions with no external imports

Package helpers provides generic helper functions with no external imports

Package helpers provides generic helper functions with no external imports

Index

Constants

View Source
const (
	OCIURLPrefix = "oci://"

	SGETURLPrefix = "sget://"
	SGETURLScheme = "sget"

	IPV4Localhost = "127.0.0.1"
)

Nonstandard URL schemes or prefixes

Variables

This section is empty.

Functions

func DoHostnamesMatch added in v0.29.0

func DoHostnamesMatch(url1 string, url2 string) (bool, error)

DoHostnamesMatch returns a boolean indicating if the hostname of two different URLs are the same.

func ExtractBasePathFromURL added in v0.29.1

func ExtractBasePathFromURL(urlStr string) (string, error)

ExtractBasePathFromURL returns filename from URL string

func Filter

func Filter[T any](ss []T, test func(T) bool) (r []T)

Filter returns a new slice with only the elements that pass the test.

func Find

func Find[T any](ss []T, test func(T) bool) (r T)

Find returns the first element that passes the test.

func GetAvailablePort added in v0.30.0

func GetAvailablePort() (int, error)

GetAvailablePort retrieves an available port on the host machine. This delegates the port selection to the golang net library by starting a server and then checking the port that the server is using.

func GetCRCHash

func GetCRCHash(text string) uint32

GetCRCHash returns the computed CRC32 Sum of a given string

func GetCryptoHash

func GetCryptoHash(data io.ReadCloser, hashName crypto.Hash) (string, error)

GetCryptoHash returns the computed SHA256 Sum of a given file

func GetSHA256Hash added in v0.29.0

func GetSHA256Hash(data io.ReadCloser) (string, error)

GetSHA256Hash returns the computed SHA256 Sum of a given file

func IsNotZeroAndNotEqual added in v0.29.0

func IsNotZeroAndNotEqual[T any](given T, equal T) bool

IsNotZeroAndNotEqual is used to test if a struct has zero values or is equal values with another struct

func IsOCIURL added in v0.29.0

func IsOCIURL(source string) bool

IsOCIURL returns true if the given URL is an OCI URL.

func IsURL added in v0.29.0

func IsURL(source string) bool

IsURL is a helper function to check if a URL is valid.

func IsValidHostName added in v0.30.0

func IsValidHostName() bool

IsValidHostName returns a boolean indicating if the hostname of the host machine is valid according to https://www.ietf.org/rfc/rfc1123.txt.

func MatchRegex

func MatchRegex(regex *regexp.Regexp, str string) (func(string) string, error)

MatchRegex wraps a get function around a substring match.

func MergeMap

func MergeMap[T any](m1, m2 map[string]T) (r map[string]T)

MergeMap merges map m2 with m1 overwriting common values with m2's values.

func MergeMapRecursive

func MergeMapRecursive(m1, m2 map[string]interface{}) (r map[string]interface{})

MergeMapRecursive recursively (nestedly) merges map m2 with m1 overwriting common values with m2's values.

func MergeNonZero added in v0.29.0

func MergeNonZero[T any](original T, overrides T) T

MergeNonZero is used to merge non-zero overrides from one struct into another of the same type

func MergeSlices added in v0.31.0

func MergeSlices[T any](s1, s2 []T, equals EqualFunc[T]) []T

MergeSlices merges two slices, s1 and s2, and returns a new slice containing all elements from s1 and only those elements from s2 that do not exist in s1 based on the provided equal function.

func RandomString added in v0.32.0

func RandomString(length int) (string, error)

RandomString generates a secure random string of the specified length.

func RemoveMatches

func RemoveMatches[T any](ss []T, test func(T) bool) (r []T)

RemoveMatches removes the given element from the slice that matches the test.

func Retry

func Retry(fn func() error, retries int, delay time.Duration, logger func(format string, args ...any)) (err error)

Retry will retry a function until it succeeds or the timeout is reached, timeout == retries * delay.

func Reverse

func Reverse[T any](s []T) (r []T)

Reverse returns a new slice with the elements in reverse order.

func StringToSlice added in v0.30.0

func StringToSlice(s string) []string

StringToSlice converts a comma-separated string to a slice of lowercase strings.

func TransformAndMergeMap added in v0.29.0

func TransformAndMergeMap[T any](m1, m2 map[string]T, transform func(string) string) (r map[string]T)

TransformAndMergeMap transforms keys in both maps then merges map m2 with m1 overwriting common values with m2's values.

func TransformMapKeys

func TransformMapKeys[T any](m map[string]T, transform func(string) string) (r map[string]T)

TransformMapKeys takes a map and transforms its keys using the provided function.

func Unique

func Unique[T comparable](s []T) (r []T)

Unique returns a new slice with only unique elements.

Types

type EqualFunc added in v0.31.0

type EqualFunc[T any] func(a, b T) bool

EqualFunc defines a type for a function that determines equality between two elements of type T.

Jump to

Keyboard shortcuts

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