Documentation
¶
Index ¶
- func Fix(v Fixer, name string, allow_nil bool)
- func Must[T comparable](res T, err error) T
- func MustCall[T any](arg T, fn func(arg T) error, format string, args ...any)
- func New[T interface{ ... }](res T, err error) T
- func NewErrAssertFailed(msg string) error
- func NewErrFixFailed(name string, reason error) error
- func NewErrValidateFailed(name string, reason error) error
- func TODO(msg string)
- func Validate(v Validater, name string, allow_nil bool)
- func WARN(msg string)
- type ErrAssertFailed
- type ErrFixFailed
- type ErrValidateFailed
- type Fixer
- type Validater
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Fix ¶
Fix fixes the struct to the closest valid state. Panics with an ErrValidationFailed error if the struct could not be fixed.
Parameters:
- v: The struct to fix.
- name: The name of the struct. If empty, the name "struct" is used.
- allow_nil: Whether to allow the struct to be nil.
Example:
type MyStruct struct {
Name string
}
func (ms *MyStruct) Fix() error {
if ms == nil {
return nil
}
if ms.Name == "" {
return errors.New("name cannot be empty")
}
return nil
}
ms := &MyStruct{
Name: "",
}
Fix(ms, "ms", false) // Panics: (Fix Failed) ms = name cannot be empty
func Must ¶
func Must[T comparable](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 MustCall ¶
MustCall asserts whether the function does not return an error for the given argument. If the function returns an error, it panics with an ErrAssertFailed error.
Parameters:
- arg: The argument to pass to the function.
- fn: The function to execute.
- format: The format of the function call that returned the error.
- args: The arguments of the function call.
func New ¶
New is a syntactic sugar asserting constructors. It asserts whether the constructor does not return an error and the result is non-nil. If not, it panics with an ErrAssertFailed error.
Parameters:
- res: The result of the constructor.
- inner: The error returned by the constructor.
Example:
type MyStruct struct {}
func (my_struct *MyStruct) IsNil() bool {
return my_struct == nil
}
func NewMyStruct() (*MyStruct, error) {
return nil, nil
}
res := New(NewMyStruct()) // Panics: *MyStruct = nil
func NewErrAssertFailed ¶
NewErrAssertFailed creates a new ErrAssertFailed.
Parameters:
- msg: The message of the error.
Returns:
- error: The new error. Never returns nil.
Format:
(Assertion Failed) "<msg>"
where <msg> is the message. If empty, "something went wrong" is used.
func NewErrFixFailed ¶
NewErrFixFailed creates a new ErrFixFailed.
Parameters:
- name: The name of the variable.
- reason: The message of the error.
Returns:
- error: The new error. Never returns nil.
Format:
(Fix Failed) "<msg>"
where <msg> is the message. If empty, "something went wrong" is used.
func NewErrValidateFailed ¶
NewErrValidateFailed creates a new ErrValidateFailed.
Parameters:
- name: The name of the variable.
- reason: The message of the error.
Returns:
- error: The new error. Never returns nil.
Format:
(Validate Failed) "<name> = <reason>"
where:
- <name> is the name of the variable. If empty, "struct" is used.
- <reason> is the message of the error.
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 Validate ¶
Validate asserts whether the struct's inner state is valid. If not, it panics with an ErrValidationFailed error.
Parameters:
- v: The struct to validate.
- name: The name of the struct. If empty, the name "struct" is used.
- allow_nil: Whether to allow the struct to be nil.
Example:
type MyStruct struct {
Name string
}
func (ms MyStruct) Validate() error {
if ms.Name == "" {
return errors.New("name cannot be empty")
}
return nil
}
ms := &MyStruct{
Name: "",
}
Validate(ms, "ms", false) // Panics: (Validate Failed) ms = name cannot be empty
Types ¶
type ErrAssertFailed ¶
type ErrAssertFailed struct {
// Msg describes what went wrong.
Msg string
}
ErrAssertFailed is an error that is returned when an assertion fails.
func (ErrAssertFailed) Error ¶
func (e ErrAssertFailed) Error() string
Error implements the error interface.
type ErrFixFailed ¶
type ErrFixFailed struct {
// Name is the name of the variable. If empty, "variable" is used.
Name string
// Reason describes what went wrong.
Reason error
}
ErrFixFailed is an error that is returned when a fix fails.
func (ErrFixFailed) Error ¶
func (e ErrFixFailed) Error() string
Error implements the error interface.
type ErrValidateFailed ¶
type ErrValidateFailed struct {
// Name is the name of the variable. If empty, "variable" is used.
Name string
// Reason describes what went wrong.
Reason error
}
ErrValidateFailed is an error that is returned when a validation fails.
func (ErrValidateFailed) Error ¶
func (e ErrValidateFailed) Error() string
Error implements the error interface.
type Fixer ¶
type Fixer interface {
// Fix fixes the struct. Each implementation of Fixer should describe in the
// comments the conditions under which the struct is valid.
//
// Returns:
// - error: An error if the struct could not be fixed. Nil otherwise.
Fix() error
}
Fixer is an interface that defines a method that validates and tries to bring the struct into the closest valid state.
type Validater ¶
type Validater interface {
// Validate validates the struct. Each implementation of Validater should
// describe in the comments the conditions under which the struct is valid.
//
// Returns:
// - error: An error if the struct is invalid. Nil otherwise.
//
// NOTES: This method should not modify the struct's state. For that, use
// Fixer instead.
Validate() error
}
Validater is an interface that defines a method that checks the struct's inner state/integrity.