common

package
v0.0.0-...-84d6781 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNilReceiver occurs when a method is called on a receiver who was not
	// expected to be nil. This error can be checked with the == operator.
	//
	// Format:
	// 	"receiver must not be nil"
	ErrNilReceiver error
)
View Source
var SD commonT

SD is the namespace for SD-like functions.

Functions

func Assert

func Assert(cond bool, msg string)

Assert asserts that the given condition is true. If it is not, a panic is triggered with an ErrAssertFail error.

Parameters:

  • cond: The condition to assert.
  • msg: The error message for the panic.

func ClearFrom

func ClearFrom[T any](slice *[]T, from_idx int)

ClearFrom clears all elements in the slice starting from the given index. If the given index is less than or equal to 0, the entire slice is cleared. If the given index is greater than or equal to the length of the slice, the function does nothing.

Parameters:

  • slice: The slice to clear elements from.
  • from_idx: The index to start clearing elements from.

func ClearTo

func ClearTo[T any](slice *[]T, to_idx int)

ClearTo clears all elements in the slice up to (but not including) the given index. If the given index is less than 0, the function does nothing. If the given index is greater than or equal to the length of the slice, the entire slice is cleared.

Parameters:

  • slice: The slice to clear elements from.
  • to_idx: The index up to which to clear elements.

func EitherOrString

func EitherOrString(elems []string) string

EitherOrString is a function that returns a string representation of a slice of strings. Empty strings are ignored.

Parameters:

  • values: The values to convert to a string.

Returns:

  • string: The string representation.

Example:

EitherOrString([]string{"a", "b", "c"}) // "either a, b, or c"

func Must

func Must[T any](res T, err error) T

Must is a helper function that wraps a call to a function that returns (T, error) and panics if the error is not nil.

This function is intended to be used to handle errors in a way that is easy to read and write.

Parameters:

  • res: The result of the function.
  • err: The error returned by the function.

Returns:

  • T: The result of the function.

func NewErrAssertFail

func NewErrAssertFail(inner error) error

NewErrAssertFail returns a new ErrAssertFail from the given error.

Parameters:

  • inner: The error.

Returns:

  • error: The new error. Never returns nil.

Format:

"ASSERT FAIL: <reason>"

Where, <reason> is the error message of the given error. If nil, "something went wrong" is used.

func NewErrAt

func NewErrAt(idx int, inner error) error

NewErrAt returns a new ErrAt from the given index and inner error.

Parameters:

  • idx: The index at which the error occurred.
  • inner: The inner error.

Returns:

  • error: The new error. Never returns nil.

Format:

"at index <idx>: <reason>"

Where:

  • <idx>: The index at which the error occurred.
  • <reason>: The reason for the error. If nil, "something went wrong" is used instead.

func NewErrBadParam

func NewErrBadParam(param_name, msg string) error

NewErrBadParam creates a new ErrBadParam error with the specified parameter name and message.

Parameters:

  • param_name: The name of the parameter causing the error.
  • msg: The error message describing why the parameter is bad.

Returns:

  • error: An instance of ErrBadParam. Never returns nil.

Format:

"parameter (<param_name>) <msg>"

where:

  • (<param_name>): The name of the parameter. If empty, it is omitted.
  • <msg>: The error message describing why the parameter is bad. If empty, "is invalid" is used.

func NewErrInvalidType

func NewErrInvalidType(got any, wants ...any) error

NewErrInvalidType creates a new ErrInvalidType error with the specified expected types and actual type.

Parameters:

  • wants: The expected types.
  • got: The actual type.

Returns:

  • error: The new ErrInvalidType error. Never returns nil.

Format:

"want <want>, got <got>"

Where:

  • <want>: The expected type.
  • <got>: The actual type. If nil, "<nil>" is used instead.

func NewErrMust

func NewErrMust(inner error) error

NewErrMust creates a new ErrMust error.

Parameters:

  • inner: The inner error.

Returns:

  • error: The new error. Never returns nil.

Format:

"must: <inner>"

Where, <inner>: The inner error. If nil, "something went wrong" is used instead.

func NewErrNilParam

func NewErrNilParam(param_name string) error

NewErrNilParam is a convenience function that creates a new ErrBadParam error with the specified parameter name and the message "must not be nil".

Parameters:

  • param_name: The name of the parameter causing the error.

Returns:

  • error: An instance of ErrBadParam. Never returns nil.

Format:

"parameter (<param_name>) must not be nil"

where:

  • (<param_name>): The name of the parameter. If empty, it is omitted.

func NewErrNotAsExpected

func NewErrNotAsExpected(quote bool, kind string, got string, expecteds ...string) error

NewErrNotAsExpected creates a new ErrNotAsExpected error.

Parameters:

  • quote: Whether or not to quote the strings in the error message.
  • kind: The kind of thing that was not as expected. This is used in the error message.
  • got: The actual value. If empty, "nothing" is used in the error message.
  • expecteds: The expected values. If empty, "something" is used in the error message.

Returns:

  • error: The new error. Never returns nil.

Format:

"expected <kind> to be <expected>, got <got>"

Where:

  • <kind>: The kind of thing that was not as expected. This is used in the error message.
  • <expected>: The expected values. This is used in the error message.
  • <got>: The actual value. This is used in the error message. If nil, "nothing" is used instead.

Duplicate values are automatically removed and the list of expected values is sorted in ascending order.

func TODO

func TODO(msg string)

TODO panics with a TODO message. The given message is appended to the string "TODO: ". If the message is empty, the message "TODO: Handle this case" is used instead.

Parameters:

  • msg: The message to append to the string "TODO: ".

This function is meant to be used only when the code is being built or refactored.

func Warn

func Warn(msg string)

Warn prints a warning message to the console. The message is prefixed with "[WARNING]:" to indicate its nature.

Parameters:

  • msg: The warning message to be displayed.

Panics if there is an error writing to the standard output.

Types

type ErrAssertFail

type ErrAssertFail struct {
	// Inner is the error that occurred.
	Inner error
}

ErrAssertFail occurs when an assertion fails.

func (ErrAssertFail) Error

func (e ErrAssertFail) Error() string

Error implements the error interface.

func (ErrAssertFail) Unwrap

func (e ErrAssertFail) Unwrap() error

Unwrap returns the inner error.

Returns:

  • error: The inner error.

type ErrAt

type ErrAt struct {
	// Idx is the index at which the error occurred.
	Idx int

	// Inner is the inner error.
	Inner error
}

ErrAt occurs when an error occurs at a specific index.

func (ErrAt) Error

func (e ErrAt) Error() string

Error implements the error interface.

func (ErrAt) Unwrap

func (e ErrAt) Unwrap() error

Unwrap implements the errors.Wrapper interface.

Returns:

  • error: The inner error.

type ErrBadParam

type ErrBadParam struct {
	// ParamName is the name of the parameter causing the error.
	ParamName string

	// Msg is the error message describing why the parameter is bad.
	Msg string
}

ErrBadParam occurs when a parameter is bad. (i.e., not a valid value).

func (ErrBadParam) Error

func (e ErrBadParam) Error() string

Error implements the error interface.

type ErrInvalidType

type ErrInvalidType struct {
	// Types are the expected types.
	Types []any

	// Got is the actual type.
	Got any
}

ErrInvalidType occurs when a type is not as expected.

func (ErrInvalidType) Error

func (e ErrInvalidType) Error() string

Error implements the error interface.

type ErrMust

type ErrMust struct {
	// Inner is the inner error.
	Inner error
}

ErrMust occurs when something must be true.

func (ErrMust) Error

func (e ErrMust) Error() string

Error implements the error interface.

func (ErrMust) Unwrap

func (e ErrMust) Unwrap() error

Unwrap returns the inner error.

Returns:

  • error: The inner error.

type ErrNotAsExpected

type ErrNotAsExpected struct {
	// Quote if true, the strings will be quoted before being printed.
	Quote bool

	// Kind is the kind of the string that is not as expected.
	Kind string

	// Expecteds are the strings that were expecteds.
	Expecteds []string

	// Got is the actual string.
	Got string
}

ErrNotAsExpected occurs when a string is not as expected.

func (ErrNotAsExpected) Error

func (e ErrNotAsExpected) Error() string

Error implements the error interface.

type Pair

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

	// Second is the second value.
	Second B
}

Pair is a pair of two values.

func NewPair

func NewPair[A, 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.

Jump to

Keyboard shortcuts

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