utils

package
v1.7.1 Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidUseOfMultiType = errors.New("invalid use of MultiType")
View Source
var NilType = GetType[Nil]()

NilType represent the type of Nil.

Functions

func Clone

func Clone[K comparable, V any, M ~map[K]V](m M) M

func ConcatSlices

func ConcatSlices[T any](slices ...[]T) []T

func ExtractMultiTypeTypes added in v1.2.0

func ExtractMultiTypeTypes(mtType reflect.Type) ([]reflect.Type, error)

func Filter

func Filter[T any](elements []T, filterFn func(T) bool) []T

func Find

func Find[T any](elements []T, findFn func(T) bool) (T, bool)

func FromEntries

func FromEntries[K comparable, V any](entries []Entry[K, V]) map[K]V

func GetType added in v1.2.0

func GetType[T any]() reflect.Type

GetType returns reflect.Type of the generic parameter it receives.

func IndexOf

func IndexOf[T any](elements []T, findFn func(T) bool) int

func IsMultiType added in v1.2.0

func IsMultiType(t reflect.Type) bool

func Keys

func Keys[K comparable, V any, M ~map[K]V](m M) []K

func LastIndexOf

func LastIndexOf[T any](elements []T, findFn func(T) bool) int

func Map

func Map[T, R any](elements []T, mapFn func(T) R) []R

func Ptr added in v1.2.0

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

func StructKeys added in v1.2.0

func StructKeys(structType reflect.Type, tag string) map[string]reflect.StructField

StructKeys returns a map of "key" -> "field" for all the fields in the struct. Key is the field tag if exists or the field name otherwise. Field is the reflect.StructField of the field.

Unexported fields or fields with tag value of "-" are ignored.

StructKeys will recursively traverse all the embedded structs and return their fields as well.

func Values

func Values[K comparable, V any, M ~map[K]V](m M) []V

Types

type Entry

type Entry[K comparable, V any] struct {
	Key   K
	Value V
}

func Entries

func Entries[K comparable, V any, M ~map[K]V](m M) []Entry[K, V]

type InMemoryLogger

type InMemoryLogger interface {
	Logger
	Printed() string
}

func NewInMemoryLogger

func NewInMemoryLogger() InMemoryLogger

func NewInMemoryLoggerWithLevel

func NewInMemoryLoggerWithLevel(level LogLevel) InMemoryLogger

type LogCounters

type LogCounters struct {
	Errors   int
	Warnings int
}

type LogLevel

type LogLevel int
const (
	Error LogLevel = iota
	Warn
	Info
	Off
)

func (LogLevel) MarshalText

func (l LogLevel) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler.

func (*LogLevel) UnmarshalText

func (l *LogLevel) UnmarshalText(data []byte) error

UnmarshalText implements encoding.TextUnmarshaler.

type Logger

type Logger interface {
	Log(LogLevel, any)
	Logf(LogLevel, string, ...any)
	LogIfNotNil(LogLevel, any) bool
	LogIfNotNilf(LogLevel, any, string, ...any) bool
	Info(any)
	Infof(string, ...any)
	Warn(any)
	Warnf(string, ...any)
	Error(any)
	Errorf(string, ...any)
	ErrorIfNotNil(any) bool
	ErrorIfNotNilf(any, string, ...any) bool
	WarnIfNotNil(any) bool
	WarnIfNotNilf(any, string, ...any) bool
	Counters() LogCounters
	Warnings() int
	Errors() int
	MustHaveNoWarnings() error
	MustHaveNoErrors() error
	MustHaveNoWarningsf(string, ...any) error
	MustHaveNoErrorsf(string, ...any) error
	AppendCounters(LogCounters)
	// NewCounter creates a cloned logger with the same output and log level but with new counters
	NewCounter() Logger
	WithLevel(level LogLevel)
}

Logger act as a regular logger that counts logged errors and warnings.

func NewLoggerWithLevel

func NewLoggerWithLevel(out io.Writer, level LogLevel) Logger

type MultiType added in v1.2.0

type MultiType[T any] struct {
	Values T
}

func (*MultiType[T]) MarshalJSON added in v1.2.0

func (o *MultiType[T]) MarshalJSON() ([]byte, error)

func (*MultiType[T]) MultiTypeTypes added in v1.2.0

func (o *MultiType[T]) MultiTypeTypes() ([]reflect.Type, error)

func (*MultiType[T]) UnmarshalJSON added in v1.2.0

func (o *MultiType[T]) UnmarshalJSON(bytes []byte) error

type Nil added in v1.2.0

type Nil *uintptr

Nil represents an empty type. You can use it with the HandlerFunc generic parameters to declare no Request with no request body, no path or query params, or responses with no response body.

type Set

type Set[T comparable] map[T]bool

Set keeps a collection of unique items with efficient access for addition, removal and check existence of elements.

func NewSet

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

NewSet creates a new Set and populate it with the provided elements. duplicate elements will be saved once

func (Set[T]) Add

func (s Set[T]) Add(value T) bool

Add an element to the set. returns true if the element is a new element in the set and false if the element was already part of the set.

func (Set[T]) Clone

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

Clone creates a new copy of the set

func (Set[T]) Has

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

Has checks if an element exist in the set. returns true if the element exist and false otherwise.

func (Set[T]) Remove

func (s Set[T]) Remove(value T) bool

Remove an element from the set. returns true if removed and false if the element was not on the set to begin with.

Jump to

Keyboard shortcuts

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