helpers

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: May 6, 2025 License: MPL-2.0 Imports: 23 Imported by: 136

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AnySliceToTypedSlice added in v0.9.0

func AnySliceToTypedSlice(input any) any

AnySliceToTypedSlice converts a slice of `any` (`[]interface{}`) into a strongly typed slice If mixed types are detected, it returns the original `[]any` to prevent panics.

func AppendSliceUnique added in v1.0.0

func AppendSliceUnique[T comparable](slice1, slice2 []T) []T

AppendSliceUnique appends elements from slice2 to slice1, ensuring uniqueness.

func ByteMapToStringMap added in v1.0.0

func ByteMapToStringMap(m map[string][]byte) map[string]string

func Clean added in v0.3.0

func Clean(str string) string

Clean removes non-printing characters from the string

func CombineErrors added in v0.3.0

func CombineErrors(errors ...error) error

func CombineErrorsWithPrefix added in v0.3.0

func CombineErrorsWithPrefix(prefix string, errorList ...error) error

func DereferencePointer

func DereferencePointer(val interface{}) interface{}

DereferencePointer checks if val is a pointer, and if so, dereferences it

func EscapePropertyName added in v0.4.0

func EscapePropertyName(name string) string

EscapePropertyName replaces any '.' characters in the property name with propertyPathDotEscape

func ExecuteMethod

func ExecuteMethod(item interface{}, methodName string) (returnValues []interface{}, err error)

ExecuteMethod use reflection to invoke method. We do not support functions which expect parameters.

func FileHash added in v0.5.0

func FileHash(filePath string) (string, error)

FileHash streams a file into a MD5 hasher and returns the resultant MD5 hash as a HEX encoded string. Uses FileMD5Hash under-the-hood

func FileMD5Hash added in v0.5.0

func FileMD5Hash(filePath string) ([]byte, error)

FileMD5Hash streams a file into a MD5 hasher and returns the resultant MD5 hash bytes This DOES NOT read the whole file into memory

func FilterMap added in v0.8.0

func FilterMap[K comparable, V any](src map[K]V, keys []K) map[K]V

FilterMap builds a map based on `src`, but using only keys specified in `keys`

func GetArrayValue

func GetArrayValue(i interface{}, index int) (interface{}, bool)

func GetCallingFunction

func GetCallingFunction(level int) string

GetCallingFunction :: return the calling function

level is how far up the call stack to go. so
func f1(){
	f2()
}
func f2(){
	// returns "f2"
	helpers.GetCallingFunction(0)

	// returns "f1"
	helpers.GetCallingFunction(1)
}

func GetFieldValueFromInterface

func GetFieldValueFromInterface(i interface{}, fieldName string) (interface{}, bool)

GetFieldValueFromInterface uses reflection to return the value of the given property path

func GetFunctionName

func GetFunctionName(i interface{}) string

GetFunctionName :: return the name of the given function

func GetMD5Hash added in v0.5.0

func GetMD5Hash(text string) string

GetMD5Hash returns the MD5 hash of the given string

func GetNestedFieldValueFromInterface

func GetNestedFieldValueFromInterface(item interface{}, propertyPath string) (interface{}, bool)

GetNestedFieldValueFromInterface uses reflection to return the value of the given nested property path

func InstantiateType

func InstantiateType(t reflect.Type) interface{}

InstantiateType returns a interface representing the zero value for the specified type.

func IsFieldArray

func IsFieldArray(fieldName string) (string, int, bool)
	TODO: add support for multi-dimensional arrays
 	like - arr[1][2] arr[1][2][3] and so on..

func IsNil added in v0.3.0

func IsNil(i interface{}) bool

IsNil uses reflection to determine whether the given value is nil this is needed as a simple 'val == nil' check does not work for an interface https://mangatmodi.medium.com/go-check-nil-interface-the-right-way-d142776edef1

func IsOnlyNumeric added in v1.1.0

func IsOnlyNumeric(s string) bool

IsOnlyNumeric returns true if the string only contains numeric characters

func IsZero

func IsZero(i interface{}) bool

IsZero uses reflection to determine whether the given value is the zero value of it's type

func LimitPrintableRunes added in v0.3.0

func LimitPrintableRunes(s string, n int) string

LimitPrintableRunes limits the string to the given number of runes

func LintName added in v0.4.0

func LintName(name string) (should string)

LintName modifies the given name to make common intialialisms upper case

func PadRight added in v0.5.0

func PadRight(str string, length int, pad byte) string

PadRight returns a new string of a specified length in which the end of the current string is padded with spaces or with a specified Unicode character.

func ParseTime added in v1.1.0

func ParseTime(input string) (time.Time, error)

ParseTime parses a string into a time.Time object attempting to parse it as a variety of formats

func PrintableLength added in v0.3.0

func PrintableLength(str string) int

PrintableLength returns the number of printable runes in a string (after stripping ansi codes)

func RemoveFromStringSlice

func RemoveFromStringSlice(slice []string, values ...string) []string

RemoveFromStringSlice removes the given values from the slice.

func Resize added in v0.5.0

func Resize(s string, length uint) string

Resize resizes the string with the given length. It ellipses with '…' when the string's length exceeds the desired length or pads spaces to the right of the string when length is smaller than desired

func SliceToLookup added in v0.6.0

func SliceToLookup[K comparable](src []K) map[K]struct{}

SliceToLookup converts a slice into a lookup

func SortedMapKeys added in v0.6.0

func SortedMapKeys[V any](m map[string]V) []string

SortedMapKeys returns the sorted keys of the map `m`

func SplitByRune added in v0.5.0

func SplitByRune(str string, r rune) []string

SplitByRune uses the CSV decoder to parse out the tokens - even if they are quoted and/or escaped

func SplitByWhitespace added in v0.5.0

func SplitByWhitespace(str string) []string

SplitByWhitespace splits by the ' ' rune

func StringFnvHash added in v0.5.0

func StringFnvHash(s string) uint32

StringFnvHash returns a FNV1 hash of the given string. returns 0 if there's an error

func StringSliceDiff

func StringSliceDiff(slice1, slice2 []string) []string

StringSliceDiff finds elements in slice1 that are not in slice2.

func StringSliceDistinct added in v0.3.0

func StringSliceDistinct(slice []string) []string

func StringSliceEqualIgnoreOrder added in v0.9.0

func StringSliceEqualIgnoreOrder(a, b []string) bool

func StringSliceHasDuplicates added in v0.3.0

func StringSliceHasDuplicates(slice []string) bool

StringSliceHasDuplicates returns whether a string slice has duplicate elements

func StructToMap added in v1.0.0

func StructToMap(s any) map[string]any

StructToMap uses reflection to convert a struct to a map

func Tabify added in v0.2.0

func Tabify(str string, tab string) string

Tabify adds the provided tab string to beginning of each line of string

func TabifyStringSlice added in v0.3.0

func TabifyStringSlice(strs []string, tab string) []string

TabifyStringSlice adds the provided tab string to beginning of each line of string

func ToError added in v0.1.1

func ToError(val interface{}) error

ToError formats the supplied value as an error (or just returns it if already an error)

func TrimBlankLines added in v0.9.0

func TrimBlankLines(str string) string

TrimBlankLines removes any empty lines from the string

func TruncateString added in v0.2.0

func TruncateString(str string, length int) string

TruncateString limits the string to the given length, adding an ellipsis if the string is being truncated, also handles newlines

func UnescapePropertyName added in v0.4.0

func UnescapePropertyName(name string) string

UnescapePropertyName replaces any propertyPathDotEscape occurrences with "."

Types

This section is empty.

Jump to

Keyboard shortcuts

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