Documentation
¶
Overview ¶
Example ¶
package main
import (
"fmt"
"github.com/storozhukBM/verifier"
)
type Person struct {
name string
age int32
hasLicense bool
}
func main() {
person := &Person{
name: "John Smith",
age: 42,
hasLicense: false,
}
err := sellAlcohol(person)
if err != nil {
fmt.Print(err)
}
}
func sellAlcohol(p *Person) error {
verify := verifier.New()
verify.That(p != nil, "person can't be nil")
verify.PanicOnError() // use if you don't want to tolerate such errors
verify.That(p.age >= 21, "age should be 21 or higher, but yours: %d", p.age)
verify.That(p.hasLicense, "customer should have license")
if verify.GetError() != nil {
return verify.GetError()
}
fmt.Print("yes, you can have some alcohol")
return nil
}
Output: customer should have license
Index ¶
- func SetUnhandledVerificationsWriter(w io.Writer)
- type Verify
- func (v *Verify) GetError() error
- func (v *Verify) PanicOnError()
- func (v *Verify) Predicate(predicate func() bool, message string, args ...interface{}) *Verify
- func (v *Verify) String() string
- func (v *Verify) That(positiveCondition bool, message string, args ...interface{}) *Verify
- func (v *Verify) WithErrFactory(factory func(string, ...interface{}) error) *Verify
- func (v *Verify) WithError(positiveCondition bool, err error) *Verify
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func SetUnhandledVerificationsWriter ¶
SetUnhandledVerificationsWriter gives you ability to override UnhandledVerificationsWriter (default: os.Stdout).
Types ¶
type Verify ¶
type Verify struct {
// contains filtered or unexported fields
}
Verify represents verification instance. All checks can be performed on it using `That` or `Predicate` functions. After one failed check all others won't count and predicates won't be evaluated. Use Verify.GetError function to check if there where any during verification process.
func New ¶
func New() *Verify
New creates verification instance (recommended). It tracks verification state. If you forget to check internal error, using `GetError` or `PanicOnError` methods, it will write error message to UnhandledVerificationsWriter (default: os.Stdout). This mechanism will help you track down possible unhandled verifications. If you don't wan't to track anything, create zero verifier `Verify{}`.
func Offensive ¶
func Offensive() *Verify
Offensive creates verification instance (not-recommended). It tracks verification state and stops application process when founds unchecked verification. If you forget to check internal error, using `GetError` or `PanicOnError` methods, it will write error message to UnhandledVerificationsWriter (default: os.Stdout) and WILL STOP YOUR PROCESS. Created for people who adopt offensive programming(https://en.wikipedia.org/wiki/Offensive_programming). This mechanism will help you track down possible unhandled verifications. USE IT WISELY.
func (*Verify) GetError ¶
GetError extracts error from internal state to check if there where any during verification process.
func (*Verify) PanicOnError ¶
func (v *Verify) PanicOnError()
PanicOnError panics if there is an error in internal state. Created for people who adopt offensive programming(https://en.wikipedia.org/wiki/Offensive_programming).
func (*Verify) Predicate ¶
That evaluates predicate passed as first argument. If `predicate() == true`, verification will proceed for other checks. If `predicate() == false`, internal state will be filled with error, using message argument as format in error factory func(message, args...) (default: fmt.Errorf). After the first failed verification all others won't count and predicates won't be evaluated.
func (*Verify) That ¶
That verifies condition passed as first argument. If `positiveCondition == true`, verification will proceed for other checks. If `positiveCondition == false`, internal state will be filled with error, using message argument as format in error factory func(message, args...) (default: fmt.Errorf). After the first failed verification all others won't count and predicates won't be evaluated.
func (*Verify) WithErrFactory ¶ added in v1.2.0
WithErrFactory sets error construction function (default: fmt.Errorf). Use it to set custom error type of error, returned by Verify.GetError().
func (*Verify) WithError ¶ added in v1.1.0
WithError verifies condition passed as first argument. If `positiveCondition == true`, verification will proceed for other checks. If `positiveCondition == false`, internal state will be filled with error specified as second argument. After the first failed verification all others won't count and predicates won't be evaluated.