utils

package
v0.0.0-...-a4f0009 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2024 License: AGPL-3.0 Imports: 24 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultTimeout for http requests
	DefaultTimeout = 120 * time.Second
	// MaxRetries for http requests
	MaxRetries = 5
	// RetryDelay for http requests
	RetryDelay = 5 * time.Second
)
View Source
const UnknownLang = "-"

Variables

This section is empty.

Functions

func Chunks

func Chunks[T any](slice []T, chunkSize int) [][]T

Chunks divides slice by chunks with specified size

func ConstantTimeEq

func ConstantTimeEq(s1, s2 string) bool

ConstantTimeEq checks if 2 strings are equal in constant time

func DetectLanguage

func DetectLanguage(detector lingua.LanguageDetector, text string) (langCode string, confidence float64)

DetectLanguage and return it's ISO 639-1 code and confidence

func Do

func Do(req *http.Request, maxRetries ...int) (*http.Response, error)

Do performs HTTP request with timeout, User-Agent, and retrier

func Get

func Get(ctx context.Context, uri string, maxRetries ...int) (*http.Response, error)

Get performs HTTP GET request with timeout, User-Agent, and retrier

func JSON

func JSON(input any) ([]byte, error)

JSON marshals input into canonical json

func MapKeys

func MapKeys[K comparable, V any](datamap map[K]V) []K

MapKeys returns keys of the map

func MarkdownEmail

func MarkdownEmail(email string) string

MarkdownEmail returns markdown link to email

func MarkdownLink(link string) string

MarkdownLink returns markdown link

func MarkdownMXID

func MarkdownMXID(mxid string) string

MarkdownMXID returns markdown link to MXID

func MarkdownRender

func MarkdownRender(mdtext string) (text, html string)

MarkdownRender coverts markdown text into text and html forms

func MayTemplate

func MayTemplate(tplString string, vars any) string

func MergeSlices

func MergeSlices[K comparable](slices ...[]K) []K

MergeSlices and remove duplicates

func NewContext

func NewContext(parent ...context.Context) context.Context

NewContext creates a new context with a logger and sentry hub

func ParseURL

func ParseURL(uri string) *url.URL

ParseURL parses a URL and returns a URL structure

func PoolProgress

func PoolProgress(wp *workpool.WorkPool, callback func(), optionalDuration ...time.Duration)

func RemoveFromSlice

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

RemoveFromSlice removes elements of toRemove from base slice

func ServerFrom

func ServerFrom(matrixID string) string

Server returns server name from the matrix ID (room id/alias, user ID, etc)

func SetLogLevel

func SetLogLevel(level string)

SetLogLevel sets the log level

func SetName

func SetName(name string)

SetName sets the name of the application

func SetSentryDSN

func SetSentryDSN(dsn string)

SetSentryDSN sets the sentry DSN

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 StartSpan

func StartSpan(ctx context.Context, operation string) *sentry.Span

StartSpan starts a new span, and if there is no transaction, it starts a new transaction

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 Template

func Template(tplString string, vars any) (string, error)

Template parses template

func Truncate

func Truncate(s string, length int) string

Truncate string

func URLSafeDecode

func URLSafeDecode(safeString string) string

URLSafeDecode decodes url-safe string into the original form

func URLSafeEncode

func URLSafeEncode(unsafeString string) string

URLSafeEncode encodes url-unsafe string into url-safe form

func Uniq

func Uniq[T comparable](slice []T) []T

Uniq removes duplicates from slice

func ValuesOrDefault

func ValuesOrDefault(values url.Values, defaultEncoded string) string

ValuesOrDefault returns the encoded values or the default encoded values

Types

type List

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

List is unique list

func NewList

func NewList[T comparable, V any]() *List[T, V]

NewList creates new list

func NewListFromSlice

func NewListFromSlice[T comparable](slice []T) *List[T, T]

NewListFromSlice 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

Jump to

Keyboard shortcuts

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