mirror of
https://github.com/IBM/fp-go.git
synced 2025-08-10 22:31:32 +02:00
fix: add ReaderIOEither sample
Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
This commit is contained in:
@@ -13,7 +13,7 @@
|
|||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
// Package reader implements the example in [https://dev.to/gcanti/getting-started-with-fp-ts-reader-1ie5]
|
// Package example1 implements the first example in [https://dev.to/gcanti/getting-started-with-fp-ts-reader-1ie5]
|
||||||
package example1
|
package example1
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
@@ -13,7 +13,7 @@
|
|||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
// Package reader implements the example in [https://dev.to/gcanti/getting-started-with-fp-ts-reader-1ie5]
|
// Package example2 implements the second example in [https://dev.to/gcanti/getting-started-with-fp-ts-reader-1ie5]
|
||||||
package example2
|
package example2
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
89
samples/readerioeither/example1/reader_test.go
Normal file
89
samples/readerioeither/example1/reader_test.go
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
// 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 example1
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
|
||||||
|
B "github.com/IBM/fp-go/bytes"
|
||||||
|
E "github.com/IBM/fp-go/either"
|
||||||
|
F "github.com/IBM/fp-go/function"
|
||||||
|
I "github.com/IBM/fp-go/identity"
|
||||||
|
IOE "github.com/IBM/fp-go/ioeither"
|
||||||
|
J "github.com/IBM/fp-go/json"
|
||||||
|
RIOE "github.com/IBM/fp-go/readerioeither"
|
||||||
|
)
|
||||||
|
|
||||||
|
type (
|
||||||
|
WriterType = func([]byte) IOE.IOEither[error, []byte]
|
||||||
|
|
||||||
|
Dependencies struct {
|
||||||
|
Writer WriterType
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
func getWriter(deps *Dependencies) WriterType {
|
||||||
|
return deps.Writer
|
||||||
|
}
|
||||||
|
|
||||||
|
// SerializeToWriter marshals the input to JSON and persists the result via the [Writer] passed in via the [*Dependencies]
|
||||||
|
func SerializeToWriter[A any](data A) RIOE.ReaderIOEither[*Dependencies, error, []byte] {
|
||||||
|
return F.Pipe1(
|
||||||
|
RIOE.Ask[*Dependencies, error](),
|
||||||
|
RIOE.ChainIOEitherK[*Dependencies](F.Flow2(
|
||||||
|
getWriter,
|
||||||
|
F.Pipe2(
|
||||||
|
data,
|
||||||
|
J.Marshal[A],
|
||||||
|
E.Fold(F.Flow2(
|
||||||
|
IOE.Left[[]byte, error],
|
||||||
|
F.Constant1[WriterType, IOE.IOEither[error, []byte]],
|
||||||
|
), I.Ap[IOE.IOEither[error, []byte], []byte]),
|
||||||
|
),
|
||||||
|
)),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleReaderIOEither() {
|
||||||
|
|
||||||
|
// writeToStdOut implements a writer to stdout
|
||||||
|
writeToStdOut := func(data []byte) IOE.IOEither[error, []byte] {
|
||||||
|
return IOE.TryCatchError(func() ([]byte, error) {
|
||||||
|
_, err := os.Stdout.Write(data)
|
||||||
|
return data, err
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
deps := Dependencies{
|
||||||
|
Writer: writeToStdOut,
|
||||||
|
}
|
||||||
|
|
||||||
|
data := map[string]string{
|
||||||
|
"a": "b",
|
||||||
|
"c": "d",
|
||||||
|
}
|
||||||
|
|
||||||
|
// writeData will persist to a configurable target
|
||||||
|
writeData := F.Pipe1(
|
||||||
|
SerializeToWriter(data),
|
||||||
|
RIOE.Map[*Dependencies, error](B.ToString),
|
||||||
|
)
|
||||||
|
|
||||||
|
_ = writeData(&deps)()
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// {"a":"b","c":"d"}
|
||||||
|
}
|
Reference in New Issue
Block a user