Skip to content

Commit

Permalink
feat(either): adding Unpack()
Browse files Browse the repository at this point in the history
  • Loading branch information
samber committed Dec 9, 2022
1 parent 32f937a commit 806c717
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions either.go
Expand Up @@ -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() {
Expand Down
5 changes: 5 additions & 0 deletions either3.go
Expand Up @@ -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() {
Expand Down
11 changes: 11 additions & 0 deletions either3_test.go
Expand Up @@ -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)

Expand Down
5 changes: 5 additions & 0 deletions either4.go
Expand Up @@ -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() {
Expand Down
12 changes: 12 additions & 0 deletions either4_test.go
Expand Up @@ -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)

Expand Down
5 changes: 5 additions & 0 deletions either5.go
Expand Up @@ -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() {
Expand Down
8 changes: 8 additions & 0 deletions either5_example_test.go
Expand Up @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions either5_test.go
Expand Up @@ -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)

Expand Down
16 changes: 16 additions & 0 deletions either_example_test.go
Expand Up @@ -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()
Expand Down
12 changes: 12 additions & 0 deletions either_test.go
Expand Up @@ -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)

Expand Down

0 comments on commit 806c717

Please sign in to comment.