Skip to content

Commit

Permalink
fix: add ReaderIOEither sample
Browse files Browse the repository at this point in the history
Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
  • Loading branch information
CarstenLeue committed Feb 4, 2024
1 parent 2f99ca4 commit 9e04974
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// See the License for the specific language governing permissions and
// 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

import (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// See the License for the specific language governing permissions and
// 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

import (
Expand Down
89 changes: 89 additions & 0 deletions samples/readerioeither/example1/reader_test.go
Original file line number Diff line number Diff line change
@@ -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"}
}

0 comments on commit 9e04974

Please sign in to comment.