utils

package
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Jan 23, 2024 License: MIT Imports: 21 Imported by: 1

Documentation

Index

Constants

View Source
const (
	ZeroIPv4 = "0.0.0.0"
	ZeroIPv6 = "0000:0000:0000:0000:0000:0000:0000:0000"
)

Variables

View Source
var (
	ErrLengthsDontMatch = errors.New("lengths don't match")
	ErrTypesDontMatch   = errors.New("types don't match")
	ErrKeysDontMatch    = errors.New("keys don't match")
	ErrValuesDontMatch  = errors.New("values don't match")
)

Functions

func AreSlicesIntersect

func AreSlicesIntersect[T comparable](slice1, slice2 []T) bool

func CloneRequestBody

func CloneRequestBody(r *http.Request) (body string)

func CloneResponseBody

func CloneResponseBody(r *http.Response) (body string)

func CloserFunc

func CloserFunc(r io.Reader, close func() error) io.ReadCloser

func DeepCopy added in v0.0.3

func DeepCopy[T any](v T) (c T, err error)

func DurationUntilNextInterval

func DurationUntilNextInterval(now time.Time, interval time.Duration) time.Duration

func EscapeChars

func EscapeChars(str string, chars map[rune]struct{}) string

func EscapeJSONString added in v0.0.3

func EscapeJSONString(str string, level int) string

func FillEmptyIP

func FillEmptyIP(ipv4, ipv6 string) (string, string)

func First added in v0.0.3

func First[T any](arr []T) (f T)

func FuncName

func FuncName() string

func GetFirst

func GetFirst(str string) string

func GetRequestIP

func GetRequestIP(r *http.Request) (requestIP string)

func GetSecond

func GetSecond(str string) string

func IsInSlice

func IsInSlice[T comparable](i T, slice []T) bool

func JoinIPVersions

func JoinIPVersions(ipv4, ipv6 string) (ip string)

func JoinParams

func JoinParams(first, second string) string

func JoinQuoted

func JoinQuoted(lines []string, quote, separator string) string

func JoinQuotedInt

func JoinQuotedInt(lines []int, quote, separator string) string

func Last added in v0.0.3

func Last[T any](arr []T) (f T)

func Must added in v0.0.3

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

func PanicOnError added in v0.0.3

func PanicOnError(err error)

func ParseFields added in v0.0.3

func ParseFields(structPtr any, parser TagsParser) error

func Range added in v0.0.3

func Range[T int | int8 | int16 | int32 | int64 | float32 | float64](start, end T) []T

Range returns array of values [start, end)

func Retry

func Retry(f func() error, times int)

func RetryOverTime

func RetryOverTime(f func() error, timeout time.Duration, tick time.Duration) error

func SQLReplaceArgs

func SQLReplaceArgs(query string, args ...any) string

func SetCORS

func SetCORS(w http.ResponseWriter)

func SetContentTypeHeader

func SetContentTypeHeader(w http.ResponseWriter, contentType ContentType)

func SplitIPVersions

func SplitIPVersions(ip string) (ipv4, ipv6 string)

func ToUnderscoreACII

func ToUnderscoreACII(s string) (underscored string)

func UnGzipBody

func UnGzipBody(bodyContent []byte) ([]byte, error)

func WrapForCORS added in v0.0.3

func WrapForCORS(handler http.Handler) http.Handler

Types

type Commission

type Commission Percent

func (Commission) AddTo

func (e Commission) AddTo(value float64) float64

func (Commission) SubtractFrom

func (e Commission) SubtractFrom(value float64) float64

type ContentType

type ContentType string
const (
	ContentTypeApplicationJSON ContentType = "application/json"
	ContentTypeTextHTML        ContentType = "text/html"
)

type DeepCopier added in v0.0.3

type DeepCopier interface {
	DeepCopy() interface{}
}

type Diff

type Diff struct {
	Path  string
	Left  string
	Right string
	Error error
}

type Diffs

type Diffs []Diff

func CompareJSON

func CompareJSON(leftJSON, rightJSON string) (diff Diffs, err error)

func (Diffs) String

func (ds Diffs) String() string

type Distribution

type Distribution []float64

func (*Distribution) GetMax

func (d *Distribution) GetMax() float64

func (*Distribution) GetMin

func (d *Distribution) GetMin() float64

func (*Distribution) GetQuantileValue

func (d *Distribution) GetQuantileValue(quantileNumber uint) float64

func (*Distribution) TrimMax

func (d *Distribution) TrimMax(difference float64)

TrimMax sorts and trims max values if difference is too big, difference 0.25 means 25% between max values

type ExpiringStorage added in v0.0.4

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

func NewExpiringStorage added in v0.0.4

func NewExpiringStorage[K comparable, V any](expirationTimeout time.Duration, cleanupInterval time.Duration) *ExpiringStorage[K, V]

func (*ExpiringStorage[K, V]) Get added in v0.0.4

func (e *ExpiringStorage[K, V]) Get(key K) (value V)

func (*ExpiringStorage[K, V]) Keys added in v0.0.5

func (e *ExpiringStorage[K, V]) Keys() []K

func (*ExpiringStorage[K, V]) Len added in v0.0.4

func (e *ExpiringStorage[K, V]) Len() int

func (*ExpiringStorage[K, V]) Run added in v0.0.4

func (e *ExpiringStorage[K, V]) Run(ctx context.Context)

func (*ExpiringStorage[K, V]) Set added in v0.0.5

func (e *ExpiringStorage[K, V]) Set(key K, value V)

type Percent

type Percent float64
const (
	OnePercent Percent = 0.01
)

func PercentFrom

func PercentFrom[percentT int | int32 | int64 | float32 | float64](percent percentT) Percent

func (Percent) Float64

func (p Percent) Float64() float64

func (*Percent) MarshalJSON

func (p *Percent) MarshalJSON() ([]byte, error)

func (Percent) Scale

func (p Percent) Scale(val float64) float64

Scale returns p% imply that val is 100%

func (Percent) ScaleReversed

func (p Percent) ScaleReversed(val float64) float64

ScaleReversed returns 100% imply that val is p%

func (Percent) String

func (p Percent) String() string

func (*Percent) UnmarshalJSON

func (p *Percent) UnmarshalJSON(data []byte) error

type QueryDecoder added in v0.0.3

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

func NewQueryDecoder added in v0.0.3

func NewQueryDecoder(query url.Values) *QueryDecoder

func (*QueryDecoder) AddTypeParsing added in v0.0.3

func (qp *QueryDecoder) AddTypeParsing(typeSample any, parseFunc func(string) (any, error))

func (*QueryDecoder) DecodeQuery added in v0.0.3

func (qp *QueryDecoder) DecodeQuery(dst any) error

func (*QueryDecoder) ParseTags added in v0.0.3

func (qp *QueryDecoder) ParseTags(fieldVal reflect.Value, tags []string) error

func (*QueryDecoder) TagName added in v0.0.3

func (qp *QueryDecoder) TagName() string

type QueryEncoder added in v0.0.3

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

func NewQueryEncoder added in v0.0.3

func NewQueryEncoder(query url.Values) *QueryEncoder

func (*QueryEncoder) EncodeQuery added in v0.0.3

func (qe *QueryEncoder) EncodeQuery(src any) error

func (*QueryEncoder) ParseTags added in v0.0.3

func (qe *QueryEncoder) ParseTags(fieldVal reflect.Value, tags []string) error

func (*QueryEncoder) TagName added in v0.0.3

func (qe *QueryEncoder) TagName() string

type QueryMarshaller added in v0.0.3

type QueryMarshaller interface {
	MarshalQuery() (param string, err error)
}

type QueryUnmarshaler added in v0.0.3

type QueryUnmarshaler interface {
	UnmarshalQuery(param string) (err error)
}

type Request added in v0.0.4

type Request struct {
	Domain          string
	PageURL         string
	OS              string
	DeviceType      string
	BrowserLanguage string
	UA              string
	IP              string
}

func ParseGetRequest added in v0.0.4

func ParseGetRequest(httpReq *http.Request) (req *Request, err error)

type SQLStringValue

type SQLStringValue string

func ToSQLStringValue

func ToSQLStringValue(lines ...string) []SQLStringValue

type Set

type Set[data comparable] map[data]struct{}

func NewSet

func NewSet[T comparable]() Set[T]

func (Set[T]) Add

func (s Set[T]) Add(data ...T)

func (Set[T]) Delete

func (s Set[T]) Delete(data T)

func (Set[T]) Exists

func (s Set[T]) Exists(data T) bool

func (Set[T]) Values

func (s Set[T]) Values() []T

type TagsParser added in v0.0.3

type TagsParser interface {
	TagName() string
	ParseTags(fieldVal reflect.Value, tags []string) error
}

type UnixTime added in v0.0.3

type UnixTime time.Time

func Unix added in v0.0.3

func Unix(sec int64) UnixTime

func (*UnixTime) MarshalQuery added in v0.0.3

func (ut *UnixTime) MarshalQuery() (param string, err error)

func (*UnixTime) Time added in v0.0.3

func (ut *UnixTime) Time() time.Time

func (*UnixTime) UnmarshalQuery added in v0.0.3

func (ut *UnixTime) UnmarshalQuery(param string) (err error)

Jump to

Keyboard shortcuts

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