kit

package module
v1.7.7 Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2026 License: LGPL-3.0 Imports: 18 Imported by: 2

README

kit - auxilary utilities

  • auxilary functions: Go Reference
  • format subpackage: Go Reference
  • retry subpackage: Go Reference
  • template subpackage: Go Reference
  • workpool subpackage: Go Reference

This is a shared utils package, used by other etke.cc projects. Check godoc for the details

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrUnknown = errors.New("unknown error")

ErrUnknown represents an unknown error

Functions

func AnonymizeIP added in v1.7.0

func AnonymizeIP(ip string) string

AnonymizeIP drops the last octet of the IPv4 and IPv6 address to anonymize it

func Chunk

func Chunk[T any](items []T, chunkSize int) (chunks [][]T)

Chunk slices into chunks

func Eq

func Eq(s1, s2 string) bool

Eq checks if 2 strings are equal in constant time

func Hash

func Hash(str string) string

Hash returns sha256 hash of a string

func IsNil added in v1.7.6

func IsNil(i any) bool

IsNil checks if the given value is nil, including interfaces holding nil pointers

func IsValidIP added in v1.7.4

func IsValidIP(ipStr string) bool

IsValidIP checks if the given string is a valid IPv4 or IPv6 address by: - ensuring the format is correct - ensuring the IP is not an unspecified, loopback, private, multicast, or link-local address

func MapFromSlice added in v1.7.2

func MapFromSlice[T cmp.Ordered](slice []T) map[T]bool

MapFromSlice creates a map from slice elements as keys The map values are set to true, indicating the presence of the key. This is useful for quickly checking if a key exists in the map.

func MapKeys

func MapKeys[T cmp.Ordered, V any](data map[T]V) []T

MapKeys returns map keys only

func MergeMapKeys

func MergeMapKeys[V any](m map[string]V, adds ...map[string]V) []string

MergeMapKeys returns map keys only from multiple maps

func MergeSlices

func MergeSlices[K cmp.Ordered](slices ...[]K) []K

MergeSlices and remove duplicates

func RemoveFromSlice

func RemoveFromSlice[K comparable](base, toRemove []K) []K

RemoveFromSlice removes elements of toRemove from base slice

func Reverse added in v1.3.0

func Reverse[T any](slice []T)

Reverse slice

func SliceToString

func SliceToString(slice []string, delimiter string, hook func(string) string) string

SliceToString converts slice of strings into single string (using strings.Join) with optional hook

func StringToInt

func StringToInt(value string, optionalDefaultValue ...int) int

StringToInt converts string to int with optional default value

func StringToSlice

func StringToSlice(value string, optionalDefaultValue ...string) []string

StringToSlice converts comma-separated string to slice with optional default value

func Truncate

func Truncate(s string, length int) string

Truncate truncates a string to a specified length, adding "..." if truncated

func Uniq

func Uniq(slice []string) []string

Uniq removes duplicates from slice

func Unquote

func Unquote(s string) string

Unquote is a wrapper around strconv.Unquote, but returns original string if unquoting fails

Types

type AggregateError added in v1.7.7

type AggregateError struct {
	Errors []error
	// contains filtered or unexported fields
}

AggregateError represents an aggregate of multiple errors

func NewAggregateError added in v1.7.7

func NewAggregateError(errs ...error) *AggregateError

NewAggregateError creates a new aggregate error

func (*AggregateError) As added in v1.7.7

func (a *AggregateError) As(target any) bool

As checks if the target error type is in the aggregate

func (*AggregateError) Error added in v1.7.7

func (a *AggregateError) Error() string

Error returns the aggregated error messages

func (*AggregateError) Is added in v1.7.7

func (a *AggregateError) Is(target error) bool

Is checks if the target error is in the aggregate

func (*AggregateError) Join added in v1.7.7

func (a *AggregateError) Join(errs ...error) *AggregateError

Join adds errors to the aggregate and returns nil if no errors were added

func (*AggregateError) Unwrap added in v1.7.7

func (a *AggregateError) Unwrap() []error

Unwrap returns the underlying errors WARNING: This method returns original slice. Do not modify it.

type ErrorResponse

type ErrorResponse struct {
	StatusCode int    `json:"-"`     // HTTP status code, optional, not serialized
	Err        string `json:"error"` // Error message
	// contains filtered or unexported fields
}

ErrorResponse represents an error response

func NewErrorResponse

func NewErrorResponse(err error, optionalStatusCode ...int) *ErrorResponse

NewErrorResponse creates a new error response

func (ErrorResponse) Error

func (e ErrorResponse) Error() string

Error returns the error message

func (ErrorResponse) Unwrap added in v1.7.7

func (e ErrorResponse) Unwrap() error

Unwrap returns the underlying error

type List

type List[T cmp.Ordered, V any] struct {
	// contains filtered or unexported fields
}

List is unique list, V is used only for AddMapKeys

func NewList

func NewList[T cmp.Ordered, V any]() *List[T, V]

NewList creates new list

func NewListFrom

func NewListFrom[T cmp.Ordered](slice []T) *List[T, T]

NewListFrom creates a new list and popupates it from slice

func (*List[T, V]) Add

func (l *List[T, V]) Add(item T)

Add item to the list

func (*List[T, V]) AddMapKeys

func (l *List[T, V]) AddMapKeys(datamap map[T]V)

AddMapKeys adds keys from map to the list

func (*List[T, V]) AddSlice

func (l *List[T, V]) AddSlice(dataslice []T)

AddSlice adds keys from slice to the list

func (*List[T, V]) Len

func (l *List[T, V]) Len() int

Len - data length

func (*List[T, V]) Remove

func (l *List[T, V]) Remove(item T)

Remove item from list

func (*List[T, V]) RemoveSlice

func (l *List[T, V]) RemoveSlice(dataslice []T)

RemoveSlice removes items from data using slice

func (*List[T, V]) Slice

func (l *List[T, V]) Slice() []T

Slice returns list data as slice

type MatrixError

type MatrixError struct {
	Code string `json:"errcode"`
	Err  string `json:"error"`
}

MatrixError represents an error response from the Matrix API

func MatrixErrorFrom

func MatrixErrorFrom(r io.Reader) *MatrixError

MatrixErrorFrom creates a new Matrix error from io.Reader

func NewMatrixError

func NewMatrixError(code, err string) *MatrixError

NewMatrixError creates a new Matrix error

func (MatrixError) Error

func (e MatrixError) Error() string

Error returns the error message

type Mutex added in v1.4.0

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

Mutex is a key-based mutex that allows locking and unlocking based on a key.

func NewMutex added in v1.4.0

func NewMutex() *Mutex

NewMutex creates a new Mutex instance

func (*Mutex) Lock added in v1.4.0

func (km *Mutex) Lock(key string)

Lock locks the mutex for a specific key.

func (*Mutex) Unlock added in v1.4.0

func (km *Mutex) Unlock(key string)

Unlock unlocks the mutex for a specific key.

type StringsBuilder added in v1.7.5

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

StringsBuilder is a wrapper around strings.Builder with short syntax, it also exposes all methods of strings.Builder, so you can use it as a drop-in replacement

func NewStringsBuilder added in v1.7.5

func NewStringsBuilder() StringsBuilder

NewStringsBuilder creates a new StringsBuilder instance

func (*StringsBuilder) B added in v1.7.5

B is shortcut for WriteByte

func (*StringsBuilder) Cap added in v1.7.5

func (sb *StringsBuilder) Cap() int

Cap returns the current capacity of the accumulated string

func (*StringsBuilder) Grow added in v1.7.5

func (sb *StringsBuilder) Grow(n int) *StringsBuilder

Grow grows sb's capacity, if necessary, to guarantee space for another n bytes

func (*StringsBuilder) Len added in v1.7.5

func (sb *StringsBuilder) Len() int

Len returns the current length of the accumulated string

func (*StringsBuilder) R added in v1.7.5

R is shortcut for WriteRune

func (*StringsBuilder) Reset added in v1.7.5

func (sb *StringsBuilder) Reset() *StringsBuilder

Reset resets the StringsBuilder to be empty and returns itself

func (*StringsBuilder) S added in v1.7.5

S is shortcut for WriteString

func (*StringsBuilder) String added in v1.7.5

func (sb *StringsBuilder) String() string

String returns the accumulated string

func (*StringsBuilder) Unwrap added in v1.7.5

func (sb *StringsBuilder) Unwrap() strings.Builder

Unwrap returns the underlying strings.Builder

func (*StringsBuilder) Write added in v1.7.5

func (sb *StringsBuilder) Write(p []byte) (int, error)

Write appends the contents of p to sb's buffer

func (*StringsBuilder) WriteRune added in v1.7.5

func (sb *StringsBuilder) WriteRune(r rune) (int, error)

WriteRune appends the UTF-8 encoding of Unicode code point r to sb's buffer

func (*StringsBuilder) WriteString added in v1.7.5

func (sb *StringsBuilder) WriteString(s string) (int, error)

WriteString appends the contents of s to sb's buffer

type WaitGroup added in v1.7.3

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

WaitGroup is a wrapper around sync.WaitGroup to have some syntax sugar It does not provide any additional functionality

func NewWaitGroup added in v1.7.3

func NewWaitGroup() *WaitGroup

NewWaitGroup creates a new WaitGroup

func (*WaitGroup) Do added in v1.7.3

func (w *WaitGroup) Do(f ...func())

Do runs the given functions in separate goroutines and waits for them to complete

func (*WaitGroup) Get added in v1.7.3

func (w *WaitGroup) Get() *sync.WaitGroup

Get returns the underlying sync.WaitGroup

func (*WaitGroup) Wait added in v1.7.3

func (w *WaitGroup) Wait()

Wait waits for all functions to complete

Directories

Path Synopsis
format module
Package retry provides a simple retry mechanism for Go with exponential backoff and jitter.
Package retry provides a simple retry mechanism for Go with exponential backoff and jitter.
Package template provides a handy wrapper around text/template package.
Package template provides a handy wrapper around text/template package.
package workpool provides a simple work pool implementation
package workpool provides a simple work pool implementation

Jump to

Keyboard shortcuts

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