Skip to content

Commit 806c717

Browse files
committed
feat(either): adding Unpack()
1 parent 32f937a commit 806c717

File tree

11 files changed

+94
-0
lines changed

11 files changed

+94
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ Methods:
196196
- `.Right()` [doc](https://pkg.go.dev/github.com/samber/mo#Either.Right)
197197
- `.MustLeft()` [doc](https://pkg.go.dev/github.com/samber/mo#Either.MustLeft)
198198
- `.MustRight()` [doc](https://pkg.go.dev/github.com/samber/mo#Either.MustRight)
199+
- `.Unpack()` [doc](https://pkg.go.dev/github.com/samber/mo#Either.Unpack)
199200
- `.LeftOrElse()` [doc](https://pkg.go.dev/github.com/samber/mo#Either.LeftOrElse)
200201
- `.RightOrElse()` [doc](https://pkg.go.dev/github.com/samber/mo#Either.RightOrElse)
201202
- `.LeftOrEmpty()` [doc](https://pkg.go.dev/github.com/samber/mo#Either.LeftOrEmpty)
@@ -225,6 +226,7 @@ Methods:
225226
- `.IsArgX()` [doc](https://pkg.go.dev/github.com/samber/mo#Either5.IsArg1)
226227
- `.ArgX()` [doc](https://pkg.go.dev/github.com/samber/mo#Either5.Arg1)
227228
- `.MustArgX()` [doc](https://pkg.go.dev/github.com/samber/mo#Either5.MustArg1)
229+
- `.Unpack()` [doc](https://pkg.go.dev/github.com/samber/mo#Either5.Unpack)
228230
- `.ArgXOrElse()` [doc](https://pkg.go.dev/github.com/samber/mo#Either5.Arg1OrElse)
229231
- `.ArgXOrEmpty()` [doc](https://pkg.go.dev/github.com/samber/mo#Either5.Arg1OrEmpty)
230232
- `.ForEach()` [doc](https://pkg.go.dev/github.com/samber/mo#Either5.ForEach)

either.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ func (e Either[L, R]) MustRight() R {
7575
return e.right
7676
}
7777

78+
// Unpack returns all values
79+
func (e Either[L, R]) Unpack() (L, R) {
80+
return e.left, e.right
81+
}
82+
7883
// LeftOrElse returns left value of a Either struct or fallback.
7984
func (e Either[L, R]) LeftOrElse(fallback L) L {
8085
if e.IsLeft() {

either3.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ func (e Either3[T1, T2, T3]) MustArg3() T3 {
112112
return e.arg3
113113
}
114114

115+
// Unpack returns all values
116+
func (e Either3[T1, T2, T3]) Unpack() (T1, T2, T3) {
117+
return e.arg1, e.arg2, e.arg3
118+
}
119+
115120
// Arg1OrElse returns the first argument of a Either3 struct or fallback.
116121
func (e Either3[T1, T2, T3]) Arg1OrElse(fallback T1) T1 {
117122
if e.IsArg1() {

either3_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,17 @@ func TestEither3MustArg(t *testing.T) {
112112
})
113113
}
114114

115+
func TestEither3Unpack(t *testing.T) {
116+
is := assert.New(t)
117+
118+
either := NewEither3Arg1[int, bool, float64](42)
119+
either3Arg1, either3Arg2, either3Arg3 := either.Unpack()
120+
121+
is.Equal(42, either3Arg1)
122+
is.Equal(false, either3Arg2)
123+
is.Equal(float64(0), either3Arg3)
124+
}
125+
115126
func TestEither3GetOrElse(t *testing.T) {
116127
is := assert.New(t)
117128

either4.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,11 @@ func (e Either4[T1, T2, T3, T4]) MustArg4() T4 {
144144
return e.arg4
145145
}
146146

147+
// Unpack returns all values
148+
func (e Either4[T1, T2, T3, T4]) Unpack() (T1, T2, T3, T4) {
149+
return e.arg1, e.arg2, e.arg3, e.arg4
150+
}
151+
147152
// Arg1OrElse returns the first argument of a Either4 struct or fallback.
148153
func (e Either4[T1, T2, T3, T4]) Arg1OrElse(fallback T1) T1 {
149154
if e.IsArg1() {

either4_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,18 @@ func TestEither4MustArg(t *testing.T) {
165165
})
166166
}
167167

168+
func TestEither4Unpack(t *testing.T) {
169+
is := assert.New(t)
170+
171+
either := NewEither4Arg1[int, bool, float64, string](42)
172+
either4Arg1, either4Arg2, either4Arg3, either4Arg4 := either.Unpack()
173+
174+
is.Equal(42, either4Arg1)
175+
is.Equal(false, either4Arg2)
176+
is.Equal(float64(0), either4Arg3)
177+
is.Equal("", either4Arg4)
178+
}
179+
168180
func TestEither4GetOrElse(t *testing.T) {
169181
is := assert.New(t)
170182

either5.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,11 @@ func (e Either5[T1, T2, T3, T4, T5]) MustArg5() T5 {
176176
return e.arg5
177177
}
178178

179+
// Unpack returns all values
180+
func (e Either5[T1, T2, T3, T4, T5]) Unpack() (T1, T2, T3, T4, T5) {
181+
return e.arg1, e.arg2, e.arg3, e.arg4, e.arg5
182+
}
183+
179184
// Arg1OrElse returns the first argument of a Either5 struct or fallback.
180185
func (e Either5[T1, T2, T3, T4, T5]) Arg1OrElse(fallback T1) T1 {
181186
if e.IsArg1() {

either5_example_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ func ExampleEither5_MustArg1() {
4343
// Output: 42
4444
}
4545

46+
func ExampleEither5_Unpack() {
47+
either5 := NewEither5Arg1[int, bool, float64, string, byte](42)
48+
a, b, c, d, e := either5.Unpack()
49+
50+
fmt.Println(a, b, c, d, e)
51+
// Output: 42 false 0 0
52+
}
53+
4654
func ExampleEither5_Arg1OrElse() {
4755
either5Arg1 := NewEither5Arg1[int, bool, float64, string, byte](42)
4856
result1 := either5Arg1.Arg1OrElse(21)

either5_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,19 @@ func TestEither5MustArg(t *testing.T) {
230230
})
231231
}
232232

233+
func TestEither5Unpack(t *testing.T) {
234+
is := assert.New(t)
235+
236+
either := NewEither5Arg1[int, bool, float64, string, string](42)
237+
either5Arg1, either5Arg2, either5Arg3, either5Arg4, either5Arg5 := either.Unpack()
238+
239+
is.Equal(42, either5Arg1)
240+
is.Equal(false, either5Arg2)
241+
is.Equal(float64(0), either5Arg3)
242+
is.Equal("", either5Arg4)
243+
is.Equal("", either5Arg5)
244+
}
245+
233246
func TestEither5GetOrElse(t *testing.T) {
234247
is := assert.New(t)
235248

either_example_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,22 @@ func ExampleRight() {
2020
// Output: world 42
2121
}
2222

23+
func ExampleEither_Unpack_left() {
24+
either := Left[string, int]("42")
25+
left, right := either.Unpack()
26+
27+
fmt.Println(left, right)
28+
// Output: 42 0
29+
}
30+
31+
func ExampleEither_Unpack_right() {
32+
either := Right[string, int](42)
33+
left, right := either.Unpack()
34+
35+
fmt.Println(left, right)
36+
// Output: 42
37+
}
38+
2339
func ExampleEither_IsLeft_left() {
2440
left := Left[string, int]("hello")
2541
result := left.IsLeft()

0 commit comments

Comments
 (0)