core

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewReaderFromBytes

func NewReaderFromBytes[T any](r io.Reader) func(f decoderFn) Reader[T]

NewReaderFromBytes converts an io.Reader (bytes) into a iox.Reader (values). Nil 'r' returns an empty non-nil Reader; nil 'f' uses json.NewDecoder.

Examples (interactive):

Example:

// Used as io.Reader
b := bytes.NewBuffer(nil)

// Using json encoder, so the decoder has to be json in NewReaderFromBytes
json.NewEncoder(b).Encode("test1")

r := NewReaderFromBytes[string](b)(
	func(r io.Reader) Decoder {
		return json.NewDecoder(r)
	},
)

t.Log(r.Read(context.Background())) // "test1" <nil>
t.Log(r.Read(context.Background())) // "", io.EOF

func NewReaderFromValues

func NewReaderFromValues[T any](r Reader[T]) func(f encoderFn) io.Reader

NewReaderFromValues converts an iox.Reader (values) into an io.Reader (bytes). Nil 'r' returns an empty non-nil Reader; nil 'f' uses json.NewEncoder.

Examples (interactive):

Example:

// Create the io.Reader from value Reader.
r := NewReaderFromValues(NewReaderFrom("test1"))(
    func(w io.Writer) Encoder {
        return json.NewEncoder(w)
    },
)

// Instantly pass it to a decoder just so we may log out the values.
dec := json.NewDecoder(r)
val := ""

t.Log(dec.Decode(&val), val) // <nil>, "test1"
t.Log(dec.Decode(&val), val) // EOF, "test1" <--- val is unchanged.

func NewWriterFromBytes

func NewWriterFromBytes[T any](w Writer[T]) func(f decoderFn) io.Writer

NewWriterFromBytes creates an io.Writer (bytes) which writes into 'w'. Nil 'w' returns an empty non-nil Writer; nil 'f' uses json.NewDecoder.

Examples (interactive):

Example:

// Writes simply logs values.
vw := WriterImpl[int]{
	Impl: func(ctx context.Context, v int) error {
		t.Log(v)
		return nil
	},
}

// io.Writer
bw := NewWriterFromBytes(vw)(
	func(r io.Reader) Decoder {
		return json.NewDecoder(r)
	},
)

// Logs "9"
json.NewEncoder(bw).Encode(9)

func NewWriterFromValues

func NewWriterFromValues[T any](w io.Writer) func(f encoderFn) Writer[T]

NewWriterFromValues creates a Writer (vals) which writes into 'w'. Nil 'w' returns an empty non-nil Writer; nil 'f' uses json.NewEncoder.

Examples (interactive):

Example:

// Defining our io.Writer to rcv the data + encoding method.
b := bytes.NewBuffer(nil)
f := func(w io.Writer) Encoder { return json.NewEncoder(w) }
w := NewWriterFromValues[int](b)(f)

// Write values, they are encoded and passed to 'b'. Err handling ignored.
w.Write(nil, 2)

// We'll use these to read what's in 'b'.
dec := json.NewDecoder(b)
val := 0

t.Log(dec.Decode(&val), val) // <nil> 2
t.Log(dec.Decode(&val), val) // EOF 2

Types

type Decoder

type Decoder interface {
	Decode(e any) error
}

Decoder decodes values from binary form. Some commonly used encoders are:

  • json.NewDecoder(bytes.NewBuffer(nil))
  • gob.NewDecoder(bytes.NewBuffer(nil))

type DecoderImpl

type DecoderImpl struct {
	Impl func(d any) error
}

DecoderImpl lets you implement Decoder with a function. Place it into "Impl" and it will be called by the "Decode" method.

Example:

func myDecoder() Decoder {
    return DecoderImpl{
        Impl: func(d any) error {
            // Your code.
        }
    }
}

func (DecoderImpl) Decode

func (impl DecoderImpl) Decode(d any) error

Decode implements Decoder by deferring to the internal "Impl" func. If the internal "Impl" is not set, an io.EOF will be returned.

type Encoder

type Encoder interface {
	Encode(e any) error
}

Encoder encodes values into binary form. Some commonly used encoders are:

  • json.NewEncoder(bytes.NewBuffer(nil))
  • gob.NewEncoder(bytes.NewBuffer(nil))

type EncoderImpl

type EncoderImpl struct {
	Impl func(e any) error
}

EncoderImpl lets you implement Encoder with a function. Place it into "Impl" and it will be called by the "Encode" method.

Example:

func myEncoder() Encoder {
    return EncoderImpl{
        Impl: func(e any) error {
            // Your code.
        }
    }
}

func (EncoderImpl) Encode

func (impl EncoderImpl) Encode(e any) error

Encode implements Encoder by deferring to the internal "Impl" func. If the internal "Impl" is not set, an io.ErrClosedPipe will be returned.

type ReadCloser

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

ReadCloser groups Reader with io.Closer.

type ReadCloserImpl

type ReadCloserImpl[T any] struct {
	ImplC func() error
	ImplR func(context.Context) (T, error)
}

ReadCloserImpl lets you implement ReadCloser with functions. This is similar to ReaderImpl but lets you implement io.Closer as well.

Examples (interactive):

func (ReadCloserImpl[T]) Close

func (impl ReadCloserImpl[T]) Close() (err error)

Read implements Closer by deferring to the internal "ImplC" func. If the internal "ImplC" func is nil, nothing will happen.

func (ReadCloserImpl[T]) Read

func (impl ReadCloserImpl[T]) Read(ctx context.Context) (r T, err error)

Read implements Reader by deferring to the internal "ImplR" func. If the internal "ImplR" is not set, an io.EOF will be returned.

type ReadWriteCloser

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

ReadWriteCloser groups Reader[T] and Writer[U] with io.Closer.

type ReadWriteCloserImpl

type ReadWriteCloserImpl[T, U any] struct {
	ImplC func() error
	ImplR func(context.Context) (T, error)
	ImplW func(context.Context, U) error
}

ReadWriteCloserImpl lets you implement ReadWriteCloser with functions. This is similar to ReadWriterImpl but lets you implement io.Closer as well.

Examples (interactive):

func (ReadWriteCloserImpl[T, U]) Close

func (impl ReadWriteCloserImpl[T, U]) Close() (err error)

Close implements io.Close by deferring to the internal ImplC func. If the internal ImplC func is nil, nothing will happen.

func (ReadWriteCloserImpl[T, U]) Read

func (impl ReadWriteCloserImpl[T, U]) Read(ctx context.Context) (r T, err error)

Read implements Reader[T] by deferring logic to the internal ImplR func. If it's not set, an io.EOF is returned.

func (ReadWriteCloserImpl[T, U]) Write

func (impl ReadWriteCloserImpl[T, U]) Write(ctx context.Context, v U) (err error)

Write implements Writer[U] by deferring logic to the internal ImplW func. If it's not set, an io.ErrClosedPipe is returned.

type ReadWriter

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

ReadWriter groups Reader[T] and Writer[U].

func NewReadWriterFrom

func NewReadWriterFrom[T any](vs ...T) ReadWriter[T, T]

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.

Examples (interactive):

Example:

ctx := context.Background()

rw := iox.NewReadWriterFrom("test1")
fmt.Println(rw.Read(ctx))
fmt.Println(rw.Read(ctx)) // <-- io.EOF

rw.Write(ctx, "test2")
fmt.Println(rw.Read(ctx))
fmt.Println(rw.Read(ctx)) // <-- io.EOF

type ReadWriterImpl

type ReadWriterImpl[T, U any] struct {
	ImplR func(context.Context) (T, error)
	ImplW func(context.Context, U) error
}

ReadWriterImpl lets you implement ReadWriter with functions. This is equivalent to using ReaderImpl and WriterImpl combined (see docs).

Examples (interactive):

func (ReadWriterImpl[T, U]) Read

func (impl ReadWriterImpl[T, U]) Read(ctx context.Context) (r T, err error)

Read implements the Reader[T] part of ReadWriter[T, U] by deferring logic to the internal ImplR func. If it's not set, an io.EOF is returned.

func (ReadWriterImpl[T, U]) Write

func (impl ReadWriterImpl[T, U]) Write(ctx context.Context, v U) (err error)

Write implements the Writer[U] part of ReadWriter[T, U] by deferring logic to the internal ImplW func. If it's not set, an io.ErrClosedPipe is returned.

type Reader

type Reader[T any] interface {
	Read(context.Context) (T, error)
}

Reader reads T, it is intended as a generic variant of io.Reader.

func NewReaderFrom

func NewReaderFrom[T any](vs ...T) Reader[T]

NewReaderFrom returns a Reader which yields values from the given vals.

Examples (interactive):

func NewReaderWithBatching

func NewReaderWithBatching[T any](r Reader[T], size int) Reader[[]T]

NewReaderWithBatching returns a reader which batches 'r' into slices with the given size. Nil 'r' returns an empty non-nil Reader, size <= 0 defaults to 8. Note, the last []T before an err (e.g io.EOF) may be smaller than 'size'.

Examples (interactive):

Example:

vr := NewReadWriterFrom(1,2,3)
sr := NewReaderWithBatching(vr, 2)

t.Log(sr.Read(nil)) // [1, 2], nil
t.Log(sr.Read(nil)) // [3], nil
t.Log(sr.Read(nil)) // [], io.EOF

func NewReaderWithUnbatching

func NewReaderWithUnbatching[T any](r Reader[[]T]) Reader[T]

NewReaderWithUnbatching returns a reader of T from a reader of []T. Note that there is some internal buffering, so you may want to use this with caution as an unread buffer may cause value loss.

Examples (interactive):

Example:

sr := NewReaderFrom([]int{1, 2}, []int{3})
vr := NewReaderWithUnbatching(sr)

t.Log(vr.Read(nil)) // 1, nil
t.Log(vr.Read(nil)) // 2, nil
t.Log(vr.Read(nil)) // 3, nil
t.Log(vr.Read(nil)) // 0, io.EOF

type ReaderImpl

type ReaderImpl[T any] struct {
	Impl func(context.Context) (T, error)
}

ReaderImpl lets you implement Reader with a function. Place it into "impl" and it will be called by the "Read" method.

Examples (interactive):

Example:

func myReader() Reader[int] {
    return ReaderImpl[int]{
        Impl: func(ctx context.Context) (int, error) {
            // Your implementation.
        },
    }
}

func (ReaderImpl[T]) Read

func (impl ReaderImpl[T]) Read(ctx context.Context) (r T, err error)

Read implements Reader by deferring to the internal "Impl" func. If the internal "Impl" is not set, an io.EOF will be returned.

type WriteCloser

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

WriteCloser groups Writer with io.Closer.

type WriteCloserImpl

type WriteCloserImpl[T any] struct {
	ImplC func() error
	ImplW func(context.Context, T) error
}

WriteCloserImpl lets you implement WriteCloser with functions. This is similar to WriterImpl but lets you implement io.Closer as well.

Examples (interactive):

func (WriteCloserImpl[T]) Close

func (impl WriteCloserImpl[T]) Close() error

Close implements io.Closer by deferring to the internal ImplC func. If the internal ImplC func is nil, nothing will happen.

func (WriteCloserImpl[T]) Write

func (impl WriteCloserImpl[T]) Write(ctx context.Context, v T) (err error)

Write implements Writer by deferring to the internal "ImplW" func. If the internal "ImplW" is not set, an io.ErrClosedPipe will be returned.

type Writer

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

Writer writes T, it is intended as a generic variant of io.Writer. Use io.ErrClosedPipe as a signal for when writing should stop.

func NewWriterWithBatching

func NewWriterWithBatching[T any](w Writer[[]T], size int) Writer[T]

NewWriterWithBatching returns a Writer which writes into a buffer of a given size. When the buffer is full, it is written into 'w'. Note that this should be used with caution due to the internal buffer, as there may be value loss if the process exits before the buffer is filled and written to 'w', e.g if 'size' is 10 but the process exits after only writing 9 times.

Examples (interactive):

Example:

// Writes which logs values through 't.Log'.
logWriter := WriterImpl[[]int]{}
logWriter.Impl = func(_ context.Context, v []int) error { t.Log(v); return nil }

w := NewWriterWithBatching(logWriter, 2)
w.Write(nil, 1)
w.Write(nil, 2) // Logger logs: '[1, 2]'
w.Write(nil, 3)

func NewWriterWithUnbatching

func NewWriterWithUnbatching[T any](w Writer[T]) Writer[[]T]

NewWriterWithUnbatching returns a Writer which accepts []T on a Write call, then iterates through the slice and writes each value to 'w'.

Examples (interactive):

Example:

// Writes which logs values through 't.Log'.
logWriter := WriterImpl[int]{}
logWriter.Impl = func(_ context.Context, v int) error { t.Log(v); return nil }

w := NewWriterWithUnbatching(logWriter)
w.Write(nil, []int{1, 2})
// ^ logWriter logs the following lines:
//  1
//  2

type WriterImpl

type WriterImpl[T any] struct {
	Impl func(context.Context, T) error
}

WriterImpl lets you implement Writer with a function. Place it into "impl" and it will be called by the "Write" method.

Examples (interactive):

Example:

func myWriter() Writer[int] {
    return WriterImpl[int]{
        Impl: func(ctx context.Context, v int) error {
            // Your implementation.
        },
    }
}

func (WriterImpl[T]) Write

func (impl WriterImpl[T]) Write(ctx context.Context, v T) (err error)

Write implements Writer by deferring to the internal "Impl" func. If the internal "Impl" is not set, an io.ErrClosedPipe will be returned.

Jump to

Keyboard shortcuts

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