tool

package module
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2024 License: MIT Imports: 14 Imported by: 3

README

A set of tools I use in a number projects.

Documentation

Overview

Package tool Useful general purpose tool

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Catch added in v1.0.2

func Catch(fn func(err error))

Catch Recovers from panic and callbacks with error If error is not catchableError, it will panic again May be used as defer, coupled with MustReturn or Must, to override named return values

Usage:

  func example() (val *http.Request, err error) {
	defer tool.Catch(func(caught error) {
		err = caught
 	})

	val = tool.MustReturn(funcThatReturnsValAndErr()) // <- this will be caught if err!=nil
	panic(errors.New("some error")) // <- this will not be caught
	return
}

func Console

func Console(obj ...interface{})

Console Prints %+v of arguments, great to debug stuff

func ConvertSlice added in v1.2.0

func ConvertSlice[T any, Y any](srcSlice []T, destTypedValue Y) []Y

ConvertSlice Return a new slice as `[]dstTypedValue.(type)` cast from the `srcSlice`

func Err added in v1.0.2

func Err(args ...any) error

Err Returns the last argument if it is an error, otherwise nil

func ExecTemplate added in v1.0.8

func ExecTemplate(templateText string, templateVars any) string

func In

func In[T comparable](needle T, haystack ...T) bool

In Checks if element is in a slice

func IsZero added in v1.3.0

func IsZero[T comparable](v T) bool

IsZero Checks if value is zero

func MultiMute added in v1.2.0

func MultiMute[T any](a ...T) []T

MultiMute Ignores errors, returns slice of results.

func Must

func Must(err error, verbose ...bool)

Must Tolerates no errors.

func MustReturn added in v1.0.2

func MustReturn[T any](val T, err error) T

MustReturn Tolerates no errors, returns value.

func NonZero added in v1.0.7

func NonZero[T comparable](ts ...T) T

NonZero Returns first non-zero value or zero value if all values are zero

func Objectify

func Objectify[T ~[]byte | ~string](in T, target any) bool

Objectify Unmarshalls value to the target pointer value

func Ptr

func Ptr[T any](n T) *T

Ptr Return a pointer for any passed object

func RandInt

func RandInt[num constraints.Signed](min, max num) num

RandInt Return a random number in specified range.

func Recoverer

func Recoverer[num constraints.Integer](maxPanics num, f func(), jobID ...string) (recovErr error)

Recoverer Recovers job from panic, if maxPanics<0 then infinitely

func RetryFunc

func RetryFunc[num constraints.Signed](attempts num, sleep time.Duration, f func() error) error

RetryFunc Re-runs function if error returned

func Return added in v1.0.2

func Return[T any](val T, _ error) T

Return Ignores errors, returns value.

func SetLogger

func SetLogger(l StdLogger)

SetLogger Sets tool package logger, pass nil to disable logging

func Strtr

func Strtr(subject string, oldToNew map[string]string) string

Strtr Replaces all old string occurrences with new string in subject

func Try

func Try(err error, verbose ...bool) bool

Try Probes the error and returns bool, optionally logs the message.

Types

type LogRus

type LogRus interface {
	StdLogger
	WithError(error) LogRus
	Errorln(...any)
}

type StdLogger

type StdLogger interface {
	Println(...any)
	Panicln(...any)
	Printf(string, ...any)
	Print(...any)
}

type Varchar

type Varchar string

func Jsonify

func Jsonify(s any) Varchar

Jsonify Returns Varchar implementation of the serialized value, returns empty on error

func (Varchar) Bytes

func (s Varchar) Bytes() []byte

Bytes Return Varchar as Bytes slice

func (*Varchar) MarshalJSON

func (s *Varchar) MarshalJSON() ([]byte, error)

func (Varchar) String

func (s Varchar) String() string

String Return Varchar as string

Jump to

Keyboard shortcuts

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