Documentation
¶
Index ¶
- Variables
- func Assert(cond bool, msg string)
- func ClearFrom[T any](slice *[]T, from_idx int)
- func ClearTo[T any](slice *[]T, to_idx int)
- func EitherOrString(elems []string) string
- func Must[T any](res T, err error) T
- func NewErrAssertFail(inner error) error
- func NewErrAt(idx int, inner error) error
- func NewErrBadParam(param_name, msg string) error
- func NewErrInvalidType(got any, wants ...any) error
- func NewErrMust(inner error) error
- func NewErrNilParam(param_name string) error
- func NewErrNotAsExpected(quote bool, kind string, got string, expecteds ...string) error
- func TODO(msg string)
- func Warn(msg string)
- type ErrAssertFail
- type ErrAt
- type ErrBadParam
- type ErrInvalidType
- type ErrMust
- type ErrNotAsExpected
- type Pair
Constants ¶
This section is empty.
Variables ¶
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 )
var SD commonT
SD is the namespace for SD-like functions.
Functions ¶
func Assert ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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.
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.
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.
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.