utils

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2025 License: MIT Imports: 16 Imported by: 8

README

go-utils

Simple utilities

Function suffixes

--A

Functions ends with A are functions that calls predicate functions with current index and original array.

Documentation

Index

Constants

View Source
const (
	PasswordHashTimesName   = "PASSWORD_HASH_TIMES"
	PasswordHashMemoryName  = "PASSWORD_HASH_MEMORY"
	PasswordHashThreadsName = "PASSWORD_HASH_THREADS"
	PasswordHashKeyLenName  = "PASSWORD_HASH_KEY_LENGTH"
	PasswordHashSaltLenName = "PASSWORD_HASH_SALT_LENGTH"
)
View Source
const (
	AlphaNum  = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
	Printable = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_=+[]{}|;:,.<>?/`~"
)

Variables

View Source
var (
	ErrInvalidHashFormat    = errors.New("invalid hash format")
	ErrInvalidHashVersion   = errors.New("invalid hash version")
	ErrInvalidHashAlgorithm = errors.New("invalid hash algorithm")
)
View Source
var Jsoniter = jsoniter.ConfigCompatibleWithStandardLibrary

Functions

func ApplyFunc added in v0.0.7

func ApplyFunc[A ~[]T, T any](a A, fn func(T)) A

ApplyFunc applies the given function to each element of the slice. This function is intended to operate directly on elements in the slice.

func ApplyFuncA added in v0.0.9

func ApplyFuncA[A ~[]T, T any](a A, fn func(T, int, A)) A

ApplyFuncA applies the given function to each element of the slice. This function is intended to operate directly on elements in the slice. The predicate is called with current index and the original slice.

func CloneDeepJsonable added in v0.0.7

func CloneDeepJsonable[P *V, V any](src P) (P, error)

func ComparePassword added in v0.0.4

func ComparePassword(password, encodedHash string) (bool, error)

ComparePassword compares a password with an encoded hash to check if they match.

func ContainsAny added in v0.0.7

func ContainsAny[A ~[]T, T comparable](a A, b A) bool

func FilterFunc added in v0.0.7

func FilterFunc[A ~[]T, T any](a A, fn func(T) bool) A

FilterFunc returns a new slice containing only the elements of the original slice that satisfy the predicate function.

func FilterFuncA added in v0.0.9

func FilterFuncA[A ~[]T, T any](a A, fn func(T, int, A) bool) A

FilterFuncA returns a new slice containing only the elements of the original slice that satisfy the predicate function. The predicate is called with the current element, the index of the current element, and the original slice.

func GetEnv added in v0.0.3

func GetEnv(key, defaultValue string, nonEmpty bool) string

GetEnv returns the value of the environment variable named by the key. It is guaranteed to return the `defaultValue` if the environment variable is not found. If the environment variable is found, it is guaranteed to return the `defaultValue` if the environment variable is empty and `nonEmpty` is `true`.

func GetEnvBool added in v0.0.11

func GetEnvBool(key string, defaultValue bool) (bool, error)

func GetEnvBoolCsv added in v0.0.11

func GetEnvBoolCsv(key string, defaultValue []bool) ([]bool, error)

func GetEnvCsv added in v0.0.11

func GetEnvCsv(key string, defaultValue []string) []string

func GetEnvFloat added in v0.0.11

func GetEnvFloat(key string, defaultValue float64, bitSize int) (
	float64, error,
)

func GetEnvFloat32 added in v0.0.11

func GetEnvFloat32(key string, defaultValue float32) (float32, error)

func GetEnvFloat32Csv added in v0.0.11

func GetEnvFloat32Csv(key string, defaultValue []float32) ([]float32, error)

func GetEnvFloat64 added in v0.0.11

func GetEnvFloat64(key string, defaultValue float64) (float64, error)

func GetEnvFloat64Csv added in v0.0.11

func GetEnvFloat64Csv(key string, defaultValue []float64) ([]float64, error)

func GetEnvFloatCsv added in v0.0.11

func GetEnvFloatCsv(key string, defaultValue []float64, bitSize int) (
	[]float64, error,
)

func GetEnvInt added in v0.0.11

func GetEnvInt(key string, defaultValue int64, bitSize int) (int64, error)

func GetEnvInt16 added in v0.0.11

func GetEnvInt16(key string, defaultValue int16) (int16, error)

func GetEnvInt16Csv added in v0.0.11

func GetEnvInt16Csv(key string, defaultValue []int16) ([]int16, error)

func GetEnvInt32 added in v0.0.11

func GetEnvInt32(key string, defaultValue int32) (int32, error)

func GetEnvInt32Csv added in v0.0.11

func GetEnvInt32Csv(key string, defaultValue []int32) ([]int32, error)

func GetEnvInt64 added in v0.0.11

func GetEnvInt64(key string, defaultValue int64) (int64, error)

func GetEnvInt64Csv added in v0.0.11

func GetEnvInt64Csv(key string, defaultValue []int64) ([]int64, error)

func GetEnvInt8 added in v0.0.11

func GetEnvInt8(key string, defaultValue int8) (int8, error)

func GetEnvInt8Csv added in v0.0.11

func GetEnvInt8Csv(key string, defaultValue []int8) ([]int8, error)

func GetEnvIntCsv added in v0.0.11

func GetEnvIntCsv(key string, defaultValue []int64, bitSize int) (
	[]int64, error,
)

func GetEnvUint added in v0.0.11

func GetEnvUint(key string, defaultValue uint64, bitSize int) (uint64, error)

func GetEnvUint16 added in v0.0.11

func GetEnvUint16(key string, defaultValue uint16) (uint16, error)

func GetEnvUint16Csv added in v0.0.11

func GetEnvUint16Csv(key string, defaultValue []uint16) ([]uint16, error)

func GetEnvUint32 added in v0.0.11

func GetEnvUint32(key string, defaultValue uint32) (uint32, error)

func GetEnvUint32Csv added in v0.0.11

func GetEnvUint32Csv(key string, defaultValue []uint32) ([]uint32, error)

func GetEnvUint64 added in v0.0.11

func GetEnvUint64(key string, defaultValue uint64) (uint64, error)

func GetEnvUint64Csv added in v0.0.11

func GetEnvUint64Csv(key string, defaultValue []uint64) ([]uint64, error)

func GetEnvUint8 added in v0.0.11

func GetEnvUint8(key string, defaultValue uint8) (uint8, error)

func GetEnvUint8Csv added in v0.0.11

func GetEnvUint8Csv(key string, defaultValue []uint8) ([]uint8, error)

func GetEnvUintCsv added in v0.0.11

func GetEnvUintCsv(key string, defaultValue []uint64, bitSize int) (
	[]uint64, error,
)

func GetEnvWithDefault

func GetEnvWithDefault(key, defaultValue string) string

GetEnvWithDefault returns the value of the environment variable named by the key. It is guaranteed to return the default value if the environment variable is not found or is empty.

func GetEnvWithDefaultNE added in v0.0.3

func GetEnvWithDefaultNE(key, defaultValue string) string

GetEnvWithDefaultNE returns the value of the environment variable named by the key. It is guaranteed to return the default value if the environment variable is not found. If the environment variable is found, it is guaranteed to return the default value if the environment variable is empty.

func HashPassword added in v0.0.4

func HashPassword(password string) (string, error)

HashPassword generates a new password hash using the argon2id algorithm.

func HashPasswordWithParams added in v0.0.4

func HashPasswordWithParams(password string, params PasswordHashParams) (
	string, error,
)

HashPassword generates a new password hash using the argon2id algorithm.

func Intersect added in v0.0.7

func Intersect[A ~[]T, T comparable](a A, b A) A

func IntersectFunc added in v0.0.7

func IntersectFunc[A ~[]T, T any, O comparable](a A, b A, fn func(T) O) A

IntersectFunc returns a new slice containing the intersection of two slices based on the result of the predicate function.

func IntersectFuncA added in v0.0.9

func IntersectFuncA[A ~[]T, T any, O comparable](
	a A, b A, fn func(T, int, A) O,
) A

IntersectFuncA returns a new slice containing the intersection of two slices based on the result of the predicate function. The predicate function is called with the current element, the index of the current element, and the original slice.

func JoinInteger added in v0.0.4

func JoinInteger[A ~[]T, T Integer](a A, sep string) string

JoinInteger converts a slice of integers to a string, joining them with the separator.

func JoinNumbersWithFormat added in v0.0.7

func JoinNumbersWithFormat[A ~[]T, T Numbers](a A, sep, format string) string

func MapToAny added in v0.1.0

func MapToAny[T any](val T) (any, error)

MapToAny converts an element of specific type to an element of type any, returning an error if the conversion is not possible. Mainly for use as predicate function in higher-order functions such as SliceMapFuncAE.

func MapToType added in v0.0.4

func MapToType[T any](val any) T

MapToType converts an element of interface{} to an element of specific type, returning an error if the conversion is not possible. Mainly for use as predicate function in higher-order functions such as SliceMapFunc.

func MapToTypeE added in v0.2.0

func MapToTypeE[T any](val any) (T, error)

MapToTypeE converts an element of interface{} to an element of specific type, returning an error if the conversion is not possible. Mainly for use as predicate function in higher-order functions such as SliceMapFuncAE.

func NewUuid added in v0.1.2

func NewUuid() (uuid.UUID, error)

NewUuid returns a new UUID. It tries to generate a UUID using V7, V6, and finally falls back to V4. If all methods fail, it returns an error.

func PanicIfError added in v0.0.2

func PanicIfError(err error)

PanicIfError panics if the error is not nil.

func Pluck added in v0.0.4

func Pluck[A ~[]V, V any, T any](a A, fn func(V) T) []T

Pluck extracts a list of values from a slice of structs. The predicate function fn is applied to each element of the slice, and the result is stored in a new slice.

func PluckA added in v0.0.9

func PluckA[A ~[]V, V any, T any](a A, fn func(V, int, A) T) []T

PluckA extracts a list of values from a slice of structs. The predicate function fn is applied to each element of the slice, and the result is stored in a new slice. The predicate is called with current index and the original slice.

func Ptr added in v0.1.1

func Ptr[T any](v T) *T

Ptr returns a pointer to the value. Use with caution! This function directly returns a pointer to the parameter.

func RandomAlphaNum added in v0.0.4

func RandomAlphaNum(length int) (string, error)

func RandomPrintable added in v0.0.4

func RandomPrintable(length int) (string, error)

func RandomString added in v0.0.4

func RandomString(dict string, length int) (string, error)

func RequestBaseUrl added in v0.0.6

func RequestBaseUrl(request *http.Request) *url.URL

RequestBaseUrl returns the base URL of the request.

func RequestFullUrl added in v0.1.3

func RequestFullUrl(request *http.Request) string

RequestFullUrl returns the full URL of the request.

func RequestUriWithoutSchemeHost added in v0.0.6

func RequestUriWithoutSchemeHost(req *http.Request) *url.URL

RequestUriWithoutSchemeHost returns a new URL instance without scheme and host (domain).

func RequestUrlWithQueryParam added in v0.0.6

func RequestUrlWithQueryParam(
	request *http.Request, name string, value string,
) *url.URL

RequestUrlWithQueryParam returns a new URL instance with the given query parameter set.

func RequestUrlWithQueryParams added in v0.0.6

func RequestUrlWithQueryParams(
	request *http.Request, params map[string]string,
) *url.URL

RequestUrlWithQueryParams returns a new URL instance with the given query parameters set.

func RequestUrlWithoutQueryParams added in v0.0.6

func RequestUrlWithoutQueryParams(
	req *http.Request, params ...string,
) *url.URL

RequestUrlWithoutQueryParams returns a new URL instance without specified query parameters.

func ReturnOrPanic added in v0.0.12

func ReturnOrPanic[T any](v T, err error) T

ReturnOrPanic returns the value if the error is nil, otherwise panics.

func SliceFindFunc added in v0.0.4

func SliceFindFunc[A ~[]I, I any](a A, fn func(I) bool) I

SliceFindFunc returns the first element in the slice that satisfies the predicate function, or the zero value of the element type if none is found.

func SliceFindFuncA added in v0.0.9

func SliceFindFuncA[A ~[]I, I any](a A, fn func(I, int, A) bool) I

SliceFindFuncA returns the first element in the slice that satisfies the predicate function, or the zero value of the element type if none is found. The predicate is called with current index and the original slice.

func SliceMapFunc added in v0.0.4

func SliceMapFunc[AO ~[]O, AI ~[]I, O, I any](a AI, fn func(I) O) AO

SliceMapFunc applies the predicate function to each element of the slice, returning a new slice with the results.

func SliceMapFuncA added in v0.0.9

func SliceMapFuncA[AO ~[]O, AI ~[]I, O, I any](a AI, fn func(I, int, AI) O) AO

SliceMapFuncA applies the predicate function to each element of the slice, returning a new slice with the results. The predicate is called with current index and the original slice.

func SliceMapFuncAE added in v0.2.0

func SliceMapFuncAE[AO ~[]O, AI ~[]I, O, I any](
	a AI, fn func(I, int, AI) (O, error),
) (AO, error)

SliceMapFuncAE applies the predicate function to each element of the slice, returning a new slice with the results. If the predicate function returns an error, the function stops and returns the error. The predicate is called with current index and the original slice.

func SliceMapFuncE added in v0.2.0

func SliceMapFuncE[AO ~[]O, AI ~[]I, O, I any](
	a AI, fn func(I) (O, error),
) (AO, error)

SliceMapFuncE applies the predicate function to each element of the slice, returning a new slice with the results. If the predicate function returns an error, the function stops and returns the error.

func StringContainsAny added in v0.0.4

func StringContainsAny(s string, subs []string) bool

func StringIndexOfAny added in v0.0.4

func StringIndexOfAny(s string, subs []string) int

func Union added in v0.0.10

func Union[A ~[]T, T comparable](a A, b A) A

func UnionFunc added in v0.0.10

func UnionFunc[A ~[]I, I any](a A, b A, fn func(I) bool) A

func UnionFuncA added in v0.0.10

func UnionFuncA[A ~[]I, I any](a A, b A, fn func(I, int, A) bool) A

func UrlWithQueryParam added in v0.0.6

func UrlWithQueryParam(u url.URL, name string, value string) *url.URL

UrlWithQueryParam returns a URL with the given query parameters set.

func UrlWithQueryParams added in v0.0.6

func UrlWithQueryParams(u url.URL, params map[string]string) *url.URL

UrlWithQueryParams returns a URL with the given query parameters set.

func UrlWithoutQueryParams added in v0.0.6

func UrlWithoutQueryParams(u url.URL, params ...string) *url.URL

UrlWithoutQueryParams returns a URL without specified query parameters.

Types

type ComplexNumbers added in v0.0.7

type ComplexNumbers interface {
	~complex64 | ~complex128
}

type Floats added in v0.0.7

type Floats interface {
	~float32 | ~float64
}

type Integer added in v0.0.4

type Integer interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 |
		~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}

Integer is a type constraint that includes all integer types

type Numbers added in v0.0.7

type Numbers interface {
	Integer | Floats | ComplexNumbers
}

type PasswordHashParams added in v0.0.4

type PasswordHashParams struct {
	// Number of iterations
	Times uint32
	// Amount of memory to use, in KB
	Memory uint32
	// Parallelism, number of threads to use
	Threads uint8
	// Key length
	KeyLen uint32
	// Salt length
	SaltLen uint32
}

func DefaultPasswordHashParams added in v0.0.4

func DefaultPasswordHashParams() (*PasswordHashParams, error)

type SimpleTaggedLog added in v0.0.2

type SimpleTaggedLog struct {
	Debug bool
	// contains filtered or unexported fields
}

func NewDebugLogger added in v0.0.2

func NewDebugLogger() SimpleTaggedLog

func NewLogger added in v0.0.2

func NewLogger() SimpleTaggedLog

func WrapLogger added in v0.0.2

func WrapLogger(logger *lg.Logger, debug bool) SimpleTaggedLog

func (SimpleTaggedLog) Debugf added in v0.0.2

func (l SimpleTaggedLog) Debugf(format string, args ...interface{})

func (SimpleTaggedLog) Errorf added in v0.0.2

func (l SimpleTaggedLog) Errorf(format string, args ...interface{})

func (SimpleTaggedLog) Infof added in v0.0.2

func (l SimpleTaggedLog) Infof(format string, args ...interface{})

func (SimpleTaggedLog) PanicIfError added in v0.0.2

func (l SimpleTaggedLog) PanicIfError(err error)

func (SimpleTaggedLog) Panicf added in v0.0.2

func (l SimpleTaggedLog) Panicf(format string, args ...interface{})

type TaggedLogger added in v0.1.1

type TaggedLogger interface {
	Debugf(format string, args ...interface{})
	Errorf(format string, args ...interface{})
	Infof(format string, args ...interface{})
	Panicf(format string, args ...interface{})
	PanicIfError(err error)
}

Jump to

Keyboard shortcuts

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