Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add EmptyOnError, EmptyOnError{1->6} #495

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,76 @@ func Must6[T1, T2, T3, T4, T5, T6 any](val1 T1, val2 T2, val3 T3, val4 T4, val5
return val1, val2, val3, val4, val5, val6
}

// just assume fine if err is nil or true.
func fine(err any) bool {
Copy link
Contributor

Choose a reason for hiding this comment

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

I won't use err as a variable name here, as the type is unknown.

I would update the signature of all Just* methods

But it's the way must was written, so …

Copy link
Author

Choose a reason for hiding this comment

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

This is a reasonable suggestion, maybe I can adjust must along with it.

Copy link
Author

Choose a reason for hiding this comment

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

How do you feel about changing err to failure?

Copy link
Contributor

Choose a reason for hiding this comment

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

Naming is a pain.

@samber is better than I am 😬😂

Copy link
Owner

Choose a reason for hiding this comment

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

I have a good friend named ChatGPT. Perfect for brainstorming.

if err == nil {
return true
}

switch e := err.(type) {
case bool:
return e
case error:
return false
default:
return false
}
}

// Just is a helper that wraps a call to a function returning just a value
// and return zero value if err is nil or true.
func Just[T any](val T, err any) T {
if fine(err) {
return val
}
return Empty[T]()
}

// Just1 is an alias to Just
func Just1[T any](val T, err any) T {
return Just(val, err)
}

// Just2 has the same behavior as Just, but callback returns 2 variables.
func Just2[T1, T2 any](val1 T1, val2 T2, err any) (T1, T2) {
if fine(err) {
return val1, val2
}
return Empty[T1](), Empty[T2]()
}

// Just3 has the same behavior as Just, but callback returns 3 variables.
func Just3[T1, T2, T3 any](val1 T1, val2 T2, val3 T3, err any) (T1, T2, T3) {
if fine(err) {
return val1, val2, val3
}
return Empty[T1](), Empty[T2](), Empty[T3]()
}

// Just4 has the same behavior as Just, but callback returns 4 variables.
func Just4[T1, T2, T3, T4 any](val1 T1, val2 T2, val3 T3, val4 T4, err any) (T1, T2, T3, T4) {
if fine(err) {
return val1, val2, val3, val4
}
return Empty[T1](), Empty[T2](), Empty[T3](), Empty[T4]()
}

// Just5 has the same behavior as Just, but callback returns 5 variables.
func Just5[T1, T2, T3, T4, T5 any](val1 T1, val2 T2, val3 T3, val4 T4, val5 T5, err any) (T1, T2, T3, T4, T5) {
if fine(err) {
return val1, val2, val3, val4, val5
}
return Empty[T1](), Empty[T2](), Empty[T3](), Empty[T4](), Empty[T5]()
}

// Just6 has the same behavior as Just, but callback returns 6 variables.
func Just6[T1, T2, T3, T4, T5, T6 any](val1 T1, val2 T2, val3 T3, val4 T4, val5 T5, val6 T6, err any) (T1, T2, T3, T4, T5, T6) {
if fine(err) {
return val1, val2, val3, val4, val5, val6
}
return Empty[T1](), Empty[T2](), Empty[T3](), Empty[T4](), Empty[T5](), Empty[T6]()
}

// Try calls the function and return false in case of error.
func Try(callback func() error) (ok bool) {
ok = true
Expand Down
192 changes: 183 additions & 9 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestMust(t *testing.T) {
is.PanicsWithValue("operation should fail: assert.AnError general error for testing", func() {
Must0(cb(), "operation should fail")
})

is.PanicsWithValue("must: invalid err type 'int', should either be a bool or an error", func() {
Must0(0)
})
Expand Down Expand Up @@ -253,6 +253,180 @@ func TestMustX(t *testing.T) {
}
}

func TestJust(t *testing.T) {
t.Parallel()
is := assert.New(t)

is.Equal("foo", Just("foo", nil))
is.Zero(Just("whatever", errors.New("something went wrong")))

is.Equal(1, Just(1, true))
is.Zero(Just(999, false))
is.Zero(Just(999, errors.New("something went wrong")))

cb := func() (string, error) {
return "whatever", assert.AnError
}
is.Zero(Just(cb()))
}

func TestJustX(t *testing.T) {
t.Parallel()
is := assert.New(t)

{
val1 := Just1(1, nil)
is.Equal(1, val1)
is.Zero(Just1(1, errors.New("something went wrong")))
}

{
val1, val2 := Just2(1, 2, nil)
is.Equal(1, val1)
is.Equal(2, val2)

val1, val2 = Just2(1, 2, errors.New("something went wrong"))
is.Zero(val1)
is.Zero(val2)
}

{
val1, val2, val3 := Just3(1, 2, 3, nil)
is.Equal(1, val1)
is.Equal(2, val2)
is.Equal(3, val3)

val1, val2, val3 = Just3(1, 2, 3, errors.New("something went wrong"))
is.Zero(val1)
is.Zero(val2)
is.Zero(val3)
}

{
val1, val2, val3, val4 := Just4(1, 2, 3, 4, nil)
is.Equal(1, val1)
is.Equal(2, val2)
is.Equal(3, val3)
is.Equal(4, val4)

val1, val2, val3, val4 = Just4(1, 2, 3, 4, errors.New("something went wrong"))
is.Zero(val1)
is.Zero(val2)
is.Zero(val3)
is.Zero(val4)
}

{
val1, val2, val3, val4, val5 := Just5(1, 2, 3, 4, 5, nil)
is.Equal(1, val1)
is.Equal(2, val2)
is.Equal(3, val3)
is.Equal(4, val4)
is.Equal(5, val5)

val1, val2, val3, val4, val5 = Just5(1, 2, 3, 4, 5, errors.New("something went wrong"))
is.Zero(val1)
is.Zero(val2)
is.Zero(val3)
is.Zero(val4)
is.Zero(val5)
}

{
val1, val2, val3, val4, val5, val6 := Just6(1, 2, 3, 4, 5, 6, nil)
is.Equal(1, val1)
is.Equal(2, val2)
is.Equal(3, val3)
is.Equal(4, val4)
is.Equal(5, val5)
is.Equal(6, val6)

val1, val2, val3, val4, val5, val6 = Just6(1, 2, 3, 4, 5, 6, errors.New("something went wrong"))
is.Zero(val1)
is.Zero(val2)
is.Zero(val3)
is.Zero(val4)
is.Zero(val5)
is.Zero(val6)
}

{
val1 := Just(1, true)
is.Equal(1, val1)
is.Zero(Just(1, false))
}

{
val1, val2 := Just2(1, 2, true)
is.Equal(1, val1)
is.Equal(2, val2)

val1, val2 = Just2(1, 2, false)
is.Zero(val1)
is.Zero(val2)
}

{
val1, val2, val3 := Just3(1, 2, 3, true)
is.Equal(1, val1)
is.Equal(2, val2)
is.Equal(3, val3)

val1, val2, val3 = Just3(1, 2, 3, false)
is.Zero(val1)
is.Zero(val2)
is.Zero(val3)
}

{
val1, val2, val3, val4 := Just4(1, 2, 3, 4, true)
is.Equal(1, val1)
is.Equal(2, val2)
is.Equal(3, val3)
is.Equal(4, val4)

val1, val2, val3, val4 = Just4(1, 2, 3, 4, false)
is.Zero(val1)
is.Zero(val2)
is.Zero(val3)
is.Zero(val4)
}

{
val1, val2, val3, val4, val5 := Just5(1, 2, 3, 4, 5, true)
is.Equal(1, val1)
is.Equal(2, val2)
is.Equal(3, val3)
is.Equal(4, val4)
is.Equal(5, val5)

val1, val2, val3, val4, val5 = Just5(1, 2, 3, 4, 5, false)
is.Zero(val1)
is.Zero(val2)
is.Zero(val3)
is.Zero(val4)
is.Zero(val5)
}

{
val1, val2, val3, val4, val5, val6 := Just6(1, 2, 3, 4, 5, 6, true)
is.Equal(1, val1)
is.Equal(2, val2)
is.Equal(3, val3)
is.Equal(4, val4)
is.Equal(5, val5)
is.Equal(6, val6)

val1, val2, val3, val4, val5, val6 = Just6(1, 2, 3, 4, 5, 6, false)
is.Zero(val1)
is.Zero(val2)
is.Zero(val3)
is.Zero(val4)
is.Zero(val5)
is.Zero(val6)
}
}

func TestTry(t *testing.T) {
t.Parallel()
is := assert.New(t)
Expand All @@ -271,11 +445,11 @@ func TestTry(t *testing.T) {
func TestTryX(t *testing.T) {
t.Parallel()
is := assert.New(t)

is.True(Try1(func() error {
return nil
}))

is.True(Try2(func() (string, error) {
return "", nil
}))
Expand All @@ -295,11 +469,11 @@ func TestTryX(t *testing.T) {
is.True(Try6(func() (string, string, string, string, string, error) {
return "", "", "", "", "", nil
}))

is.False(Try1(func() error {
panic("error")
}))

is.False(Try2(func() (string, error) {
panic("error")
}))
Expand All @@ -319,11 +493,11 @@ func TestTryX(t *testing.T) {
is.False(Try6(func() (string, string, string, string, string, error) {
panic("error")
}))

is.False(Try1(func() error {
return errors.New("foo")
}))

is.False(Try2(func() (string, error) {
return "", errors.New("foo")
}))
Expand Down Expand Up @@ -513,13 +687,13 @@ func TestTryWithErrorValue(t *testing.T) {
})
is.False(ok)
is.Equal("error", err)

err, ok = TryWithErrorValue(func() error {
return errors.New("foo")
})
is.False(ok)
is.EqualError(err.(error), "foo")

err, ok = TryWithErrorValue(func() error {
return nil
})
Expand Down
Loading