ptk

package module
v0.0.0-...-c6fdbb4 Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2019 License: MIT Imports: 22 Imported by: 3

README

ptk

PathDNA Tool Kit (random helper functions and utils)

Documentation

Index

Constants

View Source
const DefaultBcryptRounds = 12

Variables

This section is empty.

Functions

func Base64ToJSON

func Base64ToJSON(enc *base64.Encoding, mimeType string, r io.Reader) func(w io.Writer) error

Base64ToJSON allows converting a reader into a base64 encoded string and streaming it directly without using buffers. if enc is nil, it uses base64.StdEncoding by default. Example: WriteJSONObject(w, M{"image": Base64ToJSON(nil, "image/png", r)})

func CheckPassword

func CheckPassword(hash string, password string) bool

func CreateMAC

func CreateMAC(password, token, salt string) string

func DecodeBase64

func DecodeBase64(s string) []byte

func ErrorWithLine

func ErrorWithLine(err error) error

func HashPassword

func HashPassword(password string, cost int) (string, error)

func MarshalJSON

func MarshalJSON(indent bool, v interface{}) (string, error)

func Pipe

func Pipe(rfn func(io.Reader) error, wfn func(io.Writer) error) error

Pipe is an io.Pipe helper

func PipeJSONObject

func PipeJSONObject(obj M) io.Reader

PipeJSONObject uses an io.Pipe to stream an M rather than keeping it in memory. if the type of value is io.Reader, it will be directly streamed (so it must be valid JSON). if the type is func(io.Writer|*bufio.Writer) error, it will pass the underlying writer. otherwise json.Marshal will be called on the value.

func RandomSafeString

func RandomSafeString(ln int) string

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 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 RuntimeLine

func RuntimeLine(callerIdx int) string

func SleepUntil

func SleepUntil(ctx context.Context, hour, min, sec int) error

func UniqueSlice

func UniqueSlice(in []string) (out []string)

UniqueSlice overwrites in

func VerifyMac

func VerifyMac(mac1, password, token, salt string) bool

func WaitFor

func WaitFor(fn func() error) <-chan error

func WriteJSONObject

func WriteJSONObject(w io.Writer, obj M) (err error)

Types

type Error

type Error struct {
	Msg string
	Err error
}

func (Error) Error

func (e Error) Error() string

type Errors

type Errors []error

func (Errors) Err

func (es Errors) Err() error

func (Errors) Error

func (es Errors) Error() string

func (*Errors) Push

func (es *Errors) Push(err error) (pushed bool)

func (*Errors) PushWithLine

func (es *Errors) PushWithLine(err error) (pushed bool)

func (*Errors) Wrap

func (es *Errors) Wrap(msg string, err error) (pushed bool)

func (*Errors) WrapWithLine

func (es *Errors) WrapWithLine(msg string, err error) (pushed bool)

type HTTPClient

type HTTPClient struct {
	http.Client
	DefaultHeaders http.Header
	DefaultQuery   url.Values
}
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, ct, uri string, reqData, respData interface{}) 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.
  • ct: 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.

type M

type M map[string]interface{}

M is a QoL shortcut for map[string]interface{}

func (M) ToJSON

func (m M) ToJSON(indent bool) string

ToJSON returns a string json representation of M, mostly for debugging.

type MultiSet

type MultiSet map[string]Set

MultiSet is a very simple Set

func NewMultiSet

func NewMultiSet(keys ...string) MultiSet

func (MultiSet) Delete

func (ms MultiSet) Delete(key string, subKeys ...string)

func (MultiSet) Has

func (ms MultiSet) Has(key, subKey string) bool

func (MultiSet) Keys

func (ms MultiSet) Keys() []string

func (MultiSet) Set

func (ms MultiSet) Set(key string, subKeys ...string)

func (MultiSet) Sub

func (ms MultiSet) Sub(key string) Set

func (MultiSet) Values

func (ms MultiSet) Values(key string) []string

type SafeMultiSet

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

SafeMultiSet is a concurrent-safe MultiSet

func NewSafeMultiSet

func NewSafeMultiSet(keys ...string) *SafeMultiSet

func (*SafeMultiSet) Delete

func (sms *SafeMultiSet) Delete(key string, subKeys ...string)

func (*SafeMultiSet) Has

func (sms *SafeMultiSet) Has(key, subKey string) (ok bool)

func (*SafeMultiSet) Keys

func (sms *SafeMultiSet) Keys() (keys []string)

func (*SafeMultiSet) Set

func (sms *SafeMultiSet) Set(key string, subKeys ...string)

func (*SafeMultiSet) Sub

func (sms *SafeMultiSet) Sub(key string) (ss *SafeSet)

func (*SafeMultiSet) Values

func (sms *SafeMultiSet) Values(key string) (vals []string)

type SafeSet

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

func NewSafeSet

func NewSafeSet(keys ...string) *SafeSet

func (*SafeSet) Delete

func (ss *SafeSet) Delete(keys ...string)

func (*SafeSet) Has

func (ss *SafeSet) Has(key string) bool

func (*SafeSet) Keys

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

func (*SafeSet) Len

func (ss *SafeSet) Len() int

func (*SafeSet) MarshalJSON

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

func (*SafeSet) Set

func (ss *SafeSet) Set(keys ...string)

func (*SafeSet) UnmarshalJSON

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

type Sem

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

func NewSem

func NewSem(size int) *Sem

func (*Sem) Add

func (s *Sem) Add(n int)

func (*Sem) Close

func (s *Sem) Close()

func (*Sem) Done

func (s *Sem) Done()

func (*Sem) Run

func (s *Sem) Run(fn func())

func (*Sem) Wait

func (s *Sem) Wait()

type Set

type Set map[string]struct{}

Set is a simple set.

func NewSet

func NewSet(keys ...string) Set

func (Set) Delete

func (s Set) Delete(keys ...string)

func (Set) Has

func (s Set) Has(key string) bool

func (Set) Keys

func (s Set) Keys() []string

func (Set) Len

func (s Set) Len() int

func (Set) MarshalJSON

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

func (*Set) Set

func (s *Set) Set(keys ...string)

func (*Set) UnmarshalJSON

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

type TimeoutSet

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

func NewTimeoutSet

func NewTimeoutSet(purgeTimeout time.Duration) *TimeoutSet

func (*TimeoutSet) Close

func (ss *TimeoutSet) Close() error

func (*TimeoutSet) Delete

func (ss *TimeoutSet) Delete(keys ...string)

func (*TimeoutSet) Has

func (ss *TimeoutSet) Has(key string) bool

func (*TimeoutSet) IsClosed

func (ss *TimeoutSet) IsClosed() bool

func (*TimeoutSet) Keys

func (ss *TimeoutSet) Keys() []string

func (*TimeoutSet) Len

func (ss *TimeoutSet) Len() int

func (*TimeoutSet) Set

func (ss *TimeoutSet) Set(key string, to time.Duration)

func (*TimeoutSet) SetAny

func (ss *TimeoutSet) SetAny(keys ...string)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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