utils

package module
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Dec 24, 2024 License: MIT Imports: 15 Imported by: 8

README

go-utils

Simple utilities

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, int, A)) 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 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 comparable](a A, fn func(T, int, A) bool) A

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 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, int, A) O,
) A

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 MapToType added in v0.0.4

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

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 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[I ~[]V, V any, T any](
	a I, fn func(V, int, I) 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 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 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 SliceFindFunc added in v0.0.4

func SliceFindFunc[A ~[]I, I any](a A, fn func(I, int, A) 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 SliceMapFunc added in v0.0.4

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

SliceMapFunc 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 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{})

Jump to

Keyboard shortcuts

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