Skip to content

Commit

Permalink
chore: reformat comments
Browse files Browse the repository at this point in the history
  • Loading branch information
tmzane committed Oct 11, 2023
1 parent 5d106d8 commit 83ac230
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 36 deletions.
23 changes: 4 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,8 @@ The last call in the chain must be either `FirstError` or `AllErrors`.

```go
errs := check.
That(user.Name != "", errEmptyName).
Thatf(user.Age >= 18, "%d y.o. is too young", user.Age).
Thatf(isEmail(user.Email), "%s is invalid email", user.Email).
AllErrors() // OR FirstError() to check only the first error
```

The same code without `check`:

```go
var errs []error
if user.Name == "" {
errs = append(errs, errEmptyName)
}
if user.Age < 18 {
errs = append(errs, fmt.Errorf("%d y.o. is too young", user.Age))
}
if !isEmail(user.Email) {
errs = append(errs, fmt.Errorf("%s is invalid email", user.Email))
}
That(user.Name != "", errEmptyName).
Thatf(user.Age >= 18, "%d y.o. is too young", user.Age).
Thatf(isEmail(user.Email), "%s is invalid email", user.Email).
AllErrors() // OR FirstError() to check only the first error.
```
21 changes: 8 additions & 13 deletions check.go
Original file line number Diff line number Diff line change
@@ -1,44 +1,39 @@
// Package check provides convenience helpers to perform validations of any
// kind.
// Package check provides convenience helpers to perform validations of any kind.
//
// Use That/Thatf to write conditions to check, multiple calls can be chained.
// The last call in the chain must be either FirstError or AllErrors.
package check

import "fmt"

// That checks whether cond is true, and if not, records the error. That panics
// if the error is nil.
// That checks whether the condition is true, and if not, records the error.
func That(cond bool, err error) *State {
return new(State).That(cond, err)
}

// Thatf checks whether cond is true, and if not, creates an error from format
// and args, then records it.
// Thatf checks whether the condition is true, and if not, creates an error from format and args, then records it.
func Thatf(cond bool, format string, args ...any) *State {
return new(State).Thatf(cond, format, args...)
}

// State holds the errors of the failed conditions. It is exported only for the
// purpose of documentation, do not use it directly.
// State holds the errors of the failed conditions.
// It is exported only for the purpose of documentation.
type State struct {
errs []error
}

// That checks whether cond is true, and if not, records the error. That panics
// if the error is nil.
// That checks whether the condition is true, and if not, records the error.
func (s *State) That(cond bool, err error) *State {
if err == nil {
panic("check: a nil error is provided")
panic("check: a nil error provided")
}
if !cond {
s.errs = append(s.errs, err)
}
return s
}

// Thatf checks whether cond is true, and if not, creates an error from format
// and args, then records it.
// Thatf checks whether the condition is true, and if not, creates an error from format and args, then records it.
func (s *State) Thatf(cond bool, format string, args ...any) *State {
return s.That(cond, fmt.Errorf(format, args...))
}
Expand Down
4 changes: 2 additions & 2 deletions check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestCheck(t *testing.T) {
}

if errs[0] == nil || errs[1] == nil {
t.Errorf("want all errors to be not nil")
t.Errorf("want all errors to be non-nil")
}
})

Expand All @@ -47,7 +47,7 @@ func TestCheck(t *testing.T) {
check.That(false, nil)
})

//nolint:gofumpt // empty line before `if err != nil` is ok here
//nolint:gofumpt // empty line before `if err != nil` is ok here.
t.Run("all conditions are true", func(t *testing.T) {
err := check.
Thatf(true, "not an error").
Expand Down
4 changes: 2 additions & 2 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ func isEmail(string) bool { return false }

var errEmptyName = errors.New("name must not be empty")

func ExampleThat() {
func Example() {
errs := check.
That(user.Name != "", errEmptyName).
Thatf(user.Age >= 18, "%d y.o. is too young", user.Age).
Thatf(isEmail(user.Email), "%s is invalid email", user.Email).
AllErrors() // OR FirstError() to check only the first error
AllErrors() // OR FirstError() to check only the first error.

for _, err := range errs {
fmt.Println(err)
Expand Down

0 comments on commit 83ac230

Please sign in to comment.