Documentation
¶
Overview ¶
Package devtoolkit provides a collection of utilities for Golang development.
Index ¶
- Variables
- func CastToPointer[T any](v any) (*T, bool)
- func Contains[T comparable](slice []T, item T) bool
- func ContainsWithPredicate[T any](slice []T, item T, predicate func(T, T) bool) bool
- func DefaultIfNil[T any](a *T, b T) T
- func Difference[T comparable](slice, other []T) []T
- func Filter[T any](slice []T, predicate func(T) bool) []T
- func FilterNot[T any](slice []T, predicate func(T) bool) []T
- func GetMapKeys[K comparable, V any](m map[K]V) []K
- func GetMapValues[K comparable, V comparable](m map[K]V, removeDuplicates bool) []V
- func IfThenElse[T any](condition bool, a, b T) T
- func IfThenElseFn[T any](condition bool, a, b func() T) T
- func IndexOf[T comparable](slice []T, item T) int
- func IndexOfWithPredicate[T any](slice []T, item T, predicate func(T, T) bool) int
- func Intersection[T comparable](slice, other []T) []T
- func IsZero(t any) bool
- func LastIndexOf[T comparable](slice []T, item T) int
- func LastIndexOfWithPredicate[T any](slice []T, item T, predicate func(T, T) bool) int
- func LoadPropFile(filePath string, props []ToolKitProp) error
- func Map[T, R any](slice []T, mapper func(T) R) []R
- func MapToStruct[T any](m map[string]any) (*T, error)
- func RegisterCustomValidator(name string, fn func(fl validator.FieldLevel) bool)
- func Remove[T comparable](slice []T, item T) bool
- func RemoveAll[T comparable](slice []T, item T) bool
- func RemoveAllWithPredicate[T any](slice []T, item T, predicate func(T, T) bool) bool
- func RemoveAt[T any](slice []T, index int) bool
- func RemoveDuplicates[T comparable](slice []T) bool
- func RemoveIf[T any](slice []T, predicate func(T) bool) bool
- func RemoveRange[T any](slice []T, start, end int) bool
- func RemoveWithPredicate[T any](slice []T, item T, predicate func(T, T) bool) bool
- func Reverse[T any](slice []T)
- func StrToStruct[T any](s string) (*T, error)
- func StructToMap(t any) (map[string]any, error)
- func ToFloat64(value any) (float64, bool)
- func ToInt(value any) (int, bool)
- func ToPtr[T any](t T) *T
- func Union[T comparable](slice, other []T) []T
- func ZeroValue[T any]() T
- type AtomicNumber
- func (a *AtomicNumber[T]) Decrement()
- func (a *AtomicNumber[T]) DecrementAndGet() T
- func (a *AtomicNumber[T]) DecrementBy(n T)
- func (a *AtomicNumber[T]) DecrementByAndGet(n T) T
- func (a *AtomicNumber[T]) DecrementByIf(n T, cond func(T) bool) bool
- func (a *AtomicNumber[T]) DecrementIf(cond func(T) bool) bool
- func (a *AtomicNumber[T]) EqualTo(value T) bool
- func (a *AtomicNumber[T]) Get() T
- func (a *AtomicNumber[T]) GreaterThan(value T) bool
- func (a *AtomicNumber[T]) Increment()
- func (a *AtomicNumber[T]) IncrementAndGet() T
- func (a *AtomicNumber[T]) IncrementBy(n T)
- func (a *AtomicNumber[T]) IncrementByAndGet(n T) T
- func (a *AtomicNumber[T]) IncrementByIf(n T, cond func(T) bool) bool
- func (a *AtomicNumber[T]) IncrementIf(cond func(T) bool) bool
- func (a *AtomicNumber[T]) LessThan(value T) bool
- func (a *AtomicNumber[T]) Set(value T)
- type ConcurrentExec
- func (ce *ConcurrentExec) CancelExecution()
- func (ce *ConcurrentExec) Done() <-chan struct{}
- func (ce *ConcurrentExec) Errors() []error
- func (ce *ConcurrentExec) ExecuteFns(ctx context.Context, fns ...ConcurrentFn) (ConcurrentExecResponse, error)
- func (ce *ConcurrentExec) GetNotNilErrors() []error
- func (ce *ConcurrentExec) Results() []any
- type ConcurrentExecResponse
- type ConcurrentFn
- type ConcurrentManager
- type ConcurrentWorkers
- type LinkFn
- type Number
- type Pair
- type ProcessChain
- type ProcessChainOptions
- type Resilience
- type ResilienceOptions
- type SaveStep
- type ToolKitProp
- type Triple
Constants ¶
This section is empty.
Variables ¶
var ( ConcurrentExecAlreadyRunningErr = errors.New("concurrent fns already running") ConcurrentExecNilContextErr = errors.New("context must not be nil") ConcurrentExecFnsNilOrEmptyErr = errors.New("fns must not be nil or empty") )
Error values that can be returned by ConcurrentExec.
var (
ErrNilLinkFn = errors.New("nil link function")
)
Functions ¶
func CastToPointer ¶ added in v1.4.0
CastToPointer casts the given value to a pointer of the given type. v must be a pointer. if v not a pointer, returns false. if v is nil, returns false. if v is a pointer but not of the given type, returns false. if v is a pointer of the given type, returns true.
func Contains ¶
func Contains[T comparable](slice []T, item T) bool
Contains checks if a slice contains an item. Item must be comparable.
func ContainsWithPredicate ¶
ContainsWithPredicate checks if a slice contains an item. Use predicate to compare items.
func DefaultIfNil ¶ added in v1.4.0
func DefaultIfNil[T any](a *T, b T) T
DefaultIfNil returns 'a' if 'a' is not nil, otherwise returns 'b'.
func Difference ¶
func Difference[T comparable](slice, other []T) []T
Difference returns a new slice containing all items from slice that are not present in other.
func Filter ¶
Filter returns a new slice containing all items from slice for which predicate returns true.
func FilterNot ¶
FilterNot returns a new slice containing all items from slice for which predicate returns false.
func GetMapKeys ¶ added in v1.4.0
func GetMapKeys[K comparable, V any](m map[K]V) []K
GetMapKeys returns a new slice containing all keys from the given map.
func GetMapValues ¶ added in v1.4.0
func GetMapValues[K comparable, V comparable](m map[K]V, removeDuplicates bool) []V
GetMapValues returns a new slice containing all values from the given map. Note that the order of values is not guaranteed
func IfThenElse ¶ added in v1.4.0
IfThenElse returns 'a' if condition is true, otherwise returns 'b'.
func IfThenElseFn ¶ added in v1.4.0
IfThenElseFn returns 'a' if condition is true, otherwise returns 'b'.
func IndexOf ¶
func IndexOf[T comparable](slice []T, item T) int
IndexOf returns the index of the first instance of item in slice, or -1 if item is not present in slice.
func IndexOfWithPredicate ¶
IndexOfWithPredicate returns the index of the first instance of item in slice, or -1 if item is not present in slice. Use predicate to compare items.
func Intersection ¶
func Intersection[T comparable](slice, other []T) []T
Intersection returns a new slice containing all items from slice that are also present in other.
func LastIndexOf ¶
func LastIndexOf[T comparable](slice []T, item T) int
LastIndexOf returns the index of the last instance of item in slice, or -1 if item is not present in slice.
func LastIndexOfWithPredicate ¶
LastIndexOfWithPredicate returns the index of the last instance of item in slice, or -1 if item is not present in slice. Use predicate to compare items.
func LoadPropFile ¶
func LoadPropFile(filePath string, props []ToolKitProp) error
LoadPropFile loads configuration properties from a file into the provided slice of structs. The file format can be either YAML or JSON. The 'filePath' parameter specifies the path to the configuration file. The 'props' parameter is a slice of pointers to struct instances that should be populated with the loaded properties. Returns an error if the file cannot be loaded, parsed, or is of an unsupported format.
func Map ¶
func Map[T, R any](slice []T, mapper func(T) R) []R
Map returns a new slice containing the results of applying the given mapper function to each item in slice.
func MapToStruct ¶ added in v1.4.0
MapToStruct converts a map[string]any to a struct.
func RegisterCustomValidator ¶ added in v1.2.0
RegisterCustomValidator registers a custom validator function with the validator.
func Remove ¶
func Remove[T comparable](slice []T, item T) bool
Remove removes the first instance of item from slice, if present. Returns true if item was removed, false otherwise.
func RemoveAll ¶
func RemoveAll[T comparable](slice []T, item T) bool
RemoveAll removes all instances of item from slice, if present. Returns true if item was removed, false otherwise.
func RemoveAllWithPredicate ¶
RemoveAllWithPredicate removes all instances of item from slice, if present. Use predicate to compare items. Returns true if item was removed, false otherwise.
func RemoveAt ¶
RemoveAt removes the item at the given index from slice. Returns true if item was removed, false otherwise.
func RemoveDuplicates ¶
func RemoveDuplicates[T comparable](slice []T) bool
RemoveDuplicates removes all duplicate items from slice. Returns true if items were removed, false otherwise.
func RemoveIf ¶
RemoveIf removes all items from slice for which predicate returns true. Returns true if items were removed, false otherwise.
func RemoveRange ¶
RemoveRange removes the items in the given range from slice. Returns true if items were removed, false otherwise.
func RemoveWithPredicate ¶
RemoveWithPredicate removes the first instance of item from slice, if present. Use predicate to compare items. Returns true if item was removed, false otherwise.
func StrToStruct ¶ added in v1.4.0
StrToStruct converts a string to a struct.
func StructToMap ¶ added in v1.4.0
StructToMap converts a struct to a map[string]any.
func ToInt ¶ added in v1.4.0
ToInt converts the given value to int. Converts float64, float32, int, int64, int32, int16, int8, uint, uint64, uint32, uint16, uint8 to int.
func Union ¶
func Union[T comparable](slice, other []T) []T
Union returns a new slice containing all items from slice and other.
Types ¶
type AtomicNumber ¶ added in v1.4.0
type AtomicNumber[T Number] struct { // contains filtered or unexported fields }
AtomicNumber provides a concurrency-safe way to work with numeric values.
func (*AtomicNumber[T]) Decrement ¶ added in v1.4.0
func (a *AtomicNumber[T]) Decrement()
Decrement decreases the AtomicNumber's value by 1.
func (*AtomicNumber[T]) DecrementAndGet ¶ added in v1.4.0
func (a *AtomicNumber[T]) DecrementAndGet() T
DecrementAndGet decreases the AtomicNumber's value by 1 and returns the new value.
func (*AtomicNumber[T]) DecrementBy ¶ added in v1.4.0
func (a *AtomicNumber[T]) DecrementBy(n T)
DecrementBy decreases the AtomicNumber's value by the provided amount.
func (*AtomicNumber[T]) DecrementByAndGet ¶ added in v1.4.0
func (a *AtomicNumber[T]) DecrementByAndGet(n T) T
DecrementByAndGet decreases the AtomicNumber's value by the provided amount and returns the new value.
func (*AtomicNumber[T]) DecrementByIf ¶ added in v1.4.0
func (a *AtomicNumber[T]) DecrementByIf(n T, cond func(T) bool) bool
DecrementByIf decreases the AtomicNumber's value by the provided amount if the provided condition is true. It returns true if the value was decremented.
func (*AtomicNumber[T]) DecrementIf ¶ added in v1.4.0
func (a *AtomicNumber[T]) DecrementIf(cond func(T) bool) bool
DecrementIf decreases the AtomicNumber's value by 1 if the provided condition is true. It returns true if the value was decremented.
func (*AtomicNumber[T]) EqualTo ¶ added in v1.4.0
func (a *AtomicNumber[T]) EqualTo(value T) bool
EqualTo returns true if the AtomicNumber's value is equal to the provided value.
func (*AtomicNumber[T]) Get ¶ added in v1.4.0
func (a *AtomicNumber[T]) Get() T
Get returns the current value of the AtomicNumber.
func (*AtomicNumber[T]) GreaterThan ¶ added in v1.4.0
func (a *AtomicNumber[T]) GreaterThan(value T) bool
GreaterThan returns true if the AtomicNumber's value is greater than the provided value.
func (*AtomicNumber[T]) Increment ¶ added in v1.4.0
func (a *AtomicNumber[T]) Increment()
Increment increases the AtomicNumber's value by 1.
func (*AtomicNumber[T]) IncrementAndGet ¶ added in v1.4.0
func (a *AtomicNumber[T]) IncrementAndGet() T
IncrementAndGet increases the AtomicNumber's value by 1 and returns the new value.
func (*AtomicNumber[T]) IncrementBy ¶ added in v1.4.0
func (a *AtomicNumber[T]) IncrementBy(n T)
IncrementBy increases the AtomicNumber's value by the provided amount.
func (*AtomicNumber[T]) IncrementByAndGet ¶ added in v1.4.0
func (a *AtomicNumber[T]) IncrementByAndGet(n T) T
IncrementByAndGet increases the AtomicNumber's value by the provided amount and returns the new value.
func (*AtomicNumber[T]) IncrementByIf ¶ added in v1.4.0
func (a *AtomicNumber[T]) IncrementByIf(n T, cond func(T) bool) bool
IncrementByIf increases the AtomicNumber's value by the provided amount if the provided condition is true. It returns true if the value was incremented.
func (*AtomicNumber[T]) IncrementIf ¶ added in v1.4.0
func (a *AtomicNumber[T]) IncrementIf(cond func(T) bool) bool
IncrementIf increases the AtomicNumber's value by 1 if the provided condition is true. It returns true if the value was incremented.
func (*AtomicNumber[T]) LessThan ¶ added in v1.4.0
func (a *AtomicNumber[T]) LessThan(value T) bool
LessThan returns true if the AtomicNumber's value is less than the provided value.
func (*AtomicNumber[T]) Set ¶ added in v1.4.0
func (a *AtomicNumber[T]) Set(value T)
Set updates the value of the AtomicNumber.
type ConcurrentExec ¶
type ConcurrentExec struct {
// contains filtered or unexported fields
}
ConcurrentExec allows to execute a slice of ConcurrentFn concurrently. The running state, results, errors and context for the concurrent execution are stored within the struct.
func NewConcurrentExec ¶ added in v1.1.1
func NewConcurrentExec() *ConcurrentExec
func (*ConcurrentExec) CancelExecution ¶
func (ce *ConcurrentExec) CancelExecution()
func (*ConcurrentExec) Done ¶
func (ce *ConcurrentExec) Done() <-chan struct{}
func (*ConcurrentExec) Errors ¶
func (ce *ConcurrentExec) Errors() []error
func (*ConcurrentExec) ExecuteFns ¶
func (ce *ConcurrentExec) ExecuteFns(ctx context.Context, fns ...ConcurrentFn) (ConcurrentExecResponse, error)
ExecuteFns receives a context and a slice of functions to execute concurrently. It returns a ConcurrentExecResponse interface and an error if execution could not be started.
func (*ConcurrentExec) GetNotNilErrors ¶ added in v1.4.0
func (ce *ConcurrentExec) GetNotNilErrors() []error
func (*ConcurrentExec) Results ¶
func (ce *ConcurrentExec) Results() []any
type ConcurrentExecResponse ¶
type ConcurrentExecResponse interface { // Results blocks until all functions are done and returns the results. Results() []any // blocks until all fns are done // Errors blocks until all functions are done and returns any errors that occurred. Errors() []error // blocks until all fns are done // GetNotNilErrors blocks until all functions are done and returns any errors that occurred that are not nil. GetNotNilErrors() []error // blocks until all fns are done // CancelExecution cancels the execution of all functions. CancelExecution() // cancels the execution of all fns // Done returns a channel that is closed when all functions are done. Done() <-chan struct{} // returns a channel that is closed when all fns are done }
ConcurrentExecResponse is the interface returned by ExecuteFns to interact with the results of the concurrent execution.
type ConcurrentFn ¶
ConcurrentFn represents a function that can be executed concurrently. The function receives a context and returns a result and an error.
type ConcurrentManager ¶ added in v1.4.0
type ConcurrentManager struct {
// contains filtered or unexported fields
}
ConcurrentManager is a structure that manages a dynamic pool of workers. It can adjust the number of active workers based on the workload, within the provided minimum and maximum limits.
func NewConcurrentManager ¶ added in v1.4.0
func NewConcurrentManager(minWorkers, maxWorkers int, workerIncreaseRate float64, timeIncreasePeriod time.Duration) *ConcurrentManager
NewConcurrentManager creates a new instance of ConcurrentManager with specified parameters. It ensures that the provided parameters are within acceptable ranges and initializes the manager.
func (*ConcurrentManager) Allocate ¶ added in v1.4.0
func (c *ConcurrentManager) Allocate()
Allocate requests a new worker to be allocated. It blocks if the maximum number of workers has been reached, until a worker is released.
func (*ConcurrentManager) Release ¶ added in v1.4.0
func (c *ConcurrentManager) Release()
Release frees up a worker, making it available for future tasks. It only releases a worker if the release condition is met, ensuring resources are managed efficiently.
func (*ConcurrentManager) Wait ¶ added in v1.4.0
func (c *ConcurrentManager) Wait()
Wait blocks until all workers have finished their tasks. It ensures that all resources are properly cleaned up before shutting down or reinitializing the manager.
type ConcurrentWorkers ¶ added in v1.4.0
type ConcurrentWorkers struct {
// contains filtered or unexported fields
}
func NewConcurrentWorkers ¶ added in v1.4.0
func NewConcurrentWorkers(maxWorkers int) *ConcurrentWorkers
func (*ConcurrentWorkers) Execute ¶ added in v1.4.0
func (cw *ConcurrentWorkers) Execute(fn func())
func (*ConcurrentWorkers) GetError ¶ added in v1.4.0
func (cw *ConcurrentWorkers) GetError() error
func (*ConcurrentWorkers) IsOpen ¶ added in v1.4.0
func (cw *ConcurrentWorkers) IsOpen() bool
func (*ConcurrentWorkers) Stop ¶ added in v1.4.0
func (cw *ConcurrentWorkers) Stop(err error)
func (*ConcurrentWorkers) Wait ¶ added in v1.4.0
func (cw *ConcurrentWorkers) Wait()
type Number ¶ added in v1.4.0
type Number interface { constraints.Integer | constraints.Float }
Number is a type constraint that allows any type which is either an Integer or a Float.
type ProcessChain ¶ added in v1.4.0
type ProcessChain[T any] interface { // AddLink adds a new link, identified by a string key, to the chain of operations. // It returns an error if the provided link function is nil. AddLink(string, LinkFn[T]) error // SetSaveStep sets a save step function that is executed after each link in the chain. // This step is used to persist the state of the data after each operation. SetSaveStep(SaveStep[T]) // GetChain returns a slice of string keys representing the sequence of links added to the chain. GetChain() []string // Execute runs the process chain on data of type T, sequentially executing // each link in the order they were added. // It returns a slice of string keys representing the successfully executed links and an error if the execution // of any link fails. Execute(context.Context, T) ([]string, error) // ExecuteWithIgnorableLinks runs the process chain on data of type T, sequentially executing // each link in the order they were added, except for the ignorable links. // It returns a slice of string keys representing the successfully executed links and an error if the execution // of any link fails. ExecuteWithIgnorableLinks(context.Context, T, []string) ([]string, error) }
ProcessChain defines an interface for a chain of operations (links) that can be executed on data of type T. It allows adding links, setting a save step, executing the chain, and retrieving the sequence of added links.
func NewProcessChain ¶ added in v1.4.0
func NewProcessChain[T any](opts *ProcessChainOptions) ProcessChain[T]
NewProcessChain creates and returns a new instance of a process chain for data of type T.
type ProcessChainOptions ¶ added in v1.4.0
type ProcessChainOptions struct {
AddLinkNameToError bool // default: false
}
type Resilience ¶ added in v1.1.0
Resilience provides an interface for retrying operations in case of failure.
func NewResilience ¶ added in v1.1.0
func NewResilience(options *ResilienceOptions) (Resilience, error)
NewResilience returns a new Resilience instance with the provided options or defaults.
type ResilienceOptions ¶ added in v1.1.0
type ResilienceOptions struct { MaxRetries int // indicates the maximum number of retries. Default is 3. WaitTime time.Duration // indicates the wait time between retries. Default is 100ms. Backoff bool // indicates whether to use exponential backoff. Default is false. RawError bool // indicates whether to return the raw error or wrap it in a new error. Default is false. IsIgnorableErrorHandler func(error) bool // indicates whether to ignore the error or not. Default is nil. ReturnIgnorable bool // indicates whether to return the ignorable error or not. Default is false. }
ResilienceOptions contains configuration parameters for retry operations.
type ToolKitProp ¶ added in v1.3.0
type ToolKitProp interface {
SetDefaults()
}
ToolKitProp is an interface that must be implemented by all configuration property structs. It provides a method to set default values for the properties.