Documentation
¶
Overview ¶
util contains small standalone utility functions. This should have no dependencies on other server packages.
Index ¶
- func CloneMapNonNil[M ~map[K]V, K comparable, V any](m M) M
- func ConvertPathToCamel(input string) []string
- func DeleteFromMap[M ~map[K]M2, M2 ~map[K2]V, K, K2 comparable, V any](m M, k K, k2 K2)
- func ErrorType(err error) string
- func FilterSlice[T any](in []T, predicate func(T) bool) []T
- func FoldSlice[T any, A any](in []T, initializer A, reducer func(A, T) A) A
- func GetOrSetMap[M ~map[K]M2, M2 ~map[K2]V, K, K2 comparable, V any](m M, k K) M2
- func GetOrSetNew[M ~map[K]*V, K comparable, V any](m M, k K) *V
- func InterruptibleSleep(ctx context.Context, timeout time.Duration) error
- func InverseMap[M ~map[K]V, K, V comparable](m M) map[V]K
- func MapConcurrent[IN any, OUT any](input []IN, mapper func(IN) (OUT, error)) ([]OUT, error)
- func MapSlice[T, S any](xs []T, f func(T) S) []S
- func MaxTime(a, b time.Time) time.Time
- func MinTime(a, b time.Time) time.Time
- func NextAlignedTime(t time.Time, align time.Duration) time.Time
- func ParseFieldMask(mask *fieldmaskpb.FieldMask) map[string]struct{}
- func Ptr[T any](v T) *T
- func RepeatSlice[T any](xs []T, n int) []T
- func SliceHead[S ~[]E, E any](s S, n int) S
- func SliceTail[S ~[]E, E any](s S, n int) S
- func SortSlice[S ~[]E, E expconstraints.Ordered](slice S)
- func TruncateUTF8(s string, n int) string
- func WildCardStringToRegexp(pattern string) (*regexp.Regexp, error)
- func WildCardStringsToRegexp(patterns []string) (*regexp.Regexp, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CloneMapNonNil ¶
func CloneMapNonNil[M ~map[K]V, K comparable, V any](m M) M
CloneMapNonNil is like maps.Clone except it can't return nil, it will return an empty map instead.
func ConvertPathToCamel ¶
func DeleteFromMap ¶
func DeleteFromMap[M ~map[K]M2, M2 ~map[K2]V, K, K2 comparable, V any](m M, k K, k2 K2)
DeleteFromMap deletes k2 from the nested map m[k]. If the inner map becomes empty after deletion, k is also removed from m to prevent memory leaks.
func ErrorType ¶
ErrorType returns a best effort guess at the most meaningful type name for the given error. If any of err's underlying errors implement TypedError, then the type name of the first such error is returned. This allows us to be explicit about the tag values we want to use for telemetry. Otherwise, the type name of the first non-wrapper error in the depth-first traversal of err's tree is returned. We consider errors wrapped via fmt.Errorf, errors.Join and some pkg/errors functions to be wrapper errors.
func FilterSlice ¶
FilterSlice iterates over elements of a slice, returning a new slice of all elements predicate returns true for.
func GetOrSetMap ¶
func GetOrSetMap[M ~map[K]M2, M2 ~map[K2]V, K, K2 comparable, V any](m M, k K) M2
GetOrSetMap looks up k in m, a two-level map, and returns the result. If it's not present, it uses `make` to allocate new second-level map and sets that in the first map, then returns it.
func GetOrSetNew ¶
func GetOrSetNew[M ~map[K]*V, K comparable, V any](m M, k K) *V
GetOrSetNew looks up k in m and returns the result. If it's not present, it uses `new` to allocate an new value type and sets that in the map, then returns it.
func InterruptibleSleep ¶
InterruptibleSleep is like time.Sleep but can be interrupted by a context. Returns context error if interrupted, otherwise nil.
func InverseMap ¶
func InverseMap[M ~map[K]V, K, V comparable](m M) map[V]K
InverseMap creates the inverse map, ie., for a key-value map, it builds the value-key map.
func MapConcurrent ¶
MapConcurrent concurrently maps a function over input and fails fast on error.
func MapSlice ¶
func MapSlice[T, S any](xs []T, f func(T) S) []S
MapSlice given slice xs []T and f(T) S produces slice []S by applying f to every element of xs
func NextAlignedTime ¶
NextAlignedTime returns the earliest time after `t` that is aligned to an integer multiple of `align` since the unix epoch.
func ParseFieldMask ¶
func ParseFieldMask(mask *fieldmaskpb.FieldMask) map[string]struct{}
func RepeatSlice ¶
RepeatSlice given slice and a number (n) produces a new slice containing original slice n times if n is non-positive will produce nil
func SortSlice ¶
func SortSlice[S ~[]E, E expconstraints.Ordered](slice S)
SortSlice sorts the given slice of an ordered type. Sort is not guaranteed to be stable.
func TruncateUTF8 ¶
TruncateUTF8 truncates s to no more than n _bytes_, and returns a valid utf-8 string as long as the input is a valid utf-8 string. Note that truncation pays attention to codepoints only! This may truncate in the middle of a grapheme cluster.
func WildCardStringToRegexp ¶
WildCardStringToRegexp converts a given string pattern to a regular expression matching wildcards (*) with any substring.
Types ¶
This section is empty.