helpers

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2023 License: MPL-2.0 Imports: 23 Imported by: 103

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 determines whether input is []any and if so converts to an array of the underlying type

func Clean added in v0.3.0

func Clean(str string) string

Clean removes non-printing characters from the string

func CloneStringMap

func CloneStringMap(src map[string]string) map[string]string

CloneStringMap :: clone a string map - return a copy of the map

func CombineErrors added in v0.3.0

func CombineErrors(errors ...error) error

func CombineErrorsWithPrefix added in v0.3.0

func CombineErrorsWithPrefix(prefix string, errors ...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 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 MergeMaps added in v0.6.0

func MergeMaps[M ~map[K]V, K comparable, V any](old, new M) M

MergeMaps merges 'new' onto 'old'. Values existing in old already have precedence Any value existing in new but not old is added to old returns a copy of the original map

func MergeStringMaps

func MergeStringMaps(a, b map[string]string) map[string]string

MergeStringMaps :: merge 2 string maps, returning a new map

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 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 string from the string 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 returnes 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 StringSliceContains

func StringSliceContains(slice []string, val string) bool

StringSliceContains returns whether the string slice contains the given string

func StringSliceDiff

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

StringSliceDiff returns values which only exists in the fist string slice

func StringSliceDistinct added in v0.3.0

func StringSliceDistinct(slice []string) []string

StringSliceDistinct returns a slice with the unique elements the input string slice

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 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 ToTypedSlice added in v0.9.0

func ToTypedSlice[T any](input []any) []T

ToTypedSlice converts []any to []T

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