Skip to content

Conversation

@abhinav
Copy link
Collaborator

@abhinav abhinav commented Nov 4, 2019

This adds an AppendInto function that behaves similarly to Append
except, it operates on a *error on the left side and it reports
whether the right side error was non-nil.

func AppendInto(*error, error) (errored bool)

Making the left side a pointer aligns with the fast path of Append.

Returning whether the right error was non-nil aligns with the standard
if err := ...; err != nil pattern.

-if err := thing(); err != nil {
+if multierr.AppendInto(&err, thing()) {
   continue
 }

Resolves #21

This adds an AppendInto function that behaves similarly to Append
except, it operates on a `*error` on the left side and it reports
whether the right side error was non-nil.

    func AppendInto(*error, error) (errored bool)

Making the left side a pointer aligns with the fast path of `Append`.

Returning whether the right error was non-nil aligns with the standard
`if err := ...; err != nil` pattern.

```diff
-if err := thing(); err != nil {
+if multierr.AppendInto(&err, thing()) {
   continue
 }
```

Resolves #21
@abhinav abhinav requested review from jcorbin and prashantv November 4, 2019 01:47
@codecov
Copy link

codecov bot commented Nov 4, 2019

Codecov Report

Merging #31 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@          Coverage Diff          @@
##           master    #31   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files           2      2           
  Lines         116    123    +7     
=====================================
+ Hits          116    123    +7
Impacted Files Coverage Δ
error.go 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c3fc3d0...85de79e. Read the comment docs.

// for line := range lines {
// var item Item
// if multierr.AppendInto(&err, parse(line, &item)) {
// continue
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I'd write this in the negative, ala "if-not-append"

Copy link
Collaborator Author

@abhinav abhinav Nov 4, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to keep this 1:1 comparable to the non-AppendInto version for which err != nil would be more standard.

I can add more code to the success path if that would alleviate this concern.

error.go Outdated
// items = append(items, item)
// }
//
// Compare the loop body above with a version that relies solely on Append.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: sentences like this, and above circa L413, are usually "Compare with this:" or "Bla bla bla; for example:"

error.go Outdated
if err == nil {
return false
}
// We will panic if 'into' is nil. This is not documented above
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but what's the failure look like? Would it still be better to add an explicit nil guard here, so that we can write a better panic message?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair. How about a panic with the message "'into' must not be a nil pointer"?

@jcorbin
Copy link

jcorbin commented Nov 4, 2019

The only thing really preventing my from accepting, is perhaps improving the debug-ability of that panic path for when it gets mis-used.

This adds trailing colons to comments preceding examples.
This explicitly checks if into was nil and panics with a more
informative error message than we would get with just `*into`.
Copy link

@jcorbin jcorbin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Copy link
Collaborator

@prashantv prashantv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, I like how small the change ended up being!

@abhinav abhinav merged commit 60a318a into master Nov 4, 2019
@abhinav abhinav deleted the abg/append-into branch November 4, 2019 18:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Wish we could tell if the append happened

4 participants