mango

package module
v0.3.5 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2025 License: MIT Imports: 18 Imported by: 0

README

mango

a smattering of helper utilities for golang

Documentation

Index

Constants

View Source
const (
	DurationDay  = 24 * time.Hour
	DurationYear = 365 * DurationDay
)

Variables

This section is empty.

Functions

func BaseFileName

func BaseFileName(path string) string

func Colorize added in v0.3.4

func Colorize(color ColorCode, s string) string

func ColorizeBlue added in v0.3.4

func ColorizeBlue(s string) string

func ColorizeGreen added in v0.3.4

func ColorizeGreen(s string) string

func ColorizePurple added in v0.3.4

func ColorizePurple(s string) string

func ColorizeRed added in v0.3.4

func ColorizeRed(s string) string

func ColorizeWhite added in v0.3.4

func ColorizeWhite(s string) string

func ColorizeYellow added in v0.3.4

func ColorizeYellow(s string) string

func CopyFile

func CopyFile(srcPath, dstPath string) error

func Fatal

func Fatal(err error, args ...any)

func FatalIf

func FatalIf(err error, v ...any)

func FileExists

func FileExists(path string) bool

func Filter

func Filter[F any](list []F, predicate func(F) bool) []F

func Find added in v0.2.12

func Find[F any](list []F, predicate func(F) bool) (F, bool)

func FormatSimpleDate added in v0.2.12

func FormatSimpleDate(ts *time.Time) string

func FormatTimeSince added in v0.2.12

func FormatTimeSince(ts *time.Time) string

func HashStrings

func HashStrings(vals ...string) (string, error)

HashStrings computes the hash of the given strings by hashing the strings together into an int representation and then converting that to a string to keep the resulting string small

func Init

func Init(level slog.Level, application string)

Init creates a slog logger instance with a custom LogHandler and sets it as the default logger

func Map

func Map[F any, G any](list []F, mapper func(F) G) []G

func Marshal

func Marshal(payload any) (io.Reader, error)

func MarshalToString

func MarshalToString(payload any) (string, error)

func Min added in v0.2.12

func Min(vals ...int) int

Min determines the minimum among the provided values

func ReadAllString

func ReadAllString(rdr io.Reader) (string, error)

func ShortID

func ShortID() string

func SliceEqual added in v0.2.12

func SliceEqual[F any](first, second []F, comparator Comparator[F]) bool

func StringSliceEqual added in v0.2.12

func StringSliceEqual(first, second []string) bool

func TokenFromFile

func TokenFromFile(path string) (*oauth2.Token, error)

TokenFromFile will attempt to parse an oauth token at the provided file path

func Unmarshal

func Unmarshal[T any](rdr io.Reader) (*T, error)

func UnmarshalFromString

func UnmarshalFromString[T any](str string) (*T, error)

func WrapError added in v0.3.1

func WrapError(err error) error

Types

type ColorCode added in v0.3.4

type ColorCode string
const (
	ColorCodeReset  ColorCode = "\033[0m"
	ColorCodeRed    ColorCode = "\033[38;2;214;48;49m"
	ColorCodeGreen  ColorCode = "\033[38;2;46;204;113m"
	ColorCodeYellow ColorCode = "\033[38;2;241;196;15m"
	ColorCodeBlue   ColorCode = "\033[38;2;9;132;227m"
	ColorCodePurple ColorCode = "\033[38;2;155;89;182m"
	ColorCodeWhite  ColorCode = "\033[37m"
)

type Comparator added in v0.2.12

type Comparator[F any] func(F, F) bool

type LogHandler added in v0.2.13

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

LogHandler is a custom slog handler that outputs to stdout

func (*LogHandler) Enabled added in v0.2.13

func (h *LogHandler) Enabled(ctx context.Context, level slog.Level) bool

Enabled only records the log if the record's log level is greater than or equal to the handlers log level

func (*LogHandler) Handle added in v0.2.13

func (h *LogHandler) Handle(ctx context.Context, record slog.Record) error

Handle outputs the log record to stdout. Since this logger is meant to be used for local applications with a small amount of output, it will not log the timestamps.

func (*LogHandler) WithAttrs added in v0.2.13

func (h *LogHandler) WithAttrs(attrs []slog.Attr) slog.Handler

func (*LogHandler) WithGroup added in v0.2.13

func (h *LogHandler) WithGroup(name string) slog.Handler

NOTE: groups are not currently supported

type RequestBuilder

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

func NewRequest

func NewRequest() *RequestBuilder

func (*RequestBuilder) Do

func (rb *RequestBuilder) Do(ctx context.Context) (*http.Response, error)

func (*RequestBuilder) Get

func (rb *RequestBuilder) Get() *RequestBuilder

func (*RequestBuilder) Post

func (rb *RequestBuilder) Post() *RequestBuilder

func (*RequestBuilder) SetClient

func (rb *RequestBuilder) SetClient(client *http.Client) *RequestBuilder

func (*RequestBuilder) SetMethod

func (rb *RequestBuilder) SetMethod(method string) *RequestBuilder

func (*RequestBuilder) SetParams

func (rb *RequestBuilder) SetParams(params map[string]string) *RequestBuilder

func (*RequestBuilder) SetPayload

func (rb *RequestBuilder) SetPayload(payload any) *RequestBuilder

func (*RequestBuilder) SetUrl

func (rb *RequestBuilder) SetUrl(url string) *RequestBuilder

type Set

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

func NewSet

func NewSet[K comparable](items ...K) Set[K]

func (Set[K]) Add

func (s Set[K]) Add(item K)

func (Set[K]) Delete

func (s Set[K]) Delete(item K) bool

func (Set[K]) Has

func (s Set[K]) Has(item K) bool

func (Set[K]) Size

func (s Set[K]) Size() int

type StackError added in v0.3.1

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

StackError wraps an error with the stack information where the error occurred. Note that since stack is saved during creation we need to create this at the exact location where the base error is thrown and not at a higher level.

func (*StackError) Error added in v0.3.1

func (s *StackError) Error() string

func (*StackError) Unwrap added in v0.3.1

func (s *StackError) Unwrap() error

Jump to

Keyboard shortcuts

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