package file

import "github.com/IBM/fp-go/context/readerioeither/file"

Index

Examples

Variables

var (
	// Open opens a file for reading within the given context
	Open = F.Flow3(
		IOEF.Open,
		RIOE.FromIOEither[*os.File],
		RIOE.WithContext[*os.File],
	)

	// Remove removes a file by name
	Remove = F.Flow2(
		IOEF.Remove,
		RIOE.FromIOEither[string],
	)
)

Functions

func Close

func Close[C io.Closer](c C) RIOE.ReaderIOEither[any]

Close closes an object

func CreateTemp

func CreateTemp(dir, pattern string) RIOE.ReaderIOEither[*os.File]

CreateTemp created a temp file with proper parametrization

func ReadFile

func ReadFile(path string) RIOE.ReaderIOEither[[]byte]

ReadFile reads a file in the scope of a context

Example

Code:play 

package main

import (
	"context"
	"fmt"

	R "github.com/IBM/fp-go/context/readerioeither"
	F "github.com/IBM/fp-go/function"
	IO "github.com/IBM/fp-go/io"
	J "github.com/IBM/fp-go/json"
)

type RecordType struct {
	Data string `json:"data"`
}

func getData(r RecordType) string {
	return r.Data
}

func main() {

	data := F.Pipe3(
		ReadFile("./data/file.json"),
		R.ChainEitherK(J.Unmarshal[RecordType]),
		R.ChainFirstIOK(IO.Logf[RecordType]("Log: %v")),
		R.Map(getData),
	)

	result := data(context.Background())

	fmt.Println(result())

}

Output:

Right[string](Carsten)

func WithTempFile

func WithTempFile[A any](f func(*os.File) RIOE.ReaderIOEither[A]) RIOE.ReaderIOEither[A]

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 Write

func Write[R any, W io.WriteCloser](acquire RIOE.ReaderIOEither[W]) func(use func(W) RIOE.ReaderIOEither[R]) RIOE.ReaderIOEither[R]

Write uses a generator function to create a stream, writes data to it and closes it

func WriteAll

func WriteAll[W io.WriteCloser](data []byte) func(acquire RIOE.ReaderIOEither[W]) RIOE.ReaderIOEither[[]byte]

WriteAll uses a generator function to create a stream, writes data to it and closes it

Source Files

file.go tempfile.go write.go

Version
v1.0.151 (latest)
Published
Nov 23, 2024
Platform
linux/amd64
Imports
11 packages
Last checked
5 months ago

Tools for package owners.