From 806c71727cdd33ea815b70b12fd6bd70e38deb8e Mon Sep 17 00:00:00 2001 From: Samuel Berthe Date: Fri, 9 Dec 2022 17:52:35 +0100 Subject: [PATCH] feat(either): adding Unpack() --- README.md | 2 ++ either.go | 5 +++++ either3.go | 5 +++++ either3_test.go | 11 +++++++++++ either4.go | 5 +++++ either4_test.go | 12 ++++++++++++ either5.go | 5 +++++ either5_example_test.go | 8 ++++++++ either5_test.go | 13 +++++++++++++ either_example_test.go | 16 ++++++++++++++++ either_test.go | 12 ++++++++++++ 11 files changed, 94 insertions(+) diff --git a/README.md b/README.md index 9fc66a5..6875ca2 100644 --- a/README.md +++ b/README.md @@ -196,6 +196,7 @@ Methods: - `.Right()` [doc](https://pkg.go.dev/github.com/samber/mo#Either.Right) - `.MustLeft()` [doc](https://pkg.go.dev/github.com/samber/mo#Either.MustLeft) - `.MustRight()` [doc](https://pkg.go.dev/github.com/samber/mo#Either.MustRight) +- `.Unpack()` [doc](https://pkg.go.dev/github.com/samber/mo#Either.Unpack) - `.LeftOrElse()` [doc](https://pkg.go.dev/github.com/samber/mo#Either.LeftOrElse) - `.RightOrElse()` [doc](https://pkg.go.dev/github.com/samber/mo#Either.RightOrElse) - `.LeftOrEmpty()` [doc](https://pkg.go.dev/github.com/samber/mo#Either.LeftOrEmpty) @@ -225,6 +226,7 @@ Methods: - `.IsArgX()` [doc](https://pkg.go.dev/github.com/samber/mo#Either5.IsArg1) - `.ArgX()` [doc](https://pkg.go.dev/github.com/samber/mo#Either5.Arg1) - `.MustArgX()` [doc](https://pkg.go.dev/github.com/samber/mo#Either5.MustArg1) +- `.Unpack()` [doc](https://pkg.go.dev/github.com/samber/mo#Either5.Unpack) - `.ArgXOrElse()` [doc](https://pkg.go.dev/github.com/samber/mo#Either5.Arg1OrElse) - `.ArgXOrEmpty()` [doc](https://pkg.go.dev/github.com/samber/mo#Either5.Arg1OrEmpty) - `.ForEach()` [doc](https://pkg.go.dev/github.com/samber/mo#Either5.ForEach) diff --git a/either.go b/either.go index f3aadeb..8fa6987 100644 --- a/either.go +++ b/either.go @@ -75,6 +75,11 @@ func (e Either[L, R]) MustRight() R { return e.right } +// Unpack returns all values +func (e Either[L, R]) Unpack() (L, R) { + return e.left, e.right +} + // LeftOrElse returns left value of a Either struct or fallback. func (e Either[L, R]) LeftOrElse(fallback L) L { if e.IsLeft() { diff --git a/either3.go b/either3.go index 6660a96..131b8ba 100644 --- a/either3.go +++ b/either3.go @@ -112,6 +112,11 @@ func (e Either3[T1, T2, T3]) MustArg3() T3 { return e.arg3 } +// Unpack returns all values +func (e Either3[T1, T2, T3]) Unpack() (T1, T2, T3) { + return e.arg1, e.arg2, e.arg3 +} + // Arg1OrElse returns the first argument of a Either3 struct or fallback. func (e Either3[T1, T2, T3]) Arg1OrElse(fallback T1) T1 { if e.IsArg1() { diff --git a/either3_test.go b/either3_test.go index 517e053..10b84e1 100644 --- a/either3_test.go +++ b/either3_test.go @@ -112,6 +112,17 @@ func TestEither3MustArg(t *testing.T) { }) } +func TestEither3Unpack(t *testing.T) { + is := assert.New(t) + + either := NewEither3Arg1[int, bool, float64](42) + either3Arg1, either3Arg2, either3Arg3 := either.Unpack() + + is.Equal(42, either3Arg1) + is.Equal(false, either3Arg2) + is.Equal(float64(0), either3Arg3) +} + func TestEither3GetOrElse(t *testing.T) { is := assert.New(t) diff --git a/either4.go b/either4.go index fb4cf81..16e207a 100644 --- a/either4.go +++ b/either4.go @@ -144,6 +144,11 @@ func (e Either4[T1, T2, T3, T4]) MustArg4() T4 { return e.arg4 } +// Unpack returns all values +func (e Either4[T1, T2, T3, T4]) Unpack() (T1, T2, T3, T4) { + return e.arg1, e.arg2, e.arg3, e.arg4 +} + // Arg1OrElse returns the first argument of a Either4 struct or fallback. func (e Either4[T1, T2, T3, T4]) Arg1OrElse(fallback T1) T1 { if e.IsArg1() { diff --git a/either4_test.go b/either4_test.go index 21a1dc6..4d5726b 100644 --- a/either4_test.go +++ b/either4_test.go @@ -165,6 +165,18 @@ func TestEither4MustArg(t *testing.T) { }) } +func TestEither4Unpack(t *testing.T) { + is := assert.New(t) + + either := NewEither4Arg1[int, bool, float64, string](42) + either4Arg1, either4Arg2, either4Arg3, either4Arg4 := either.Unpack() + + is.Equal(42, either4Arg1) + is.Equal(false, either4Arg2) + is.Equal(float64(0), either4Arg3) + is.Equal("", either4Arg4) +} + func TestEither4GetOrElse(t *testing.T) { is := assert.New(t) diff --git a/either5.go b/either5.go index 1eadc07..bc8e3c5 100644 --- a/either5.go +++ b/either5.go @@ -176,6 +176,11 @@ func (e Either5[T1, T2, T3, T4, T5]) MustArg5() T5 { return e.arg5 } +// Unpack returns all values +func (e Either5[T1, T2, T3, T4, T5]) Unpack() (T1, T2, T3, T4, T5) { + return e.arg1, e.arg2, e.arg3, e.arg4, e.arg5 +} + // Arg1OrElse returns the first argument of a Either5 struct or fallback. func (e Either5[T1, T2, T3, T4, T5]) Arg1OrElse(fallback T1) T1 { if e.IsArg1() { diff --git a/either5_example_test.go b/either5_example_test.go index 98de76e..53fe20f 100644 --- a/either5_example_test.go +++ b/either5_example_test.go @@ -43,6 +43,14 @@ func ExampleEither5_MustArg1() { // Output: 42 } +func ExampleEither5_Unpack() { + either5 := NewEither5Arg1[int, bool, float64, string, byte](42) + a, b, c, d, e := either5.Unpack() + + fmt.Println(a, b, c, d, e) + // Output: 42 false 0 0 +} + func ExampleEither5_Arg1OrElse() { either5Arg1 := NewEither5Arg1[int, bool, float64, string, byte](42) result1 := either5Arg1.Arg1OrElse(21) diff --git a/either5_test.go b/either5_test.go index 52dea39..754f141 100644 --- a/either5_test.go +++ b/either5_test.go @@ -230,6 +230,19 @@ func TestEither5MustArg(t *testing.T) { }) } +func TestEither5Unpack(t *testing.T) { + is := assert.New(t) + + either := NewEither5Arg1[int, bool, float64, string, string](42) + either5Arg1, either5Arg2, either5Arg3, either5Arg4, either5Arg5 := either.Unpack() + + is.Equal(42, either5Arg1) + is.Equal(false, either5Arg2) + is.Equal(float64(0), either5Arg3) + is.Equal("", either5Arg4) + is.Equal("", either5Arg5) +} + func TestEither5GetOrElse(t *testing.T) { is := assert.New(t) diff --git a/either_example_test.go b/either_example_test.go index 122f5ce..ad8fd59 100644 --- a/either_example_test.go +++ b/either_example_test.go @@ -20,6 +20,22 @@ func ExampleRight() { // Output: world 42 } +func ExampleEither_Unpack_left() { + either := Left[string, int]("42") + left, right := either.Unpack() + + fmt.Println(left, right) + // Output: 42 0 +} + +func ExampleEither_Unpack_right() { + either := Right[string, int](42) + left, right := either.Unpack() + + fmt.Println(left, right) + // Output: 42 +} + func ExampleEither_IsLeft_left() { left := Left[string, int]("hello") result := left.IsLeft() diff --git a/either_test.go b/either_test.go index 5f324dc..e09f7f0 100644 --- a/either_test.go +++ b/either_test.go @@ -20,6 +20,18 @@ func TestEitherRight(t *testing.T) { is.Equal(Either[int, bool]{left: 0, right: true, isLeft: false}, right) } +func TestEitherUnpack(t *testing.T) { + is := assert.New(t) + + left1, right1 := Left[int, bool](42).Unpack() + left2, right2 := Right[int, bool](true).Unpack() + + is.Equal(42, left1) + is.Equal(false, right1) + is.Equal(0, left2) + is.Equal(true, right2) +} + func TestEitherIsLeftOrRight(t *testing.T) { is := assert.New(t)