utils

package
v0.0.0-...-bd23c5e Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2025 License: GPL-3.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrCachedItemExpired = errors.New("cached item has expired")
)

Functions

func Any

func Any[T any](in []T, f func(T) bool) bool

func AtIdx

func AtIdx[T any](s []T, index int) T

AtIdx returns the value at the given index, unless out of range; then return the zero value for the element

func BytesToSize

func BytesToSize(bytes float64) string

func Clamp

func Clamp(val, minV, maxV int) int

Clamp returns at least minV, and at most maxV

Will panic if maxV < minV or minV > maxV

func Count

func Count[T any](array []T, f func(T) bool) int

func Defer

func Defer(f func() error, log zerolog.Logger, wg *sync.WaitGroup)

Defer adds 1 to the WaitGroup, then starts a goroutine called f and deferring wg.Done

func Ext

func Ext(uri string, defaultExt ...string) string

func Filter

func Filter[T any](in []T, f func(T) bool) []T

func Find

func Find[T any](in []T, f func(T) bool) *T

Find returns the first element in the slice that returns true for the function

func FindOk

func FindOk[T any](in []T, f func(T) bool) (T, bool)

func FlatMap

func FlatMap[E any](in [][]E) []E

func FlatMapMany

func FlatMapMany[E any](in ...[]E) []E

func ForEach

func ForEach[T any](s []T, f func(T))

func GenerateApiKey

func GenerateApiKey() (string, error)

func GenerateSecret

func GenerateSecret(length int) (string, error)

func GroupBy

func GroupBy[K comparable, V any](s []V, f func(V) K) map[K][]V

func HumanReadableSpeed

func HumanReadableSpeed(s int64) string

func Identity

func Identity[T any](t T) func() T

func IsSameDay

func IsSameDay(t1, t2 time.Time) bool

func Keys

func Keys[K comparable, V any](m map[K]V) []K

func Map

func Map[T, S any](in []T, f func(T) S) []S

func MapKeys

func MapKeys[K comparable, V, T any](m map[K]V, f func(K) T) []T

func MapToString

func MapToString[T ~string](m []T) []string

func MapValues

func MapValues[K comparable, V, T any](m map[K]V, f func(V) T) []T

func MapWithIdx

func MapWithIdx[T, S any](in []T, f func(int, T) S) []S

func MapWithState

func MapWithState[T, U any, S any](in []T, state S, f func(T, S) (U, S)) []U

func MaybeMap

func MaybeMap[T, S any](in []T, f func(T) (S, bool)) []S

func Must

func Must(err error)

func MustHave

func MustHave[T any](result T, ok bool) T

func MustInvoke

func MustInvoke[T any](c Invoker) T

MustInvoke tries construction T with the given Container Ensure the needed method has been Provider to the Container

func MustReturn

func MustReturn[T any](result T, err error) T

func NewRedisCacheStorage

func NewRedisCacheStorage(log zerolog.Logger, clientName, redisAddr string) fiber.Storage

func NonEmpty

func NonEmpty(s ...string) string

NonEmpty returns the first string not empty string

func Normalize

func Normalize(s string) string

Normalize removes all non-alphanumerical numbers, and then lowercases the result

func OrDefault

func OrDefault[T any](array []T, defaultValue T) T

func OrElse

func OrElse(s string, def string) string

OrElse returns s if len(s) > 0 otherwise def

func PadFloatFromString

func PadFloatFromString(s string, n int) string

PadFloatFromString returns the float as a string, with pad called on the whole part and the decimal part copied from the input string, if present This method assumes the string is a valid float

func PadInt

func PadInt(i int, n int) string

PadInt returns the i as a string s, with 0's added to the left until len(s) >= n

func Percent

func Percent(a, b int64) int64

Percent returns the % of a contained in b. At most 100 is returned. Percent(a, 0) = 100 for a != 0, Percent(0,0) = 0

func SafeFloat

func SafeFloat(s string) float64

SafeFloat parses the string as a float64, and returns -1 on error

func Shorten

func Shorten(s string, length int) string

func SortFloats

func SortFloats(a, b string) int

func Stringify

func Stringify(i int) string

func Ternary

func Ternary[T any](condition bool, ifTrue, ifFalse T) T

func TrimLeadingZero

func TrimLeadingZero(s string) string

func Values

func Values[K comparable, V any](m map[K]V) []V

func Wait

func Wait(wg *sync.WaitGroup) <-chan struct{}

Wait returns a channel which completes when the WaitGroup has finished

func WaitFor

func WaitFor(wg *sync.WaitGroup, d time.Duration)

WaitFor waits for at most d, and at least until wg has finished

Types

type CachedItem

type CachedItem[T any] interface {
	Get() (T, error)
	HasExpired() bool
	SetExpired()
}

CachedItem is the simplest way of caching an item, do not use for more complicated stuff does not evict anything itself

func NewCachedItem

func NewCachedItem[T any](data T, exp time.Duration) CachedItem[T]

type Invoker

type Invoker interface {
	Invoke(function interface{}, opts ...dig.InvokeOption) (err error)
}

type Option

type Option[T any] func(T)

type SafeMap

type SafeMap[K comparable, V any] interface {
	Has(k K) bool
	Get(k K) (V, bool)
	Set(k K, v V)
	Len() int
	Delete(k K)
	Clear()
	Keys() []K
	Values() []V
	// ForEach iterates over the map, and unlocks during function call
	ForEach(f func(k K, v V))
	// ForEachSafe iterates over the map, does not unlock during function all.
	//
	// Use SafeMap.ForEach if you need to modify the map
	ForEachSafe(f func(k K, v V))
	Any(f func(k K, v V) bool) bool
	Count(f func(k K, v V) bool) int
	Find(f func(k K, v V) bool) (*V, bool)
}

func NewSafeMap

func NewSafeMap[K comparable, V any](m ...map[K]V) SafeMap[K, V]

type Settable

type Settable[T any] struct {
	// contains filtered or unexported fields
}

Settable represents a variable where the zero value has more meaning than undecided yet A Settable may be Set more than once

func (*Settable[T]) Get

func (s *Settable[T]) Get() (T, bool)

Get returns the value if present, otherwise zero, false

func (*Settable[T]) Set

func (s *Settable[T]) Set(t T)

Set sets the value

type Toggles

type Toggles[K comparable] struct {
	// contains filtered or unexported fields
}

func NewToggles

func NewToggles[K comparable]() *Toggles[K]

func (*Toggles[K]) Toggle

func (t *Toggles[K]) Toggle(k K)

func (*Toggles[K]) Toggled

func (t *Toggles[K]) Toggled(k K) bool

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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