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

feat(option,result): add a map functionList method #35

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
16 changes: 16 additions & 0 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,22 @@ func (o Option[T]) Map(mapper func(value T) (T, bool)) Option[T] {
return None[T]()
}

func (o Option[T]) Maps(mapperList ...func(value T) (T, bool)) Option[T] {
if !o.isPresent {
return None[T]()
}
tmpValue := o.value
for _, mapper := range mapperList {
var ok bool
tmpValue, ok = mapper(tmpValue)
if !ok {
return None[T]()
}
}

return Some(tmpValue)
}

// MapNone executes the mapper function if value is absent or returns Option.
// Play: https://go.dev/play/p/_KaHWZ6Q17b
func (o Option[T]) MapNone(mapper func() (T, bool)) Option[T] {
Expand Down
28 changes: 28 additions & 0 deletions option_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,34 @@ func ExampleOption_Map_none() {
// Output: false 0
}

func ExampleOption_Maps_some() {
some := Some(42)
result := some.Maps(
func(i int) (int, bool) {
return 1234, true
},
func(value int) (int, bool) {
return value + 1, true
},
)

fmt.Println(result.IsPresent(), result.OrEmpty())
// Output: true 1235
}
func ExampleOption_Maps_none() {
none := None[int]()
result := none.Maps(
func(i int) (int, bool) {
return 1234, true
},
func(value int) (int, bool) {
return value + 1, true
},
)

fmt.Println(result.IsPresent(), result.OrEmpty())
// Output: false 0
}
func ExampleOption_MapNone_some() {
some := Some(42)
result := some.MapNone(func() (int, bool) {
Expand Down
37 changes: 37 additions & 0 deletions option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,43 @@ func TestOptionMap(t *testing.T) {
is.Equal(Option[int]{value: 0, isPresent: false}, opt2)
}

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

mul2 := func(i int) (int, bool) {
return i * 2, true
}
inc1 := func(i int) (int, bool) {
return i + 1, true
}
returnFalse := func(i int) (int, bool) {
return 0, false
}
t.Run("maps 2 func ok ", func(t *testing.T) {
v := Some(21).Maps(mul2, inc1)
is.Equal(Option[int]{value: 43, isPresent: true}, v)
})
t.Run("maps first ok func, second failed func", func(t *testing.T) {
v := Some(21).Maps(mul2, returnFalse)
is.False(v.IsPresent())
})

t.Run("maps 2 failed func", func(t *testing.T) {
v := Some(21).Maps(returnFalse, func(i int) (int, bool) {
is.Fail("should not be called")
return 42, true
})
is.False(v.IsPresent())
})
t.Run("maps none", func(t *testing.T) {
v := None[int]().Maps(mul2, func(i int) (int, bool) {
is.Fail("should not be called")
return 42, true
})
is.False(v.IsPresent())
})
}

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

Expand Down
15 changes: 15 additions & 0 deletions result.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,21 @@ func (r Result[T]) Map(mapper func(value T) (T, error)) Result[T] {

return Err[T](r.err)
}
func (r Result[T]) Maps(mapperList ...func(value T) (T, error)) Result[T] {
if r.isErr {
return Err[T](r.err)
}

tmpValue := r.value
for _, mapper := range mapperList {
var err error
tmpValue, err = mapper(tmpValue)
if err != nil {
return Err[T](err)
}
}
return Ok[T](tmpValue)
}

// MapErr executes the mapper function if Result is invalid. It returns a new Result.
// Play: https://go.dev/play/p/WraZixg9GGf
Expand Down
30 changes: 30 additions & 0 deletions result_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,36 @@ func ExampleResult_Map_err() {
// Output: true 0 error
}

func ExampleResult_Maps_ok() {
ok := Ok(42)
result := ok.Maps(
func(i int) (int, error) {
return i * 2, nil
},
func(i int) (int, error) {
return i + 1, nil
},
)

fmt.Println(result.IsError(), result.OrEmpty(), result.Error())
// Output: false 85 <nil>
}

func ExampleResult_Maps_err() {
ko := Err[int](err)
result := ko.Maps(
func(i int) (int, error) {
return i * 2, nil
},
func(i int) (int, error) {
return i + 1, nil
},
)

fmt.Println(result.IsError(), result.OrEmpty(), result.Error())
// Output: true 0 error
}

func ExampleResult_MapErr_ok() {
ok := Ok(42)
result := ok.MapErr(
Expand Down
36 changes: 36 additions & 0 deletions result_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,42 @@ func TestResultMap(t *testing.T) {
is.Equal(Result[int]{value: 0, isErr: true, err: assert.AnError}, opt2)
}

func TestResultMaps(t *testing.T) {
is := assert.New(t)
mul2 := func(i int) (int, error) {
return i * 2, nil
}
inc1 := func(i int) (int, error) {
return i + 1, nil
}
returnErr := func(i int) (int, error) {
return 0, assert.AnError
}
t.Run("maps 2 func ok ", func(t *testing.T) {
v := Ok(21).Maps(mul2, inc1)
is.Equal(Result[int]{value: 43, isErr: false, err: nil}, v)
})
t.Run("maps first ok func, second failed func", func(t *testing.T) {
v := Ok(21).Maps(mul2, returnErr)
is.Equal(Result[int]{value: 0, isErr: true, err: assert.AnError}, v)
})

t.Run("maps 2 failed func", func(t *testing.T) {
v := Ok(21).Maps(returnErr, func(i int) (int, error) {
is.Fail("should not be called")
return 42, nil
})
is.Equal(Result[int]{value: 0, isErr: true, err: assert.AnError}, v)
})
t.Run("errors maps ", func(t *testing.T) {
v := Err[int](assert.AnError).Maps(mul2, func(i int) (int, error) {
is.Fail("should not be called")
return 42, nil
})
is.Equal(Result[int]{value: 0, isErr: true, err: assert.AnError}, v)
})
}

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

Expand Down