-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #49 from IBM/cleue-add-ioeither-sample-with-return…
…-tuple fix: add missing IOO.FromIOEither
- Loading branch information
Showing
6 changed files
with
234 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright (c) 2023 IBM Corp. | ||
// All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package tuples | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
F "github.com/IBM/fp-go/function" | ||
IOE "github.com/IBM/fp-go/ioeither" | ||
IOEF "github.com/IBM/fp-go/ioeither/file" | ||
IOEG "github.com/IBM/fp-go/ioeither/generic" | ||
IOO "github.com/IBM/fp-go/iooption" | ||
) | ||
|
||
func TestIOEitherToOption1(t *testing.T) { | ||
tmpDir := t.TempDir() | ||
content := []byte("abc") | ||
|
||
resIOO := F.Pipe2( | ||
content, | ||
IOEF.WriteFile(filepath.Join(tmpDir, "test.txt"), os.ModePerm), | ||
IOEG.Fold[IOE.IOEither[error, []byte]]( | ||
IOO.Of[error], | ||
F.Ignore1of1[[]byte](IOO.None[error]), | ||
), | ||
) | ||
|
||
fmt.Println(resIOO()) | ||
} | ||
|
||
func TestIOEitherToOption2(t *testing.T) { | ||
tmpDir := t.TempDir() | ||
content := []byte("abc") | ||
|
||
resIOO := F.Pipe3( | ||
content, | ||
IOEF.WriteFile(filepath.Join(tmpDir, "test.txt"), os.ModePerm), | ||
IOE.Swap[error, []byte], | ||
IOO.FromIOEither[[]byte, error], | ||
) | ||
|
||
fmt.Println(resIOO()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
abc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
// Copyright (c) 2023 IBM Corp. | ||
// All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package tuples | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"strings" | ||
"testing" | ||
|
||
E "github.com/IBM/fp-go/either" | ||
F "github.com/IBM/fp-go/function" | ||
IOE "github.com/IBM/fp-go/ioeither" | ||
IOEF "github.com/IBM/fp-go/ioeither/file" | ||
T "github.com/IBM/fp-go/tuple" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func sampleConvertDocx(r io.Reader) (string, map[string]string, error) { | ||
content, err := io.ReadAll(r) | ||
return string(content), map[string]string{}, err | ||
} | ||
|
||
func TestSampleConvertDocx1(t *testing.T) { | ||
// this conversion approach has the disadvantage that it exhausts the reader | ||
// so we cannot invoke the resulting IOEither multiple times | ||
convertDocx := func(r io.Reader) IOE.IOEither[error, T.Tuple2[string, map[string]string]] { | ||
return IOE.TryCatchError(func() (T.Tuple2[string, map[string]string], error) { | ||
text, meta, err := sampleConvertDocx(r) | ||
return T.MakeTuple2(text, meta), err | ||
}) | ||
} | ||
|
||
rdr := strings.NewReader("abc") | ||
resIOE := convertDocx(rdr) | ||
|
||
resE := resIOE() | ||
|
||
assert.True(t, E.IsRight(resE)) | ||
} | ||
|
||
func TestSampleConvertDocx2(t *testing.T) { | ||
// this approach assumes that `sampleConvertDocx` does not have any side effects | ||
// other than reading from a `Reader`. As a consequence it can be a pure function itself. | ||
// The disadvantage is that its input has to exist in memory which is probably not a good | ||
// idea for large inputs | ||
convertDocx := func(data []byte) E.Either[error, T.Tuple2[string, map[string]string]] { | ||
return E.TryCatchError(func() (T.Tuple2[string, map[string]string], error) { | ||
text, meta, err := sampleConvertDocx(bytes.NewReader(data)) | ||
return T.MakeTuple2(text, meta), err | ||
}) | ||
} | ||
|
||
resE := convertDocx([]byte("abc")) | ||
|
||
assert.True(t, E.IsRight(resE)) | ||
} | ||
|
||
// onClose closes a closeable resource | ||
func onClose[R io.Closer](r R) IOE.IOEither[error, R] { | ||
return IOE.TryCatchError(func() (R, error) { | ||
return r, r.Close() | ||
}) | ||
} | ||
|
||
// convertDocx3 takes an `acquire` function that creates an instance or a [ReaderCloser] whenever the resulting [IOEither] is invoked. Since | ||
// we return a [Closer] the instance will be closed after use, automatically. This design makes sure that the resulting [IOEither] can be invoked | ||
// as many times as necessary | ||
func convertDocx3[R io.ReadCloser](acquire IOE.IOEither[error, R]) IOE.IOEither[error, T.Tuple2[string, map[string]string]] { | ||
return IOE.WithResource[T.Tuple2[string, map[string]string]]( | ||
acquire, | ||
onClose[R])( | ||
func(r R) IOE.IOEither[error, T.Tuple2[string, map[string]string]] { | ||
return IOE.TryCatchError(func() (T.Tuple2[string, map[string]string], error) { | ||
text, meta, err := sampleConvertDocx(r) | ||
return T.MakeTuple2(text, meta), err | ||
}) | ||
}, | ||
) | ||
} | ||
|
||
// convertDocx4 takes an `acquire` function that creates an instance or a [Reader] whenever the resulting [IOEither] is invoked. | ||
// This design makes sure that the resulting [IOEither] can be invoked | ||
// as many times as necessary | ||
func convertDocx4[R io.Reader](acquire IOE.IOEither[error, R]) IOE.IOEither[error, T.Tuple2[string, map[string]string]] { | ||
return F.Pipe1( | ||
acquire, | ||
IOE.Chain(func(r R) IOE.IOEither[error, T.Tuple2[string, map[string]string]] { | ||
return IOE.TryCatchError(func() (T.Tuple2[string, map[string]string], error) { | ||
text, meta, err := sampleConvertDocx(r) | ||
return T.MakeTuple2(text, meta), err | ||
}) | ||
}), | ||
) | ||
} | ||
|
||
func TestSampleConvertDocx3(t *testing.T) { | ||
// IOEither that creates the reader | ||
acquire := IOEF.Open("./samples/data.txt") | ||
|
||
resIOE := convertDocx3(acquire) | ||
resE := resIOE() | ||
|
||
assert.True(t, E.IsRight(resE)) | ||
} | ||
|
||
func TestSampleConvertDocx4(t *testing.T) { | ||
// IOEither that creates the reader | ||
acquire := IOE.FromIO[error](func() *strings.Reader { | ||
return strings.NewReader("abc") | ||
}) | ||
|
||
resIOE := convertDocx4(acquire) | ||
resE := resIOE() | ||
|
||
assert.True(t, E.IsRight(resE)) | ||
} |