common

package
v0.4.6 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2024 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package errors provides a custom error type for out-of-bound errors.

Package errors provides a custom error type for out-of-bound errors.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Assert added in v0.3.35

func Assert(cond bool, msg string)

Assert panics if the condition is false.

Parameters:

  • cond: The condition to check.
  • msg: The message to show if the condition is false.

The panic message is the string msg.

func AssertDerefNil added in v0.4.4

func AssertDerefNil[T any](elem *T, param_name string) T

AssertNil panics if the element is nil but returns the element dereferenced if it is not nil.

Parameters:

  • elem: The element to check.
  • param_name: The name of the parameter.

Returns:

  • T: The element if it is not nil.

The panic message is the message "Parameter \"param_name\" must not be nil".

func AssertErr added in v0.3.38

func AssertErr(err error, format string, args ...any)

AssertErr panics if the error is not nil.

Parameters:

  • err: The error to check.
  • format: The format of the message to show if the error is not nil.
  • args: The arguments to format the message.

The format should be the function name and the args should be the parameters.

Example:

func MyFunc(param1 string, param2 int) {
    res, err := SomeFunc(param1, param2)
    AssertErr(err, "SomeFunc(%s, %d)", param1, param2) // panic("In SomeFunc(param1, param2) = err")
}

func AssertF added in v0.3.35

func AssertF(cond bool, format string, args ...any)

AssertF panics if the condition is false.

Parameters:

  • cond: The condition to check.
  • format: The format of the message to show if the condition is false.
  • args: The arguments to format the message.

The panic message is the equivalent of fmt.Sprintf(format, args).

func AssertIfZero added in v0.3.35

func AssertIfZero(elem any, msg string)

AssertIfZero panics if the element is zero.

Parameters:

  • elem: The element to check.
  • msg: The message to show if the element is zero.

The panic message is the string msg.

func AssertNil added in v0.3.40

func AssertNil[T any](elem *T, param_name string)

AssertNil panics if the element is nil but returns the element dereferenced if it is not nil.

Parameters:

  • elem: The element to check.
  • param_name: The name of the parameter.

Returns:

  • T: The element if it is not nil.

The panic message is the message "Parameter \"param_name\" must not be nil".

func AssertOk added in v0.3.40

func AssertOk(ok bool, format string, args ...any)

AssertOk panics if the condition is false.

Parameters:

  • ok: The condition to check.
  • format: The format of the message to show if the condition is false.
  • args: The arguments to format the message.

The format should be the function name and the args should be the parameters.

Example:

func MyFunc(param1 string, param2 int) {
    ok := SomeFunc(param1, param2)
    AssertOk(ok, "SomeFunc(%s, %d)", param1, param2) // panic("In SomeFunc(param1, param2) = false")
}

func AssertParam added in v0.3.35

func AssertParam(param string, cond bool, reason error)

AssertParam panics if the condition is false with a parameter name.

Parameters:

  • param: The name of the parameter.
  • cond: The condition to check.
  • reason: The reason why the parameter is invalid.

The panic message is of error *ErrInvalidParameter.

func Compare

func Compare[T Comparable](a, b T) (int, bool)

Compare compares two values of the same type that implement the Comparable interface. If the values are equal, the function returns 0. If the first value is less than the second value, the function returns -1. If the first value is greater than the second value, the function returns 1.

Parameters:

  • a: The first value to compare.
  • b: The second value to compare.

Returns:

  • int: -1 if a < b, 0 if a == b, 1 if a > b.
  • bool: True if the values are comparable.

Behaviors:

  • If the values are not comparable, the function returns false.

func CompareAny

func CompareAny(a, b any) (int, bool)

Compare compares two values of the same type that implement the Comparable interface. If the values are equal, the function returns 0. If the first value is less than the second value, the function returns -1. If the first value is greater than the second value, the function returns 1.

Parameters:

  • a: The first value to compare.
  • b: The second value to compare.

Returns:

  • int: -1 if a < b, 0 if a == b, 1 if a > b.
  • bool: True if the values are comparable.

Behaviors:

  • If the values are not comparable, the function returns false.

func CopyOf

func CopyOf(elem any) any

CopyOf creates a copy of the element by either calling the Copy method if the element implements the Copier interface or returning the element as is.

Parameters:

  • elem: The element to copy.

Returns:

  • any: A copy of the element.

func EqualOf

func EqualOf(a, b any) bool

EqualOf compares two objects of the same type. If any of the objects implements the Equaler interface, the Equals method is called. Otherwise, the objects are compared using the == operator. However, a is always checked first.

Parameters:

  • a: The first object to compare.
  • b: The second object to compare.

Returns:

  • bool: True if the objects are equal, false otherwise.

Behaviors:

  • Nil objects are always considered different.

func ExtractFirsts

func ExtractFirsts[A any, B any](pairs []Pair[A, B]) []A

ExtractFirsts extracts all the first elements from the given slice of pairs.

Parameters:

  • pairs: The slice of pairs.

Returns:

  • []A: The slice of first elements.

func ExtractSeconds

func ExtractSeconds[A any, B any](pairs []Pair[A, B]) []B

ExtractSeconds extracts all the second elements from the given slice of pairs.

Parameters:

  • pairs: The slice of pairs.

Returns:

  • []B: The slice of second elements.

func GetOrdinalSuffix

func GetOrdinalSuffix(number int) string

GetOrdinalSuffix returns the ordinal suffix for a given integer.

Parameters:

  • number: The integer for which to get the ordinal suffix. Negative numbers are treated as positive.

Returns:

  • string: The ordinal suffix for the number.

Example:

  • GetOrdinalSuffix(1) returns "1st"
  • GetOrdinalSuffix(2) returns "2nd"

func GoStringOf

func GoStringOf(elem any) string

GoStringOf returns a string representation of the element.

Parameters:

  • elem: The element to get the string representation of.

Returns:

  • string: The string representation of the element.

Behaviors:

  • If the element is nil, the function returns "nil".
  • If the element implements the fmt.GoStringer interface, the function returns the result of the GoString method.
  • If the element implements the fmt.Stringer interface, the function returns the result of the String method.
  • If the element is a string, the function returns the string enclosed in double quotes.
  • If the element is an error, the function returns the error message enclosed in double quotes.
  • Otherwise, the function returns the result of the %#v format specifier.

func Is added in v0.3.34

func Is[T error](err error) bool

Is is function that checks if an error is of type T.

Parameters:

  • err: The error to check.

Returns:

  • bool: true if the error is of type T, false otherwise (including if the error is nil).

func IsDone added in v0.3.37

func IsDone(err error) bool

IsDone checks if the iterator is exhausted.

Parameters:

  • err: The error to check.

Returns:

  • bool: True if the iterator is exhausted, false otherwise.

func IsEmpty

func IsEmpty(elem any) bool

IsEmpty returns true if the element is empty.

Parameters:

  • elem: The element to check.

Returns:

  • bool: True if the element is empty, false otherwise.

func IsErrIgnorable added in v0.3.34

func IsErrIgnorable(err error) bool

IsErrIgnorable checks if an error is an *ErrIgnorable or *ErrInvalidParameter error. If the error is nil, the function returns false.

Parameters:

  • err: The error to check.

Returns:

  • bool: True if the error is an *ErrIgnorable or *ErrInvalidParameter error, otherwise false.

func IsNoError added in v0.3.34

func IsNoError(err error) bool

IsNoError checks if an error is a no error error or if it is nil.

Parameters:

  • err: The error to check.

Returns:

  • bool: True if the error is a no error error or if it is nil, otherwise false.

func IsNotFound added in v0.3.37

func IsNotFound(err error) bool

IsNotFound checks if the error is an ErrNotFound.

Parameters:

  • err: The error to check.

Returns:

  • bool: True if the error is an ErrNotFound, false otherwise.

func LimitErrorMsg added in v0.3.34

func LimitErrorMsg(err error, limit int) error

LimitErrorMsg limits the error message to a certain number of unwraps. It returns the top level error for allowing to print the error message with the limit of unwraps applied.

If the error is nil or the limit is less than 0, the function does nothing.

Parameters:

  • err: The error to limit.
  • limit: The limit of unwraps.

Returns:

  • error: The top level error with the limit of unwraps applied.

func Max

func Max[T Comparable](a, b T) T

Max is a function that takes two parameters, a and b, of any type T according to the uc.CompareOf function and returns the larger of the two values.

Parameters:

  • a, b: The two values to compare.

Return:

  • T: The larger of the two values.

func Min

func Min[T Comparable](a, b T) T

Min is a function that takes two parameters, a and b, of any type T according to the uc.CompareOf function and returns the smaller of the two values.

Parameters:

  • a, b: The two values to compare.

Return:

  • T: The smaller of the two values.

func SliceCopy added in v0.3.31

func SliceCopy[T Copier](s []T) []T

SliceCopy creates a copy of a slice of elements by calling the Copy method of each element if the element implements the Copier interface.

Whether or not the copy is a shallow or deep copy depends on the implementation of the Copy method of the element.

Parameters:

  • s: The slice of elements to copy.

Returns:

  • []T: The copy of the slice of elements.

func SliceOf added in v0.3.34

func SliceOf[T any](elem any) []T

SliceOf converts any type to a slice of elements of the same type.

Parameters:

  • elem: The element to convert to a slice.

Returns:

  • []T: The slice representation of the element.

Behaviors:

  • Nil elements are converted to nil slices.
  • Slice elements are returned as is.
  • Slicer elements have their Slice method called.
  • Other elements are converted to slices containing a single element.

func StringOf

func StringOf(elem any) string

StringOf converts any type to a string.

Parameters:

  • elem: The element to convert to a string.

Returns:

  • string: The string representation of the element.

Behaviors:

  • String elements are returned as is.
  • fmt.Stringer elements have their String method called.
  • error elements have their Error method called.
  • []byte and []rune elements are converted to strings.
  • Other elements are converted to strings using fmt.Sprintf and the %v format.

func TypeOf

func TypeOf(value any) string

TypeOf returns the type of the value as a string.

Parameters:

  • value: The value to get the type of.

Returns:

  • string: The type of the value.

Types

type Builder added in v0.3.34

type Builder[T any] struct {
	// contains filtered or unexported fields
}

Builder is a struct that allows building iterators over a collection of elements.

func (*Builder[T]) Add added in v0.3.34

func (b *Builder[T]) Add(element T)

Add is a method of the Builder type that appends an element to the buffer.

Parameters:

  • element: The element to append to the buffer.

func (*Builder[T]) AddMany added in v0.3.34

func (b *Builder[T]) AddMany(elements []T)

AddMany is a method of the Builder type that appends multiple elements to the buffer.

Parameters:

  • elements: The elements to append to the buffer.

func (*Builder[T]) Build added in v0.3.34

func (b *Builder[T]) Build() *SimpleIterator[T]

Build creates a new iterator over the buffer of elements.

It clears the buffer after creating the iterator.

Returns:

  • *SimpleIterator[T]: The new iterator.

func (*Builder[T]) Clear added in v0.3.34

func (b *Builder[T]) Clear()

Clear is a method of the Builder type that removes all elements from the buffer.

type Comparable

type Comparable interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 |
		~float32 | ~float64 | ~string
}

Comparable is an interface that defines the behavior of a type that can be compared with other values of the same type using the < and > operators. The interface is implemented by the built-in types int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64, and string.

type Copier

type Copier interface {
	// Copy creates a copy of the element.
	//
	// Returns:
	//   - Copier: The copy of the element.
	Copy() Copier
}

Copier is an interface that provides a method to create a copy of an element.

type DoFunc

type DoFunc[T any] func(T)

DoFunc is a generic type that represents a function that takes a value and does something with it.

Parameters:

  • T: The type of the value.

type DualDoFunc

type DualDoFunc[T any, U any] func(T, U)

DualDoFunc is a generic type that represents a function that takes two values and does something with them.

Parameters:

  • T: The type of the first value.
  • U: The type of the second value.

type DynamicIterator added in v0.3.34

type DynamicIterator[E, T any] struct {
	// contains filtered or unexported fields
}

DynamicIterator is a struct that allows iterating over a collection of iterators of type Iterater[T].

func NewDynamicIterator added in v0.3.34

func NewDynamicIterator[E, T any](source Iterater[E], f func(E) Iterater[T]) *DynamicIterator[E, T]

IteratorFromIterator creates a new iterator over a collection of iterators of type Iterater[T]. It uses the input iterator to iterate over the collection of iterators and return the elements from each iterator in turn.

Parameters:

  • source: The iterator over the collection of iterators to iterate over.
  • f: The transition function that takes an element of type E and returns an iterator.

Return:

  • *DynamicIterator[E, T]: The new iterator. Nil if f or source is nil.

func (*DynamicIterator[E, T]) Consume added in v0.3.34

func (di *DynamicIterator[E, T]) Consume() (T, error)

Consume implements the Iterater interface.

func (*DynamicIterator[E, T]) Restart added in v0.3.34

func (di *DynamicIterator[E, T]) Restart()

Restart implements the Iterater interface.

type Enumer

type Enumer interface {
	~int

	fmt.Stringer
}

Enumer is an interface representing an enumeration.

type Equaler

type Equaler interface {
	// Equals returns true if the object is equal to the other object.
	//
	// Parameters:
	//   - other: The other object to compare to.
	//
	// Returns:
	//   - bool: True if the objects are equal, false otherwise.
	Equals(other Equaler) bool
}

Equaler is an interface that defines a method to compare two objects of the same type for equality.

type ErrAfter added in v0.3.34

type ErrAfter struct {
	// After is the element that was processed before the error occurred.
	After string

	// Reason is the reason for the error.
	Reason error
}

ErrAfter is an error that is returned when something goes wrong after a certain element in a stream of data.

func NewErrAfter added in v0.3.34

func NewErrAfter(after string, reason error) *ErrAfter

NewErrAfter creates a new ErrAfter error.

Parameters:

  • after: The element that was processed before the error occurred.
  • reason: The reason for the error.

Returns:

  • *ErrAfter: A pointer to the new ErrAfter error.

func (*ErrAfter) ChangeReason added in v0.3.34

func (e *ErrAfter) ChangeReason(reason error)

ChangeReason implements the Unwrapper interface.

func (*ErrAfter) Error added in v0.3.34

func (e *ErrAfter) Error() string

Error implements the Unwrapper interface.

Message: "after {after}: {reason}".

However, if the reason is nil, the message is "something went wrong after {after}" instead.

func (*ErrAfter) Unwrap added in v0.3.34

func (e *ErrAfter) Unwrap() error

Unwrap implements the Unwrapper interface.

type ErrAt added in v0.3.34

type ErrAt struct {
	// Index is the index where the error occurred.
	Index int

	// Name is the name of the index.
	Name string

	// Reason is the reason for the error.
	Reason error
}

ErrAt represents an error that occurs at a specific index.

func NewErrAt added in v0.3.34

func NewErrAt(index int, name string, reason error) *ErrAt

NewErrAt creates a new ErrAt error.

Parameters:

  • index: The index where the error occurred.
  • name: The name of the index.
  • reason: The reason for the error.

Returns:

  • *ErrAt: A pointer to the newly created ErrAt.

func (*ErrAt) ChangeReason added in v0.3.34

func (e *ErrAt) ChangeReason(reason error)

ChangeReason implements the Unwrapper interface.

func (*ErrAt) Error added in v0.3.34

func (e *ErrAt) Error() string

Error implements the Unwrapper interface.

Message: "something went wrong at the {index} {name}: {reason}".

However, if the reason is nil, the message is "something went wrong at the {index} {name}" instead.

func (*ErrAt) Unwrap added in v0.3.34

func (e *ErrAt) Unwrap() error

Unwrap implements the Unwrapper interface.

type ErrBefore added in v0.3.34

type ErrBefore struct {
	// Before is the element that was processed before the error occurred.
	Before string

	// Reason is the reason for the error.
	Reason error
}

ErrBefore is an error that is returned when something goes wrong before a certain element in a stream of data.

func NewErrBefore added in v0.3.34

func NewErrBefore(before string, reason error) *ErrBefore

NewErrBefore creates a new ErrBefore error.

Parameters:

  • before: The element that was processed before the error occurred.
  • reason: The reason for the error.

Returns:

  • *ErrBefore: A pointer to the new ErrBefore error.

func (*ErrBefore) ChangeReason added in v0.3.34

func (e *ErrBefore) ChangeReason(reason error)

ChangeReason implements the Unwrapper interface.

func (*ErrBefore) Error added in v0.3.34

func (e *ErrBefore) Error() string

Error implements the Unwrapper interface.

Message: "before {before}: {reason}".

However, if the reason is nil, the message is "something went wrong before {before}" instead.

func (*ErrBefore) Unwrap added in v0.3.34

func (e *ErrBefore) Unwrap() error

Unwrap implements the Unwrapper interface.

type ErrEmpty added in v0.3.34

type ErrEmpty[T any] struct {
	// Value is the value that caused the error.
	Value T
}

ErrEmpty represents an error when a value is empty.

func NewErrEmpty added in v0.3.34

func NewErrEmpty[T any](value T) *ErrEmpty[T]

NewErrEmpty creates a new ErrEmpty error.

Parameters:

  • value: The value that caused the error.

Returns:

  • *ErrEmpty: A pointer to the newly created ErrEmpty.

func (*ErrEmpty[T]) Error added in v0.3.34

func (e *ErrEmpty[T]) Error() string

Error implements the error interface.

Message: "<type> must not be empty"

type ErrExhaustedIter added in v0.3.34

type ErrExhaustedIter struct{}

ErrExhaustedIter is an error type that is returned when an iterator is exhausted (i.e., there are no more elements to consume).

func NewErrExhaustedIter added in v0.3.34

func NewErrExhaustedIter() *ErrExhaustedIter

NewErrExhaustedIter creates a new ErrExhaustedIter error.

Returns:

  • *ErrExhaustedIter: A pointer to the new error.

func (*ErrExhaustedIter) Error added in v0.3.34

func (e *ErrExhaustedIter) Error() string

Error implements the error interface.

Message: "iterator is exhausted"

type ErrGT added in v0.3.34

type ErrGT struct {
	// Value is the value that caused the error.
	Value int
}

ErrGT represents an error when a value is less than or equal to a specified value.

func NewErrGT added in v0.3.34

func NewErrGT(value int) *ErrGT

NewErrGT creates a new ErrGT error with the specified value.

Parameters:

  • value: The minimum value that is not allowed.

Returns:

  • *ErrGT: A pointer to the newly created ErrGT.

func (*ErrGT) Error added in v0.3.34

func (e *ErrGT) Error() string

Error implements the error interface.

Message: "value must be greater than <value>"

If the value is 0, the message is "value must be positive".

type ErrGTE added in v0.3.34

type ErrGTE struct {
	// Value is the value that caused the error.
	Value int
}

ErrGTE represents an error when a value is less than a specified value.

func NewErrGTE added in v0.3.34

func NewErrGTE(value int) *ErrGTE

NewErrGTE creates a new ErrGTE error with the specified value.

Parameters:

  • value: The minimum value that is allowed.

Returns:

  • *ErrGTE: A pointer to the newly created ErrGTE.

func (*ErrGTE) Error added in v0.3.34

func (e *ErrGTE) Error() string

Error implements the error interface.

Message: "value must be greater than or equal to <value>"

If the value is 0, the message is "value must be non-negative".

type ErrIgnorable added in v0.3.34

type ErrIgnorable struct {
	// Err is the error that can be ignored.
	Err error
}

ErrIgnorable represents an error that can be ignored. Useful for indicating that an error is ignorable.

func NewErrIgnorable added in v0.3.34

func NewErrIgnorable(err error) *ErrIgnorable

NewErrIgnorable creates a new ErrIgnorable error.

Parameters:

  • err: The error that can be ignored.

Returns:

  • *ErrIgnorable: A pointer to the newly created ErrIgnorable.

func (*ErrIgnorable) ChangeReason added in v0.3.34

func (e *ErrIgnorable) ChangeReason(reason error)

ChangeReason implements the Unwrapper interface.

func (*ErrIgnorable) Error added in v0.3.34

func (e *ErrIgnorable) Error() string

Error implements the Unwrapper interface.

Message: "ignorable error" if the reason is nil, otherwise the error message.

func (*ErrIgnorable) Unwrap added in v0.3.34

func (e *ErrIgnorable) Unwrap() error

Unwrap implements the Unwrapper interface.

type ErrInvalidCall added in v0.3.34

type ErrInvalidCall struct {
	// FnName is the name of the function.
	FnName string

	// Signature is the Signature of the function.
	Signature reflect.Type

	// Reason is the Reason for the failure.
	Reason error
}

ErrInvalidCall represents an error that occurs when a function is not called correctly.

func NewErrInvalidCall added in v0.3.34

func NewErrInvalidCall(functionName string, function any, reason error) *ErrInvalidCall

NewErrInvalidCall creates a new ErrInvalidCall.

Parameters:

  • functionName: The name of the function.
  • function: The function that failed.
  • reason: The reason for the failure.

Returns:

  • *ErrInvalidCall: A pointer to the new ErrInvalidCall.

func (*ErrInvalidCall) ChangeReason added in v0.3.34

func (e *ErrInvalidCall) ChangeReason(reason error)

ChangeReason implements the Unwrapper interface.

func (*ErrInvalidCall) Error added in v0.3.34

func (e *ErrInvalidCall) Error() string

Error implements the Unwrapper interface.

Message: "call to {function}({signature}) failed: {reason}".

However, if the reason is nil, the message is "call to {function}({signature}) failed" instead.

func (*ErrInvalidCall) Unwrap added in v0.3.34

func (e *ErrInvalidCall) Unwrap() error

Unwrap implements the Unwrapper interface.

type ErrInvalidParameter added in v0.3.34

type ErrInvalidParameter struct {
	// Parameter is the name of the Parameter.
	Parameter string

	// Reason is the Reason for the invalidity of the parameter.
	Reason error
}

ErrInvalidParameter represents an error when a parameter is invalid.

func NewErrInvalidParameter added in v0.3.34

func NewErrInvalidParameter(parameter string, reason error) *ErrInvalidParameter

NewErrInvalidParameter creates a new ErrInvalidParameter error.

Parameters:

  • parameter: The name of the parameter.
  • reason: The reason for the invalidity.

Returns:

  • *ErrInvalidParameter: A pointer to the newly created ErrInvalidParameter.

func NewErrNilParameter added in v0.3.34

func NewErrNilParameter(parameter string) *ErrInvalidParameter

ErrNilParameter represents an error when a parameter is nil. This is a shorthand for NewErrInvalidParameter(parameter, NewErrNilValue()).

Parameters:

  • parameter: The name of the parameter.

Returns:

  • *ErrInvalidParameter: A pointer to the newly created ErrInvalidParameter.

func (*ErrInvalidParameter) ChangeReason added in v0.3.34

func (e *ErrInvalidParameter) ChangeReason(reason error)

ChangeReason implements the Unwrapper interface.

func (*ErrInvalidParameter) Error added in v0.3.34

func (e *ErrInvalidParameter) Error() string

Error implements the Unwrapper interface.

Message: "parameter ({parameter}) is invalid: {reason}".

However, if the reason is nil, the message is "parameter ({parameter}) is invalid" instead.

func (*ErrInvalidParameter) Unwrap added in v0.3.34

func (e *ErrInvalidParameter) Unwrap() error

Unwrap implements the Unwrapper interface.

type ErrInvalidRune added in v0.3.34

type ErrInvalidRune struct {
	// Reason is the reason for the invalidity of the rune.
	Reason error
}

ErrInvalidRune represents an error when an invalid rune is encountered.

func NewErrInvalidRune added in v0.3.34

func NewErrInvalidRune(reason error) *ErrInvalidRune

NewErrInvalidRune creates a new ErrInvalidRuneAt error.

Parameters:

  • reason: The reason for the invalidity of the rune.

Returns:

  • *ErrInvalidRune: A pointer to the newly created ErrInvalidRune.

func (*ErrInvalidRune) ChangeReason added in v0.3.34

func (e *ErrInvalidRune) ChangeReason(reason error)

ChangeReason implements the Unwrapper interface.

func (*ErrInvalidRune) Error added in v0.3.34

func (e *ErrInvalidRune) Error() string

Error implements the Unwrapper interface.

Message: "invalid rune: {reason}".

However, if the reason is nil, the message is "rune is invalid" instead.

func (*ErrInvalidRune) Unwrap added in v0.3.34

func (e *ErrInvalidRune) Unwrap() error

Unwrap implements the Unwrapper interface.

type ErrInvalidUsage added in v0.3.34

type ErrInvalidUsage struct {
	// Reason is the reason for the invalid usage.
	Reason error

	// Usage is the usage of the function.
	Usage string
}

ErrInvalidUsage represents an error that occurs when a function is used incorrectly.

func NewErrInvalidUsage added in v0.3.34

func NewErrInvalidUsage(reason error, usage string) *ErrInvalidUsage

NewErrInvalidUsage creates a new ErrInvalidUsage error.

Parameters:

  • reason: The reason for the invalid usage.
  • usage: The usage of the function.

Returns:

  • *ErrInvalidUsage: A pointer to the new ErrInvalidUsage error.

func (*ErrInvalidUsage) ChangeReason added in v0.3.34

func (e *ErrInvalidUsage) ChangeReason(reason error)

ChangeReason implements the Unwrapper interface.

func (*ErrInvalidUsage) Error added in v0.3.34

func (e *ErrInvalidUsage) Error() string

Error is a method of the Unwrapper interface.

Message: "{reason}. {usage}".

However, if the reason is nil, the message is "invalid usage. {usage}" instead.

If the usage is empty, no usage is added to the message.

func (*ErrInvalidUsage) Unwrap added in v0.3.34

func (e *ErrInvalidUsage) Unwrap() error

Unwrap implements the Unwrapper interface.

type ErrLT added in v0.3.34

type ErrLT struct {
	// Value is the value that caused the error.
	Value int
}

ErrLT represents an error when a value is greater than or equal to a specified value.

func NewErrLT added in v0.3.34

func NewErrLT(value int) *ErrLT

NewErrLT creates a new ErrLT error with the specified value.

Parameters:

  • value: The maximum value that is not allowed.

Returns:

  • *ErrLT: A pointer to the newly created ErrLT.

func (*ErrLT) Error added in v0.3.34

func (e *ErrLT) Error() string

Error implements the error interface.

Message: "value must be less than <value>"

If the value is 0, the message is "value must be negative".

type ErrLTE added in v0.3.34

type ErrLTE struct {
	// Value is the value that caused the error.
	Value int
}

ErrLTE represents an error when a value is greater than a specified value.

func NewErrLTE added in v0.3.34

func NewErrLTE(value int) *ErrLTE

NewErrLTE creates a new ErrLTE error with the specified value.

Parameters:

  • value: The maximum value that is allowed.

Returns:

  • *ErrLTE: A pointer to the newly created ErrLTE.

func (*ErrLTE) Error added in v0.3.34

func (e *ErrLTE) Error() string

Error implements the error interface.

Message: "value must be less than or equal to <value>"

If the value is 0, the message is "value must be non-positive".

type ErrNilValue added in v0.3.34

type ErrNilValue struct{}

ErrNilValue represents an error when a value is nil.

func NewErrNilValue added in v0.3.34

func NewErrNilValue() *ErrNilValue

NewErrNilValue creates a new ErrNilValue error.

Returns:

  • *ErrNilValue: The new ErrNilValue error.

func (*ErrNilValue) Error added in v0.3.34

func (e *ErrNilValue) Error() string

Error implements the error interface.

Message: "pointer must not be nil"

type ErrNoError added in v0.3.34

type ErrNoError struct {
	// Err is the reason for the no error error.
	Err error
}

ErrNoError represents an error when no error occurs.

func NewErrNoError added in v0.3.34

func NewErrNoError(err error) *ErrNoError

NewErrNoError creates a new ErrNoError error.

Parameters:

  • err: The reason for the no error error.

Returns:

  • *ErrNoError: A pointer to the newly created ErrNoError.

func (*ErrNoError) ChangeReason added in v0.3.34

func (e *ErrNoError) ChangeReason(reason error)

ChangeReason implements the Unwrapper interface.

func (*ErrNoError) Error added in v0.3.34

func (e *ErrNoError) Error() string

Error implements the Unwrapper interface.

Message: "no error" if the reason is nil, otherwise the error message.

func (*ErrNoError) Unwrap added in v0.3.34

func (e *ErrNoError) Unwrap() error

Unwrap implements the Unwrapper interface.

type ErrNotFound added in v0.3.37

type ErrNotFound struct{}

ErrNotFound is an error type that is returned when a value is not found.

func NewErrNotFound added in v0.3.37

func NewErrNotFound() *ErrNotFound

NewErrNotFound creates a new ErrNotFound error.

Returns:

  • *ErrNotFound: A pointer to the new error.

func (*ErrNotFound) Error added in v0.3.37

func (e *ErrNotFound) Error() string

Error implements the error interface.

Message: "not found"

type ErrOrSol added in v0.3.34

type ErrOrSol[T any] struct {
	// contains filtered or unexported fields
}

ErrOrSol is a struct that holds a list of errors and a list of solutions.

func (*ErrOrSol[T]) AddAny added in v0.3.34

func (e *ErrOrSol[T]) AddAny(elem any, level int)

AddAny adds an element to the list of errors or solutions if the level is greater or equal to the current level.

Parameters:

  • elem: The element to add.
  • level: The level of the element.

Behaviors:

  • If an error has been added with a level greater than the current level, the error list is reset and the new level is updated.
  • If a solution has been added with a level greater than the current level, the solution list is reset and the new level is updated.

func (*ErrOrSol[T]) AddErr added in v0.3.34

func (e *ErrOrSol[T]) AddErr(err error, level int)

AddErr adds an error to the list of errors if the level is greater or equal to the current level.

Parameters:

  • err: The error to add.
  • level: The level of the error.

Behaviors:

  • If an error has been added with a level greater than the current level, the error list is reset and the new level is updated.
  • If the error is nil, the ignoreErr flag is set to true and the error list is reset.

func (*ErrOrSol[T]) AddSol added in v0.3.34

func (e *ErrOrSol[T]) AddSol(sol T, level int)

AddSol adds a solution to the list of solutions if the level is greater or equal to the current level.

Parameters:

  • sol: The solution to add.
  • level: The level of the solution.

Behaviors:

  • If a solution has been added with a level greater than the current level, the solution list is reset and the new level is updated.
  • This function sets the ignoreErr flag to true and resets the error list.

func (*ErrOrSol[T]) GetErrors added in v0.3.34

func (e *ErrOrSol[T]) GetErrors() []error

GetErrors returns the list of errors.

Returns:

  • []error: The list of errors.

func (*ErrOrSol[T]) GetSolutions added in v0.3.34

func (e *ErrOrSol[T]) GetSolutions() []T

GetSolutions returns the list of solutions.

Returns:

  • []T: The list of solutions.

func (*ErrOrSol[T]) HasError added in v0.3.34

func (e *ErrOrSol[T]) HasError() bool

HasError checks if errors are not ignored and if the error list is not empty.

Returns:

  • bool: True if errors are not ignored and the error list is not empty, otherwise false.

type ErrOutOfBounds added in v0.3.34

type ErrOutOfBounds struct {
	// LowerBound and UpperBound are the lower and upper bounds of the range,
	// respectively.
	LowerBound, UpperBound int

	// LowerInclusive and UpperInclusive are flags indicating whether the lower
	// and upper bounds are inclusive, respectively.
	LowerInclusive, UpperInclusive bool

	// Value is the value that caused the error.
	Value int
}

ErrOutOfBounds represents an error when a value is out of a specified range.

func NewErrOutOfBounds added in v0.3.34

func NewErrOutOfBounds(value int, lowerBound, upperBound int) *ErrOutOfBounds

NewOutOfBounds creates a new ErrOutOfBound error. By default, the lower bound is inclusive and the upper bound is exclusive.

Parameters:

  • lowerBound, upperbound: The lower and upper bounds of the range, respectively.
  • value: The value that caused the error.

Returns:

  • *ErrOutOfBounds: A pointer to the newly created ErrOutOfBound.

func (*ErrOutOfBounds) Error added in v0.3.34

func (e *ErrOutOfBounds) Error() string

Error implements the error interface.

Message: "value (value) not in range <lowerBound, upperBound>"

If the lower bound is inclusive, the message uses square brackets. If the upper bound is inclusive, the message uses square brackets. Otherwise, the message uses parentheses.

func (*ErrOutOfBounds) WithLowerBound added in v0.3.34

func (e *ErrOutOfBounds) WithLowerBound(isInclusive bool) *ErrOutOfBounds

WithLowerBound sets the inclusivity of the lower bound.

Parameters:

  • isInclusive: A boolean indicating whether the lower bound is inclusive.

Returns:

  • *ErrOutOfBound: The error instance for chaining.

func (*ErrOutOfBounds) WithUpperBound added in v0.3.34

func (e *ErrOutOfBounds) WithUpperBound(isInclusive bool) *ErrOutOfBounds

WithUpperBound sets the inclusivity of the upper bound.

Parameters:

  • isInclusive: A boolean indicating whether the upper bound is inclusive.

Returns:

  • *ErrOutOfBound: The error instance for chaining.

type ErrPanic added in v0.3.34

type ErrPanic struct {
	// Value is the value that caused the panic.
	Value any
}

ErrPanic represents an error when a panic occurs.

func NewErrPanic added in v0.3.34

func NewErrPanic(value any) *ErrPanic

NewErrPanic creates a new ErrPanic error.

Parameters:

  • value: The value that caused the panic.

Returns:

  • *ErrPanic: A pointer to the newly created ErrPanic.

func (*ErrPanic) Error added in v0.3.34

func (e *ErrPanic) Error() string

Error implements the error interface.

Message: "panic: {value}"

type ErrPossibleError added in v0.3.34

type ErrPossibleError struct {
	// Reason is the reason for the possible error.
	Reason error

	// Possible is the possible error.
	Possible error
}

ErrPossibleError represents an error that occurs when a possible error is encountered.

func NewErrPossibleError added in v0.3.34

func NewErrPossibleError(reason error, possible error) *ErrPossibleError

NewErrPossibleError creates a new ErrPossibleError error.

Parameters:

  • reason: The reason for the possible error.
  • possible: The possible error.

Returns:

  • *ErrPossibleError: A pointer to the new ErrPossibleError error.

func (*ErrPossibleError) ChangeReason added in v0.3.34

func (e *ErrPossibleError) ChangeReason(reason error)

ChangeReason implements the Unwrapper interface.

func (*ErrPossibleError) Error added in v0.3.34

func (e *ErrPossibleError) Error() string

Error implements the Unwrapper interface.

Message: "{reason}. It is possible that {possible}".

However, if the reason is nil, the message is "no error occurred. It is possible that {possible}" instead.

If the possible error is nil, the "It is possible that {possible}" part is omitted.

func (*ErrPossibleError) Unwrap added in v0.3.34

func (e *ErrPossibleError) Unwrap() error

Unwrap implements the Unwrapper interface.

type ErrUnexpectedError added in v0.3.34

type ErrUnexpectedError struct {
	// Reason is the reason for the unexpected error.
	Reason error
}

ErrUnexpectedError represents an error that occurs unexpectedly.

func NewErrUnexpectedError added in v0.3.34

func NewErrUnexpectedError(reason error) *ErrUnexpectedError

NewErrUnexpectedError creates a new ErrUnexpectedError error.

Parameters:

  • reason: The reason for the unexpected error.

Returns:

  • *ErrUnexpectedError: A pointer to the new ErrUnexpectedError error.

func (*ErrUnexpectedError) ChangeReason added in v0.3.34

func (e *ErrUnexpectedError) ChangeReason(reason error)

ChangeReason implements the Unwrapper interface.

func (*ErrUnexpectedError) Error added in v0.3.34

func (e *ErrUnexpectedError) Error() string

Error implements the Unwrapper interface.

Message: "unexpected error: {reason}".

However, if the reason is nil, the message is "unexpected error" instead.

func (*ErrUnexpectedError) Unwrap added in v0.3.34

func (e *ErrUnexpectedError) Unwrap() error

Unwrap implements the Unwrapper interface.

type ErrUnexpectedType added in v0.3.34

type ErrUnexpectedType[T any] struct {
	// Elem is the element that caused the error.
	Elem T

	// Kind is the category of the type that was expected.
	Kind string
}

ErrUnexpectedType represents an error when a value has an invalid type.

func NewErrUnexpectedType added in v0.3.34

func NewErrUnexpectedType[T any](kind string, elem T) *ErrUnexpectedType[T]

NewErrUnexpectedType creates a new ErrUnexpectedType error.

Parameters:

  • typeName: The name of the type that was expected.
  • elem: The element that caused the error.

Returns:

  • *ErrUnexpectedType: A pointer to the newly created ErrUnexpectedType.

func (*ErrUnexpectedType[T]) Error added in v0.3.34

func (e *ErrUnexpectedType[T]) Error() string

Error implements the error interface.

Message: "type <type> is not a valid <kind> type"

type ErrVariableError added in v0.3.34

type ErrVariableError struct {
	// Variable is the name of the variable that caused the error.
	Variable string

	// Reason is the reason for the variable error.
	Reason error
}

ErrVariableError represents an error that occurs when a variable is invalid.

func NewErrVariableError added in v0.3.34

func NewErrVariableError(variable string, reason error) *ErrVariableError

NewErrVariableError creates a new ErrVariableError error.

Parameters:

  • variable: The name of the variable that caused the error.
  • reason: The reason for the variable error.

Returns:

  • *ErrVariableError: A pointer to the new ErrVariableError error.

func (*ErrVariableError) ChangeReason added in v0.3.34

func (e *ErrVariableError) ChangeReason(reason error)

ChangeReason implements the Unwrapper interface.

func (*ErrVariableError) Error added in v0.3.34

func (e *ErrVariableError) Error() string

Error implements the Unwrapper interface.

Message: "variable ({variable}) error: {reason}".

However, if the reason is nil, the message is "variable ({variable}) error" instead.

func (*ErrVariableError) Unwrap added in v0.3.34

func (e *ErrVariableError) Unwrap() error

Unwrap implements the Unwrapper interface.

type ErrWhile added in v0.3.34

type ErrWhile struct {
	// Operation is the operation that was being performed.
	Operation string

	// Reason is the reason for the error.
	Reason error
}

ErrWhile represents an error that occurs while performing an operation.

func NewErrWhile added in v0.3.34

func NewErrWhile(operation string, reason error) *ErrWhile

NewErrWhile creates a new ErrWhile error.

Parameters:

  • operation: The operation that was being performed.
  • reason: The reason for the error.

Returns:

  • *ErrWhile: A pointer to the newly created ErrWhile.

func (*ErrWhile) ChangeReason added in v0.3.34

func (e *ErrWhile) ChangeReason(reason error)

ChangeReason implements the Unwrapper interface.

func (*ErrWhile) Error added in v0.3.34

func (e *ErrWhile) Error() string

Error implements the Unwrapper interface.

Message: "error while {operation}: {reason}"

However, if the reason is nil, the message is "an error occurred while {operation}" instead.

func (*ErrWhile) Unwrap added in v0.3.34

func (e *ErrWhile) Unwrap() error

Unwrap implements the Unwrapper interface.

type ErrWhileAt added in v0.3.34

type ErrWhileAt struct {
	// Index is the index where the error occurred.
	Index int

	// Element is the element where the index is pointing to.
	Element string

	// Operation is the operation that was being performed.
	Operation string

	// Reason is the reason for the error.
	Reason error
}

ErrWhileAt represents an error that occurs while performing an operation at a specific index.

func NewErrWhileAt added in v0.3.34

func NewErrWhileAt(operation string, index int, elem string, reason error) *ErrWhileAt

NewErrWhileAt creates a new ErrWhileAt error.

Parameters:

  • operation: The operation that was being performed.
  • index: The index where the error occurred.
  • elem: The element where the index is pointing to.
  • reason: The reason for the error.

Returns:

  • *ErrWhileAt: A pointer to the newly created ErrWhileAt.

func (*ErrWhileAt) ChangeReason added in v0.3.34

func (e *ErrWhileAt) ChangeReason(reason error)

ChangeReason implements the Unwrapper interface.

func (*ErrWhileAt) Error added in v0.3.34

func (e *ErrWhileAt) Error() string

Error implements the Unwrapper interface.

Message: "an error occurred while {operation} at index {index} {element}: {reason}"

However, if the reason is nil, the message is "an error occurred while {operation} at index {index} {element}" instead.

func (*ErrWhileAt) Unwrap added in v0.3.34

func (e *ErrWhileAt) Unwrap() error

Unwrap implements the Unwrapper interface.

type ErrorIfFunc

type ErrorIfFunc[T any] func(elem T) error

ErrorIfFunc is a function type that takes an element and returns an error if the element is invalid.

Parameters:

  • elem: The element to check.

Returns:

  • error: An error if the element is invalid.

type EvalManyFunc

type EvalManyFunc[E, R any] func(elem E) ([]R, error)

EvalManyFunc is a function that evaluates many elements.

Parameters:

  • elem: The element to evaluate.

Returns:

  • []R: The results of the evaluation.
  • error: An error if the evaluation failed.

type EvalOneFunc

type EvalOneFunc[E, R any] func(elem E) (R, error)

EvalOneFunc is a function that evaluates one element.

Parameters:

  • elem: The element to evaluate.

Returns:

  • R: The result of the evaluation.
  • error: An error if the evaluation failed.

type Iterable added in v0.3.34

type Iterable[T any] interface {
	// Iterator returns an iterator over the collection of elements.
	//
	// Returns:
	//   - Iterater[T]: An iterator over the collection of elements.
	Iterator() Iterater[T]
}

Iterable is an interface that defines a method to get an iterator over a collection of elements of type T. It is implemented by data structures that can be iterated over.

type Iterater added in v0.3.34

type Iterater[T any] interface {
	// Consume advances the iterator to the next element in the
	// collection and returns the current element.
	//
	// Returns:
	//  - T: The current element in the collection.
	//  - error: An error if the iterator is exhausted or if an error occurred
	//    while consuming the element.
	Consume() (T, error)

	// Restart resets the iterator to the beginning of the
	// collection.
	Restart()
}

Iterater is an interface that defines methods for an iterator over a collection of elements of type T.

func IteratorOf added in v0.3.34

func IteratorOf[T any](elem any) Iterater[T]

IteratorOf converts any type to an iterator over elements of the same type.

Parameters:

  • elem: The element to convert to an iterator.

Returns:

  • Iterater[T]: The iterator over the element.

Behaviors:

  • IF elem is nil, an empty iterator is returned.
  • IF elem -implements-> Iterater[T], the element is returned as is.
  • IF elem -implements-> Iterable[T], the element's Iterator method is called.
  • IF elem -implements-> []T, a new iterator over the slice is created.
  • ELSE, a new iterator over a single-element collection is created.

type MainFunc

type MainFunc func() error

MainFunc is a function type that takes no parameters and returns an error. It is used to represent things such as the main function of a program.

Returns:

  • error: An error if the function failed.

type Objecter

type Objecter interface {
	fmt.Stringer
	Copier
	Equaler
}

Objecter is an interface that defines the behavior of an object that can be copied, compared, and converted to a string.

type Pair

type Pair[A, B any] struct {
	// The first value.
	First A

	// The second value.
	Second B
}

Pair is a pair of values.

func NewPair

func NewPair[A any, B any](first A, second B) Pair[A, B]

NewPair creates a new pair.

Parameters:

  • first: The first value.
  • second: The second value.

Returns:

  • Pair[A, B]: The new pair.

func (Pair[A, B]) String

func (p Pair[A, B]) String() string

String implements the fmt.Stringer interface.

type ProceduralIterator added in v0.3.34

type ProceduralIterator[E Iterable[T], T any] struct {
	// contains filtered or unexported fields
}

ProceduralIterator is a struct that allows iterating over a collection of iterators of type Iterater[T].

func NewProceduralIterator added in v0.3.34

func NewProceduralIterator[E Iterable[T], T any](source Iterater[E]) *ProceduralIterator[E, T]

IteratorFromIterator creates a new iterator over a collection of iterators of type Iterater[T]. It uses the input iterator to iterate over the collection of iterators and return the elements from each iterator in turn.

Parameters:

  • source: The iterator over the collection of iterators to iterate over.

Return:

  • *ProceduralIterator[E, T]: The new iterator over the collection of elements. Nil if source is nil.

func (*ProceduralIterator[E, T]) Consume added in v0.3.34

func (pi *ProceduralIterator[E, T]) Consume() (T, error)

Consume implements the Iterater interface.

func (*ProceduralIterator[E, T]) Restart added in v0.3.34

func (pi *ProceduralIterator[E, T]) Restart()

Restart implements the Iterater interface.

type RoutineFunc

type RoutineFunc func()

Routine is a function type used to represent a go routine.

type SimpleIterator added in v0.3.34

type SimpleIterator[T any] struct {
	// contains filtered or unexported fields
}

SimpleIterator is a struct that allows iterating over a slice of elements of any type.

func NewSimpleIterator added in v0.3.34

func NewSimpleIterator[T any](values []T) *SimpleIterator[T]

NewSimpleIterator creates a new iterator over a slice of elements of type T.

Parameters:

  • values: The slice of elements to iterate over.

Return:

  • *SimpleIterator[T]: A new iterator over the given slice of elements.

Behaviors:

  • If values is nil, the iterator is initialized with an empty slice.
  • Modifications to the slice of elements after creating the iterator will affect the values seen by the iterator.

func (*SimpleIterator[T]) Consume added in v0.3.34

func (iter *SimpleIterator[T]) Consume() (T, error)

Consume implements the Iterater interface.

func (*SimpleIterator[T]) Restart added in v0.3.34

func (iter *SimpleIterator[T]) Restart()

Restart implements the Iterater interface.

type SliceIterator added in v0.3.37

type SliceIterator[T any] struct {
	// contains filtered or unexported fields
}

SliceIterator is a struct that allows iterating over a collection of iterators of type Iterater[T].

func NewSliceIterator added in v0.3.37

func NewSliceIterator[T any](source Iterater[[]T]) *SliceIterator[T]

IteratorFromIterator creates a new iterator over a collection of iterators of type Iterater[T]. It uses the input iterator to iterate over the collection of iterators and return the elements from each iterator in turn.

Parameters:

  • source: The iterator over the collection of iterators to iterate over.

Return:

  • *SliceIterator[T]: The new iterator over the collection of elements. Nil if source is nil.

func (*SliceIterator[T]) Consume added in v0.3.37

func (pi *SliceIterator[T]) Consume() (T, error)

Consume implements the Iterater interface.

func (*SliceIterator[T]) Restart added in v0.3.37

func (pi *SliceIterator[T]) Restart()

Restart implements the Iterater interface.

type Slicer added in v0.3.34

type Slicer[T any] interface {
	// Slice returns a slice containing all the elements in the data structure.
	//
	// Returns:
	//   - []T: A slice containing all the elements in the data structure.
	Slice() []T

	Iterable[T]
}

Slicer is an interface that provides a method to convert a data structure to a slice.

type Unwrapper added in v0.3.34

type Unwrapper interface {
	// Unwrap returns the error that this error wraps.
	//
	// Returns:
	//   - error: The error that this error wraps.
	Unwrap() error

	// ChangeReason changes the reason of the error.
	//
	// Parameters:
	//   - reason: The new reason of the error.
	ChangeReason(reason error)

	error
}

Unwrapper is an interface that defines a method to unwrap an error.

Jump to

Keyboard shortcuts

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