Skip to content

Commit

Permalink
validate: imp append
Browse files Browse the repository at this point in the history
  • Loading branch information
ainar-g committed Dec 6, 2024
1 parent 31eb218 commit 4b54b5d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
6 changes: 3 additions & 3 deletions netutil/httputil/logmw.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (mw *LogMiddleware) Wrap(h http.Handler) (wrapped http.Handler) {
rw.Reset(w)

l.Log(ctx, mw.lvl, "started")
defer mw.printFinished(ctx, l, rw, startTime)
defer mw.logFinished(ctx, l, rw, startTime)

h.ServeHTTP(rw, nextReq)
rw.SetImplicitSuccess()
Expand All @@ -75,8 +75,8 @@ func (mw *LogMiddleware) Wrap(h http.Handler) (wrapped http.Handler) {
return http.HandlerFunc(f)
}

// printFinished is called at the end of handling of a query.
func (mw *LogMiddleware) printFinished(
// logFinished is called at the end of handling of a query.
func (mw *LogMiddleware) logFinished(
ctx context.Context,
l *slog.Logger,
rw *CodeRecorderResponseWriter,
Expand Down
20 changes: 16 additions & 4 deletions validate/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,21 @@ type Interface interface {
Validate() (err error)
}

// Append validates values, wraps errors with the name and the index, appends
// them to errs, and returns the result.
func Append[T Interface](errs []error, name string, values []T) (res []error) {
// Append validates v and, if it returns an error, appends it to errs and
// returns the result.
func Append(errs []error, name string, v Interface) (res []error) {
res = errs
err := v.Validate()
if err != nil {
res = append(res, fmt.Errorf("%s: %w", name, err))
}

return res
}

// AppendSlice validates values, wraps errors with the name and the index,
// appends them to errs, and returns the result.
func AppendSlice[T Interface](errs []error, name string, values []T) (res []error) {
res = errs
for i, v := range values {
// TODO(a.garipov): Consider flattening error slices.
Expand All @@ -46,7 +58,7 @@ func Append[T Interface](errs []error, name string, values []T) (res []error) {
// Slice validates values, wraps errors with the name and the index, and returns
// the result as a single joined error.
func Slice[T Interface](name string, values []T) (err error) {
return errors.Join(Append(nil, name, values)...)
return errors.Join(AppendSlice(nil, name, values)...)
}

// InRange returns an error of v is less than min or greater than max. The
Expand Down
19 changes: 19 additions & 0 deletions validate/validate_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,25 @@ func (v *value) Validate() (err error) {
return v.err
}

func ExampleAppend() {
var errs []error

var (
badValue = &value{
err: errors.Error("test error"),
}
goodValue = &value{}
)

errs = validate.Append(errs, "first_value", goodValue)
errs = validate.Append(errs, "second_value", badValue)

fmt.Println(errors.Join(errs...))

// Output:
// second_value: test error
}

func ExampleSlice() {
values := []*value{
0: {
Expand Down

0 comments on commit 4b54b5d

Please sign in to comment.