1
0
mirror of https://github.com/IBM/fp-go.git synced 2025-07-17 01:32:23 +02:00

fix: add WithTempFile to ReaderIOEither

Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
This commit is contained in:
Dr. Carsten Leue
2023-09-19 18:06:32 +02:00
parent 6532a83e82
commit b15ab38861
6 changed files with 95 additions and 2 deletions

View File

@ -25,6 +25,7 @@ import (
F "github.com/IBM/fp-go/function"
"github.com/IBM/fp-go/internal/file"
IOE "github.com/IBM/fp-go/ioeither"
IOEF "github.com/IBM/fp-go/ioeither/file"
)
var (
@ -35,6 +36,12 @@ var (
RIOE.FromIOEither[*os.File],
RIOE.WithContext[*os.File],
)
// Remove removes a file by name
Remove = F.Flow2(
IOEF.Remove,
RIOE.FromIOEither[string],
)
)
// Close closes an object

View File

@ -0,0 +1,53 @@
// 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 file
import (
"os"
RIOE "github.com/IBM/fp-go/context/readerioeither"
FL "github.com/IBM/fp-go/file"
F "github.com/IBM/fp-go/function"
IO "github.com/IBM/fp-go/io"
IOF "github.com/IBM/fp-go/io/file"
IOEF "github.com/IBM/fp-go/ioeither/file"
)
var (
// onCreateTempFile creates a temp file with sensible defaults
onCreateTempFile = CreateTemp("", "*")
// destroy handler
onReleaseTempFile = F.Flow4(
IOF.Close[*os.File],
IO.Map(FL.GetName),
RIOE.FromIO[string],
RIOE.Chain(Remove),
)
)
// CreateTemp created a temp file with proper parametrization
func CreateTemp(dir, pattern string) RIOE.ReaderIOEither[*os.File] {
return F.Pipe2(
IOEF.CreateTemp(dir, pattern),
RIOE.FromIOEither[*os.File],
RIOE.WithContext[*os.File],
)
}
// WithTempFile creates a temporary file, then invokes a callback to create a resource based on the file, then close and remove the temp file
func WithTempFile[A any](f func(*os.File) RIOE.ReaderIOEither[A]) RIOE.ReaderIOEither[A] {
return RIOE.WithResource[A](onCreateTempFile, onReleaseTempFile)(f)
}

View File

@ -100,6 +100,28 @@ func Map[
return RIE.Map[GRA, GRB](f)
}
func MonadMapTo[
GRA ~func(context.Context) GIOA,
GRB ~func(context.Context) GIOB,
GIOA ~func() E.Either[error, A],
GIOB ~func() E.Either[error, B],
A, B any](fa GRA, b B) GRB {
return RIE.MonadMapTo[GRA, GRB](fa, b)
}
func MapTo[
GRA ~func(context.Context) GIOA,
GRB ~func(context.Context) GIOB,
GIOA ~func() E.Either[error, A],
GIOB ~func() E.Either[error, B],
A, B any](b B) func(GRA) GRB {
return RIE.MapTo[GRA, GRB](b)
}
func MonadChain[
GRA ~func(context.Context) GIOA,
GRB ~func(context.Context) GIOB,

View File

@ -61,6 +61,14 @@ func Map[A, B any](f func(A) B) func(ReaderIOEither[A]) ReaderIOEither[B] {
return G.Map[ReaderIOEither[A], ReaderIOEither[B]](f)
}
func MonadMapTo[A, B any](fa ReaderIOEither[A], b B) ReaderIOEither[B] {
return G.MonadMapTo[ReaderIOEither[A], ReaderIOEither[B]](fa, b)
}
func MapTo[A, B any](b B) func(ReaderIOEither[A]) ReaderIOEither[B] {
return G.MapTo[ReaderIOEither[A], ReaderIOEither[B]](b)
}
func MonadChain[A, B any](ma ReaderIOEither[A], f func(A) ReaderIOEither[B]) ReaderIOEither[B] {
return G.MonadChain(ma, f)
}

View File

@ -27,7 +27,7 @@ import (
var (
// Command executes a command
// use this version if the command does not produce any side effect, i.e. if the output is uniquely determined by by the input
// typically you'd rather use the IOEither version of the command
// typically you'd rather use the [IOEither] version of the command
Command = F.Curry3(command)
)

View File

@ -20,11 +20,14 @@ import (
)
type (
// command output
// CommandOutput represents the output of executing a command. The first field in the [Tuple2] is
// stdout, the second one is stderr. Use [StdOut] and [StdErr] to access these fields
CommandOutput = T.Tuple2[[]byte, []byte]
)
var (
// StdOut returns the field of a [CommandOutput] representing `stdout`
StdOut = T.First[[]byte, []byte]
// StdErr returns the field of a [CommandOutput] representing `stderr`
StdErr = T.Second[[]byte, []byte]
)