gio

package module
v0.0.0-...-28cf358 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 12, 2024 License: MIT Imports: 2 Imported by: 0

README

gio

  • gio: Generic-aware io interfaces like gio.Reader[int], generic-aware in-memory pipe gio.Pipe[string]. Forked from Go stdlib.
  • gio/pipe: Generic-aware pipeline with a standard input output streams and filters. Enable writing unix-like utilities working on top of native Go types.
  • gio/unix: byte stream aware pipeline with a standard input output streams and filters. Works like traditional unix tools.

Example

An equivalent of cat | wc -l using a native Go types and channels under the hood.

	out := &StringBuffer{}
	stdio := NewStdio[string](
		nil,
		out,
		os.Stderr,
	)

	err := pipe.NewLine[string]().Run(ctx, stdio, cat, wc)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(out.String())
	// Output: 3

os/exec wrapper

gio/unix has a *exec.Cmd wrapper allowing to run any system command as a Filter

	stdout := bytes.NewBuffer(nil)
	stdio := unix.NewStdio(
		nil,
		stdout,
		os.Stderr,
	)

	cmd := unix.NewCmd(exec.Command("go", "version"))
	err := cmd.Run(ctx, stdio)
	fmt.Println(out.String())
	// Output: go version 1.20 linux/amd64

TODO

  • explore the Transform[F, T any] option allowing type conversion

Documentation

Overview

Package gio - generic io - is forked variant of Go stdlib io module. Code is extended to be able to work on a slice of Go any type.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Pipe

func Pipe[T any]() (*PipeReader[T], *PipeWriter[T])

Pipe creates a synchronous in-memory pipe. It can be used to connect code expecting an io.Reader with code expecting an io.Writer.

Reads and Writes on the pipe are matched one to one except when multiple Reads are needed to consume a single Write. That is, each Write to the PipeWriter blocks until it has satisfied one or more Reads from the PipeReader that fully consume the written data. The data is copied directly from the Write to the corresponding Read (or Reads); there is no internal buffering.

It is safe to call Read and Write in parallel with each other or with Close. Parallel calls to Read and parallel calls to Write are also safe: the individual calls will be gated sequentially.

Types

type Closer

type Closer interface {
	Close() error
}

Closer is the interface that wraps the basic Close method.

The behavior of Close after the first call is undefined. Specific implementations may document their own behavior.

type PipeReader

type PipeReader[T any] struct {
	// contains filtered or unexported fields
}

A PipeReader is the read half of a pipe.

func (*PipeReader[T]) Close

func (r *PipeReader[T]) Close() error

Close closes the reader; subsequent writes to the write half of the pipe will return the error ErrClosedPipe.

func (*PipeReader[T]) CloseWithError

func (r *PipeReader[T]) CloseWithError(err error) error

CloseWithError closes the reader; subsequent writes to the write half of the pipe will return the error err.

CloseWithError never overwrites the previous error if it exists and always returns nil.

func (*PipeReader[T]) Read

func (r *PipeReader[T]) Read(data []T) (n int, err error)

Read implements the standard Read interface: it reads data from the pipe, blocking until a writer arrives or the write end is closed. If the write end is closed with an error, that error is returned as err; otherwise err is EOF.

type PipeWriter

type PipeWriter[T any] struct {
	// contains filtered or unexported fields
}

A PipeWriter is the write half of a pipe.

func (*PipeWriter[T]) Close

func (w *PipeWriter[T]) Close() error

Close closes the writer; subsequent reads from the read half of the pipe will return no bytes and EOF.

func (*PipeWriter[T]) CloseWithError

func (w *PipeWriter[T]) CloseWithError(err error) error

CloseWithError closes the writer; subsequent reads from the read half of the pipe will return no bytes and the error err, or EOF if err is nil.

CloseWithError never overwrites the previous error if it exists and always returns nil.

func (*PipeWriter[T]) Write

func (w *PipeWriter[T]) Write(data []T) (n int, err error)

Write implements the standard Write interface: it writes data to the pipe, blocking until one or more readers have consumed all the data or the read end is closed. If the read end is closed with an error, that err is returned as err; otherwise err is ErrClosedPipe.

type ReadCloser

type ReadCloser[T any] interface {
	Reader[T]
	Closer
}

ReadCloser is the interface that groups the basic Read and Close methods.

type Reader

type Reader[T any] interface {
	Read(p []T) (n int, err error)
}

Reader is the interface that wraps the basic Read method.

Read reads up to len(p) types into p. It returns the number of types read (0 <= n <= len(p)) and any error encountered. Even if Read returns n < len(p), it may use all of p as scratch space during the call. If some data is available but not len(p) bytes, Read conventionally returns what is available instead of waiting for more.

When Read encounters an error or end-of-file condition after successfully reading n > 0 types, it returns the number of bytes read. It may return the (non-nil) error from the same call or return the error (and n == 0) from a subsequent call. An instance of this general case is that a Reader returning a non-zero number of types at the end of the input stream may return either err == EOF or err == nil. The next Read should return 0, EOF.

Callers should always process the n > 0 bytes returned before considering the error err. Doing so correctly handles I/O errors that happen after reading some types and also both of the allowed EOF behaviors.

Implementations of Read are discouraged from returning a zero byte count with a nil error, except when len(p) == 0. Callers should treat a return of 0 and nil as indicating that nothing happened; in particular it does not indicate EOF.

Implementations must not retain p.

type WriteCloser

type WriteCloser[T any] interface {
	Writer[T]
	Closer
}

WriteCloser is the interface that groups the basic Write and Close methods.

type Writer

type Writer[T any] interface {
	Write(p []T) (n int, err error)
}

Writer is the interface that wraps the basic Write method.

Write writes len(p) types from p to the underlying data stream. It returns the number of types written from p (0 <= n <= len(p)) and any error encountered that caused the write to stop early. Write must return a non-nil error if it returns n < len(p). Write must not modify the slice data, even temporarily.

Implementations must not retain p.

Directories

Path Synopsis
package pipe implements type-safe unix like resources standard streams and pipelines This can be used to write unix like tools communicating through Go native types which can be connected together like unix allows.
package pipe implements type-safe unix like resources standard streams and pipelines This can be used to write unix like tools communicating through Go native types which can be connected together like unix allows.
package pipe implements unix like resources standard streams and pipelines This can be used to write unix like tools communicating through Go stream of bytes which can be connected together like unix allows.
package pipe implements unix like resources standard streams and pipelines This can be used to write unix like tools communicating through Go stream of bytes which can be connected together like unix allows.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL