utils

package
v0.0.0-...-315237c Latest Latest
Warning

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

Go to latest
Published: May 2, 2024 License: MIT Imports: 16 Imported by: 16

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ArrayContains

func ArrayContains[T comparable](arr []T, value T) bool

func ArrayContainsF

func ArrayContainsF[V any](arr []V, filterFunc func(V) bool) bool

func ArrayExcluding

func ArrayExcluding[T comparable](arr []T, valueToExclude ...T) []T

func ArrayFilter

func ArrayFilter[V any](arr []V, filterFunc func(V) bool) []V

func ArrayFilterMap

func ArrayFilterMap[V any, R any](arr []V, filterFunc func(V) bool, mappingFunc func(V) R) []R

func ArrayIndexOf

func ArrayIndexOf[V any](arr []V, filterFunc func(V) bool) int

func ArrayIntersection

func ArrayIntersection[T comparable](firstArray []T, secondArray []T) []T

func ArrayMap

func ArrayMap[V any, R any](arr []V, mappingFunc func(V) R) []R

func BoolPointer

func BoolPointer(b bool) *bool

func DefaultString

func DefaultString(str, defaultValue string) string

func DefaultStringFunc

func DefaultStringFunc(str string, f func() string) string

func ExtractObject

func ExtractObject(object any, path ...string) (any, error)

func GetFreePort

func GetFreePort() (int, error)

GetFreePort asks the kernel for a free open port that is ready to use.

func GetFreePorts

func GetFreePorts(count int) ([]int, error)

GetFreePort asks the kernel for free open ports that are ready to use.

func GetPort

func GetPort() int

GetPort is deprecated, use GetFreePort instead Ask the kernel for a free open port that is ready to use

func HashAny

func HashAny(value interface{}) (uint64, error)

func HashBytes

func HashBytes(payload []byte) [16]byte

func HashString

func HashString(value string) [16]byte

func HashStringInt

func HashStringInt(value string) uint32

func IsAlphanumeric

func IsAlphanumeric(str string) bool

func IsLetterOrNumber

func IsLetterOrNumber(symbol int32) bool

IsLetterOrNumber returns true if input symbol is:

A - Z: 65-90
a - z: 97-122

func IsLetterOrUnderscore

func IsLetterOrUnderscore(symbol int32) bool

func IsLowerAlphanumeric

func IsLowerAlphanumeric(str string) bool

func IsLowerLetterOrNumber

func IsLowerLetterOrNumber(symbol int32) bool

func IsLowerLetterOrUnderscore

func IsLowerLetterOrUnderscore(symbol int32) bool

func IsNumber

func IsNumber(symbol int32) bool

func IsSameSymbol

func IsSameSymbol(str string, symbol int32) bool

func IsTruish

func IsTruish(value any) bool

func JoinNonEmptyStrings

func JoinNonEmptyStrings(sep, elem1, elem2 string) string

JoinNonEmptyStrings joins strings with separator, but ignoring empty strings

func MapClear

func MapClear[K comparable, V any](mp map[K]V)

func MapCopy

func MapCopy[K comparable, V any](mp map[K]V) map[K]V

MapCopy returns copy of map

func MapFilter

func MapFilter[K comparable, V any](mp map[K]V, predicate func(K, V) bool) map[K]V

func MapHasSameKeys

func MapHasSameKeys[K comparable, V any, J any](a map[K]V, b map[K]J) bool

func MapMap

func MapMap[K comparable, V any, R any](mp map[K]V, mappingFunc func(K, V) R) map[K]R

func MapNVL

func MapNVL[K comparable, V any](mp map[K]V, key K, defaultValue V) V

func MapNVLFunc

func MapNVLFunc[K comparable, V any](mp map[K]V, key K, defaultFunc func() V) V

func MapNVLKeys

func MapNVLKeys[K comparable, V any](mp map[K]V, keys ...K) V

MapNVLKeys returns value by first key that exists or empty value of V type if no keys exist

func MapPutAll

func MapPutAll[K comparable, V any](destination map[K]V, source map[K]V) map[K]V

func MapPutIfAbsent

func MapPutIfAbsent[K comparable, V any](mp map[K]V, key K, value V) bool

MapPutIfAbsent puts value into map if key doesn't exist. Returns true if value was put

func MapToSlice

func MapToSlice[K comparable, V any, R any](mp map[K]V, mappingFunc func(K, V) R) []R

func MaxDuration

func MaxDuration(a, b time.Duration) time.Duration

func MaxInt

func MaxInt(a, b int) int

func MaxInt64

func MaxInt64(a, b int64) int64

func MinInt64

func MinInt64(a, b int64) int64

func Nvl

func Nvl[T comparable](args ...T) T

Nvl returns first not null object or pointer from varargs

return nil if all passed arguments are nil

func NvlMap

func NvlMap(args ...map[string]any) map[string]any

NvlMap returns first not empty map from varargs

return nil if all passed maps are empty

func NvlString

func NvlString(args ...string) string

NvlString returns first not empty string value from varargs

return "" if all strings are empty

func ParseBool

func ParseBool(value any) (bool, error)

ParseBool parses value of string, int or bool into a bool.

func ParseFloat

func ParseFloat(value any) (float64, error)

ParseFloat parses value of string, int, integer float into float64.

func ParseInt

func ParseInt(value any) (int, error)

ParseInt parses value of string, int, integer float into int.

func ParseObject

func ParseObject[K any](inputObject any, result *K) error

ParseObject parses struct of any type from input object that can be:

map, json string or yaml string,

already struct of provided type or pointer to it

func ParseString

func ParseString(value any) (string, error)

ParseString parses value of string - effectively strict type checking.

func SanitizeString

func SanitizeString(str string) string

SanitizeString returns string with only alphanumeric characters and underscores

func ShortenString

func ShortenString(str string, n int) string

ShortenString returns the first N slice of a string.

func ShortenStringWithEllipsis

func ShortenStringWithEllipsis(str string, n int) string

ShortenStringWithEllipsis returns the first N slice of a string and ends with ellipsis.

Types

type Cache

type Cache[T any] struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func NewCache

func NewCache[T any](ttlSeconds int64) *Cache[T]

func (*Cache[T]) Get

func (c *Cache[T]) Get(key string) (T, bool)

func (*Cache[T]) Set

func (c *Cache[T]) Set(key string, value T)

type CacheEntry

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

type RichError

type RichError struct {
	// contains filtered or unexported fields
}

func NewRichError

func NewRichError(error string, payload any) *RichError

func (*RichError) Error

func (r *RichError) Error() string

func (*RichError) Payload

func (r *RichError) Payload() any

type Set

type Set[K cmp.Ordered] map[K]struct{}

func NewSet

func NewSet[K cmp.Ordered](values ...K) Set[K]

func (Set[K]) Clear

func (s Set[K]) Clear()

func (Set[K]) Clone

func (s Set[K]) Clone() Set[K]

func (Set[K]) Contains

func (s Set[K]) Contains(key K) bool

func (Set[K]) Equals

func (s Set[K]) Equals(other Set[K]) bool

func (Set[K]) Hash

func (s Set[K]) Hash() string

func (Set[K]) Put

func (s Set[K]) Put(key K)

func (Set[K]) PutAll

func (s Set[K]) PutAll(keys []K)

func (Set[K]) PutAllKeys

func (s Set[K]) PutAllKeys(m map[K]any)

func (Set[K]) PutSet

func (s Set[K]) PutSet(keys Set[K])

func (Set[K]) Remove

func (s Set[K]) Remove(key K)

func (Set[K]) Size

func (s Set[K]) Size() int

func (Set[K]) ToSlice

func (s Set[K]) ToSlice() []K

type Ticker

type Ticker struct {
	C      chan time.Time
	Closed chan struct{}
	// contains filtered or unexported fields
}

func NewTicker

func NewTicker(d time.Duration, startDelay time.Duration) *Ticker

func (*Ticker) Period

func (t *Ticker) Period() time.Duration

func (*Ticker) Stop

func (t *Ticker) Stop()

Stop turns off a ticker. After Stop, no more ticks will be sent.

Jump to

Keyboard shortcuts

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