Skip to content

crunchypi/iox

Repository files navigation

iox

Generic extension of Go's io pkg.

Index

Errors

Expand/collapse section

This package does not define any new errors, it inherits them from the io package in the standard library.

io.EOF              // Used by e.g iox.Reader: Stop reading/consuming
io.ErrClosedPipe    // Used by e.g iox.Writer: Stop writing/producing.

Core interfaces

Core interfaces are the iox.Reader and iox.Writerlisted below. They mirror io.Reader and io.Writerbut differ in that they work with generic values instead. An extra addition is the use of context.Context, since io often involves program bounds (also it gives some added flexibility).

type Reader[T any] interface {
	Read(context.Context) (T, error)
}
type Writer[T any] interface {
	Write(context.Context, T) error
}
As with the io package from the standard library, iox readers and writers are combined with eachother and io.Closer. The full set of combinations can be seen by clicking here
type Reader[T any] interface {
	Read(context.Context) (T, error)
}

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

type Writer[T any] interface {
	Write(context.Context, T) error
}

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

type ReadWriter[T, U any] interface {
	Reader[T]
	Writer[U]
}

type ReadWriteCloser[T, U any] interface {
	io.Closer
	Reader[T]
	Writer[U]
}

There are also "impl" structs which let you implement core interfaces with functions, which reduces a lot of boilerplate. These can be seen by clicking on this section

Signatures are links to the Go playground (examples).

Constructors

All links go to examples on the Go playground.

Alternatively, you may see signatures and docs by clicking here
// NewReaderFrom returns a Reader which yields values from the given vals.
func NewReaderFrom[T any](vs ...T) Reader[T]
// NewReaderFromBytes creates a new T reader from an io.Reader and Decoder.
// It simply reads bytes from 'r', decodes them, and passes them along to the
// caller. As such, the decoder must match the encoder used to create the bytes.
// If 'r' is nil, an empty Reader is returned; if 'f' is nil, the decoder is set
// to json.NewDecoder.
func NewReaderFromBytes[T any](r io.Reader) func(f decoderFn) Reader[T]
// NewReaderFromValues creates an io.Reader from a Reader and Encoder.
// It simply reads values from 'r', encodes them, and passes them along to the
// caller. As such, when decoding values from the returned io.Reader one should
// use a decoder which matches the encoder passed here. If 'r' is nil, an
// empty (not nil) io.Reader is returned; if 'f' is nil, the encoder is set to
// json.NewEncoder. 
func NewReaderFromValues[T any](r Reader[T]) func(f encoderFn) io.Reader
// NewWriterFromValues returns a Writer which accepts values, encodes them
// using the given encoder, and then writes them to 'w'. If 'w' is nil, an empty
// Writer is returned; if 'f' is nil, the encoder is set to json.NewEncoder.
func NewWriterFromValues[T any](w io.Writer) func(f encoderFn) Writer[T]
// NewWriterFromBytes returns an io.Writer which accepts bytes, decodes them
// using the given decoder, and then writes them to 'w'. If 'w' is nil, an emtpy
// io.Writer is returned; if 'f' is nil, the decoder is set to json.NewDecoder.
func NewWriterFromBytes[T any](w Writer[T]) func(f decoderFn) io.Writer 
// NewReadWriterFrom returns a ReadWriter[T] which writes into- and read from
// an internal buffer. The buffer is initially populated with the given values.
// The buffer acts like a stack, and a read while the buf is empty returns io.EOF.
func NewReadWriterFrom[T any](vs ...T) ReadWriter[T, T]

Modifiers

All links go to examples on the Go playground.

Batching.

Filtering & mapping.

About

Generic extension of Go's io pkg

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages