otk

package module
v2.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2022 License: Apache-2.0 Imports: 38 Imported by: 0

README

otk

OneOfOne's Tool Kit (random helper functions and utils).

A clone of an old repo that no longer exists and will slowly be moved here.

License: Apache 2.0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	True    = BoolPtr(true)
	False   = BoolPtr(false)
	Nilbool *bool
)

Tribools

View Source
var (
	ErrClosedSem      = errors.New("sem is already closed")
	ErrNegativeNumber = errors.New("n < 1")
	ErrSemIsFull      = errors.New("sem is full")
)
View Source
var (
	ErrClosedPool = errors.New("the pool is closed, go swim somewhere else")
	ErrPoolIsFull = errors.New("the pool is full")
)
View Source
var ErrInvalidImageDataURL = xerrors.New("invalid dataURL, must be in format `data:image/{png,jpeg,jpg,gif,webp};base64,`")
View Source
var (
	ErrOffsetOOB = xerrors.New("offset out of bounds")
)
View Source
var ErrStopTask = errors.New("STOP")

ErrStopTask can be returned to stop the run loop

Functions

func AESDecrypt

func AESDecrypt(b64Data, sep, passphrase string, keySize uint8) (parts []string, err error)

func AESEncrypt

func AESEncrypt(parts []string, sep string, passphrase string, keySize uint8) (_ string, err error)

AESEncrypt joins parts using sep, encrypts and seals it and returns b64 string. valid sizes are 16, 24 and 32 for AES-128, 192 and 256 respectively, defaults to 16.

func AppendToTar

func AppendToTar(tw *tar.Writer, fullPath, tarPath string) (err error)

AppendToTar is a helper function for add a physical file to tar

func BoolPtr

func BoolPtr(b bool) *bool

func BoolVal

func BoolVal(b *bool) bool

func Caller

func Caller(skip int, trim bool) (function, file string, line int)

Caller returns function, file and line of the caller.

func CopyOnWriteFile

func CopyOnWriteFile(fp string, fn func(w io.Writer) error) (err error)

func CopyOnWriteFilePerms

func CopyOnWriteFilePerms(fp string, fn func(bw *bufio.Writer) error, mode os.FileMode) (err error)

func DecodeImageConfig

func DecodeImageConfig(rd io.Reader) (safeReader io.Reader, cfg image.Config, format string, err error)

DecodeImageConfig accepts a reader and returns a CachedReader and the results of image.DecodeConfig

func ImageFromDataURL

func ImageFromDataURL(dataURL string, fn func(ext string) (io.WriteCloser, error)) error

func Lesser

func Lesser[T comparable]() func(a, b T) bool

func MergeErrors

func MergeErrors(sep string, errs ...error) error

MergeErrors merges a slice of errors, but they will lose any context. returns nil if all errors are nil

func ModNextDay

func ModNextDay(start, now time.Time, days int) time.Duration

ModNextDay returns the next duration until X days since start. for example: - ModNextDay(2020-06-13, 2020-07-14, 15) => 14 days (2020-07-28) - ModNextDay(2020-06-29, 2020-07-14, 15) => 0 - ModNextDay(2020-07-14, 2020-07-14, 15) => 0 - ModNextDay(2020-07-15, 2020-07-14, 15) => 1 day (2020-07-15)

func MultiWriter

func MultiWriter(writers ...io.Writer) (io.Writer, func() []error)

MultiWriter creates a writer that duplicates its writes to all the provided writers, similar to the Unix tee(1) command.

Each write is written to each listed writer, one at a time. If a listed writer returns an error, that writer will be ignored for later writes. returns the multi writer and a func that returns a slice of errors if there were any.

func NopWriteCloser

func NopWriteCloser(w io.Writer) io.WriteCloser

func PipeRd

func PipeRd(writeFn func(io.Writer) error) io.ReadCloser

PipeRd is an io.Pipe helper, returns a io.ReadCloser

func PipeWr

func PipeWr(readerFn func(io.Reader) error) io.WriteCloser

PipeWr is an io.Pipe helper, returns a io.WriteCloser

func RandomString

func RandomString(sz int) (string, error)

func ReadFileWithDecoder

func ReadFileWithDecoder(fp string, dec func(r io.Reader) FileDecoder, out interface{}) error

func ReadJSONFile

func ReadJSONFile(fp string, out interface{}) error

func ReplaceAllStringSubmatchFunc

func ReplaceAllStringSubmatchFunc(re *regexp.Regexp, src string, repl func([]string) []string, n int) string

ReplaceAllStringSubmatchFunc is a helper function to replace regexp sub matches. based on https://gist.github.com/slimsag/14c66b88633bd52b7fa710349e4c6749 (MIT) Note: slice `in` is reused internally, make a copy if you need to keep it, ex:

cp := append([]string(nil), in...)

example:

re := regexp.MustCompile(`([:*].*?)(?:/|$)`)
ReplaceAllStringSubmatchFunc(re, "/path/:id/:name", func(in []string) []string {
	for i, s := range in {
		in[i] = "my-" + s[1:]
	}
	return in
}, -1) === "/path/my-id/my-name"

func Request

func Request(method, ct, url string, reqData, respData interface{}) error

func RequestCtx

func RequestCtx(ctx context.Context, method, ct, url string, reqData, respData interface{}) error

func RequestHeadersCtx

func RequestHeadersCtx(ctx context.Context, method, url string, header http.Header, reqData, respData interface{}) error

func Retry

func Retry(fn func() error, attempts uint, delay time.Duration, backoffMod float64) error

Retry is an alias for RetryCtx(context.Background(), fn, attempts, delay, backoffMod)

func RetryCtx

func RetryCtx(ctx context.Context, fn func() error, attempts uint, delay time.Duration, backoffMod float64) error

RetryCtx calls fn every (delay * backoffMod) until it returns nil, the passed ctx is done or attempts are reached.

func SaveImageFromDataURL

func SaveImageFromDataURL(dataURL, output string) (fp string, err error)

SaveImageFromDataURL expects output name without the extension returns full path

func TarFolder

func TarFolder(folder, fp string, topts *TarOptions) (err error)

func TimeUntil

func TimeUntil(t time.Time, hour, min, sec int, nextDay bool) time.Duration

TimeUntil is a tiny helper to return the duration until hour:min:sec if the duration is in the past and nextDay is true, it'll add 24 hours.

func TryParseTime

func TryParseTime(ts string, layouts []string, tz *time.Location) time.Time

TryParseTime will try to parse input in all provided go time formats. Accepts: U for unix ts, UN for unix nano.

func UniqueSlice

func UniqueSlice[S ~[]E, E comparable](in S, inplace bool) (out S)

UniqueSlice returns all unique keys in `in` by modifying it in place

func UnsafeBytes

func UnsafeBytes(s string) (out []byte)

func UnsafeString

func UnsafeString(p []byte) string

UnsafeString can spawn dragons out of bytes

func Unzip

func Unzip(rt io.ReaderAt, dst string, filter func(path string, f *zip.File) bool) (err error)

func ValidEmail

func ValidEmail(email string) bool

ValidEmail checks the given email for validity

func WriteFileWithEncoder

func WriteFileWithEncoder(fp string, enc func(w io.Writer) FileEncoder, in interface{}) error

func WriteJSONFile

func WriteJSONFile(fp string, in interface{}, indent bool) error

Types

type Buffer

type Buffer struct {
	bytes.Buffer
	// contains filtered or unexported fields
}

Buffer is a drop-in replacement for bytes.Buffer that implements the io.ReaderAt interface.

func NewBuffer

func NewBuffer(buf []byte) *Buffer

func NewBufferString

func NewBufferString(s string) *Buffer

func (*Buffer) ReadAt

func (b *Buffer) ReadAt(p []byte, off int64) (n int, err error)

func (*Buffer) ReadFrom

func (b *Buffer) ReadFrom(r io.Reader) (n int64, err error)

func (*Buffer) UnsafeString

func (b *Buffer) UnsafeString() string

UnsafeString returns the accumulated string, unsafely without a copy.

func (*Buffer) Write

func (b *Buffer) Write(p []byte) (n int, err error)

func (*Buffer) WriteByte

func (b *Buffer) WriteByte(c byte) (err error)

func (*Buffer) WriteRune

func (b *Buffer) WriteRune(r rune) (n int, err error)

func (*Buffer) WriteString

func (b *Buffer) WriteString(s string) (n int, err error)

type BufferPool

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

func (*BufferPool) Get

func (bp *BufferPool) Get() *Buffer

func (*BufferPool) Put

func (bp *BufferPool) Put(b *Buffer)

type CachedReader

type CachedReader struct {
	R io.Reader
	// contains filtered or unexported fields
}

func (*CachedReader) Read

func (cr *CachedReader) Read(p []byte) (int, error)

func (*CachedReader) Rewind

func (cr *CachedReader) Rewind()

type ErrorList

type ErrorList []error

func (ErrorList) Err

func (el ErrorList) Err() error

func (ErrorList) Error

func (el ErrorList) Error() string

func (ErrorList) Len

func (el ErrorList) Len() int

func (*ErrorList) Push

func (el *ErrorList) Push(errs ...error)

type FileDecoder

type FileDecoder interface {
	Decode(interface{}) error
}

type FileEncoder

type FileEncoder interface {
	Encode(interface{}) error
}

type HTTPClient

type HTTPClient struct {
	DefaultHeaders http.Header
	DefaultQuery   url.Values
	http.Client
}
var DefaultClient HTTPClient

func (*HTTPClient) AllowInsecureTLS

func (c *HTTPClient) AllowInsecureTLS(v bool) (old bool)

func (*HTTPClient) Do

func (c *HTTPClient) Do(req *http.Request) (*http.Response, error)

func (*HTTPClient) Request

func (c *HTTPClient) Request(method, ct, url string, reqData, respData interface{}) error

Request is a wrapper for `RequestCtx(context.Background(), method, ct, url, reqData, respData)`

func (*HTTPClient) RequestCtx

func (c *HTTPClient) RequestCtx(ctx context.Context, method, contentType, uri string, reqData, respData interface{}) (err error)

RequestCtx is a smart wrapper to handle http requests.

  • ctx: is a context.Context in case you want more control over canceling the request.
  • method: http method (GET, PUT, POST, etc..), if empty it defaults to GET.
  • contentType: request content-type.
  • url: the request's url.
  • reqData: data to pass to POST/PUT requests, if it's an `io.Reader`, a `[]byte` or a `string`, it will be passed as-is, `url.Values` will be encoded as "application/x-www-form-urlencoded", any other object will be encoded as JSON.
  • respData: data object to get the response or `nil`, can be , `io.Writer`, `func(io.Reader) error` to read the body directly, `func(*http.Response) error` to process the actual response, or a pointer to an object to decode a JSON body into.

func (*HTTPClient) RequestHeadersCtx

func (c *HTTPClient) RequestHeadersCtx(ctx context.Context, method, uri string, header http.Header, reqData, respData interface{}) (err error)

RequestHeaderCtx is a smart wrapper to handle http requests.

  • ctx: is a context.Context in case you want more control over canceling the request.
  • method: http method (GET, PUT, POST, etc..), if empty it defaults to GET.
  • url: the request's url.
  • header: headers to pass to the request, ex User-Agent.
  • reqData: data to pass to POST/PUT requests, if it's an `io.Reader`, a `[]byte` or a `string`, it will be passed as-is, `url.Values` will be encoded as "application/x-www-form-urlencoded", any other object will be encoded as JSON.
  • respData: data object to get the response or `nil`, can be , `io.Writer`, `func(io.Reader) error` to read the body directly, `func(*http.Response) error` to process the actual response, or a pointer to an object to decode a JSON body into.

type Int64SafeSet added in v2.0.1

type Int64SafeSet = SafeSet[int64]

type Int64Set added in v2.0.1

type Int64Set = Set[int64]

type IntSafeSet added in v2.0.1

type IntSafeSet = SafeSet[int]

type IntSet added in v2.0.1

type IntSet = Set[int]

type M

type M = map[string]interface{}

func MergeMap

func MergeMap(dst, src M) M

type MultiSet

type MultiSet[T1, T2 comparable] map[T1]Set[T2]

func (MultiSet[T1, T2]) Add

func (s MultiSet[T1, T2]) Add(key T1, values ...T2) MultiSet[T1, T2]

func (MultiSet[T1, T2]) AddIfNotExists

func (s MultiSet[T1, T2]) AddIfNotExists(key T1, value T2) bool

AddIfNotExists returns true if the key was added, false if it already existed

func (MultiSet[T1, T2]) Clone

func (s MultiSet[T1, T2]) Clone() MultiSet[T1, T2]

func (MultiSet[T1, T2]) Delete

func (s MultiSet[T1, T2]) Delete(keys ...T1) MultiSet[T1, T2]

func (MultiSet[T1, T2]) DeleteValues

func (s MultiSet[T1, T2]) DeleteValues(key T1, values ...T2) MultiSet[T1, T2]

func (MultiSet[T1, T2]) Equal

func (s MultiSet[T1, T2]) Equal(os MultiSet[T1, T2]) bool

func (MultiSet[T1, T2]) Has

func (s MultiSet[T1, T2]) Has(key T1, sub T2) bool

func (MultiSet[T1, T2]) Keys

func (s MultiSet[T1, T2]) Keys() []T1

func (MultiSet[T1, T2]) Len

func (s MultiSet[T1, T2]) Len() int

func (MultiSet[T1, T2]) Match

func (s MultiSet[T1, T2]) Match(fn func(key T1, s Set[T2]) bool, all bool) bool

func (MultiSet[T1, T2]) Merge

func (s MultiSet[T1, T2]) Merge(o MultiSet[T1, T2]) MultiSet[T1, T2]

func (MultiSet[T1, T2]) MergeSet

func (s MultiSet[T1, T2]) MergeSet(key T1, o Set[T2]) MultiSet[T1, T2]

func (*MultiSet[T1, T2]) Set

func (s *MultiSet[T1, T2]) Set(key T1, values ...T2) MultiSet[T1, T2]

func (MultiSet[T1, T2]) SortedKeys

func (s MultiSet[T1, T2]) SortedKeys() []T1

func (MultiSet[T1, T2]) String

func (s MultiSet[T1, T2]) String() string

func (MultiSet[T1, T2]) Values

func (s MultiSet[T1, T2]) Values(key T1) Set[T2]

type SafeErrorList

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

func (*SafeErrorList) Err

func (el *SafeErrorList) Err() error

func (*SafeErrorList) Error

func (el *SafeErrorList) Error() string

func (*SafeErrorList) Len

func (el *SafeErrorList) Len() int

func (*SafeErrorList) Push

func (el *SafeErrorList) Push(errs ...error)

type SafeSet

type SafeSet[T comparable] struct {
	// contains filtered or unexported fields
}

func SafeSetOf

func SafeSetOf[T comparable](keys ...T) *SafeSet[T]

func (*SafeSet[T]) Add

func (ss *SafeSet[T]) Add(keys ...T) *SafeSet[T]

func (*SafeSet[T]) AddIfNotExists

func (ss *SafeSet[T]) AddIfNotExists(key T) bool

func (*SafeSet[T]) Clone

func (ss *SafeSet[T]) Clone() *SafeSet[T]

func (*SafeSet[T]) Delete

func (ss *SafeSet[T]) Delete(keys ...T) *SafeSet[T]

func (*SafeSet[T]) Has

func (ss *SafeSet[T]) Has(key T) bool

func (*SafeSet[T]) Keys

func (ss *SafeSet[T]) Keys() []T

func (*SafeSet[T]) Len

func (ss *SafeSet[T]) Len() int

func (*SafeSet[T]) MarshalJSON

func (ss *SafeSet[T]) MarshalJSON() ([]byte, error)

func (*SafeSet[T]) Merge

func (ss *SafeSet[T]) Merge(o Set[T]) *SafeSet[T]

func (*SafeSet[T]) MergeSafe

func (ss *SafeSet[T]) MergeSafe(o *SafeSet[T]) *SafeSet[T]

func (*SafeSet[T]) Set

func (ss *SafeSet[T]) Set(keys ...T) *SafeSet[T]

func (*SafeSet[T]) SortedKeys

func (ss *SafeSet[T]) SortedKeys() []T

func (*SafeSet[T]) UnmarshalJSON

func (ss *SafeSet[T]) UnmarshalJSON(data []byte) error

type Scheduler

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

func NewScheduler

func NewScheduler(pctx context.Context) *Scheduler

func (*Scheduler) Start

func (c *Scheduler) Start(id string, fn TaskFunc, startIn, thenEvery time.Duration) error

func (*Scheduler) Stop

func (c *Scheduler) Stop(id string) error

func (*Scheduler) StopAll

func (c *Scheduler) StopAll()

type Sem

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

func NewSem

func NewSem(size int) *Sem

func (*Sem) Acquire

func (s *Sem) Acquire(n int) error

func (*Sem) Close

func (s *Sem) Close() error

func (*Sem) Go

func (s *Sem) Go(fn func()) error

func (*Sem) Release

func (s *Sem) Release(n int) error

type Set

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

Set is a simple set.

func SetOf

func SetOf[T comparable](keys ...T) Set[T]

func (Set[T]) Add

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

func (*Set[T]) AddIfNotExists

func (s *Set[T]) AddIfNotExists(key T) bool

AddIfNotExists returns true if the key was added, false if it already existed

func (Set[T]) Clone

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

func (Set[T]) Delete

func (s Set[T]) Delete(keys ...T) Set[T]

func (Set[T]) Equal

func (s Set[T]) Equal(os Set[T]) bool

func (Set[T]) Has

func (s Set[T]) Has(key T) bool

func (Set[T]) Keys

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

func (Set[T]) Len

func (s Set[T]) Len() int

func (Set[T]) MarshalJSON

func (s Set[T]) MarshalJSON() ([]byte, error)

func (Set[T]) Merge

func (s Set[T]) Merge(os ...Set[T]) Set[T]

func (*Set[T]) Set

func (s *Set[T]) Set(keys ...T) Set[T]

func (Set[T]) SortedKeys

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

func (Set[T]) String

func (s Set[T]) String() string

func (*Set[T]) UnmarshalJSON

func (s *Set[T]) UnmarshalJSON(data []byte) (err error)

type StringMultiSet added in v2.0.1

type StringMultiSet = MultiSet[string, string]

type StringSafeSet added in v2.0.1

type StringSafeSet = SafeSet[string]

type StringSet added in v2.0.1

type StringSet = Set[string]

type TarOptions

type TarOptions struct {
	CompressFn    func(w io.Writer) io.WriteCloser
	FilterFn      func(path string, fi os.FileInfo) bool
	BufSize       int
	DeleteOnError bool
}

type TaskFunc

type TaskFunc = func(ctx context.Context, t time.Time) error

type Uint64SafeSet added in v2.0.1

type Uint64SafeSet = SafeSet[uint64]

type Uint64Set added in v2.0.1

type Uint64Set = Set[uint64]

type UintSafeSet added in v2.0.1

type UintSafeSet = SafeSet[uint]

type UintSet added in v2.0.1

type UintSet = Set[uint]

type Workers

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

func NewWorkers

func NewWorkers(ctx context.Context, initial, increaseBy int) *Workers

func (*Workers) Close

func (w *Workers) Close() error

func (*Workers) Exec

func (w *Workers) Exec(fn func(context.Context)) (_ error)

type XorShiftRNG

type XorShiftRNG uint64

XorShiftRNG simple efficient thread-safe lock-free rng, self seeds with time.Now().UnixNano() if 0

func (*XorShiftRNG) Bytes

func (x *XorShiftRNG) Bytes() [8]byte

func (*XorShiftRNG) Int63

func (x *XorShiftRNG) Int63() int64

func (*XorShiftRNG) Seed

func (x *XorShiftRNG) Seed(n int64)

func (*XorShiftRNG) Uint64

func (x *XorShiftRNG) Uint64() (rv uint64)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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