Documentation
¶
Index ¶
- func NewReaderFromBytes[T any](r io.Reader) func(f decoderFn) Reader[T]
- func NewReaderFromValues[T any](r Reader[T]) func(f encoderFn) io.Reader
- func NewWriterFromBytes[T any](w Writer[T]) func(f decoderFn) io.Writer
- func NewWriterFromValues[T any](w io.Writer) func(f encoderFn) Writer[T]
- type Decoder
- type DecoderImpl
- type Encoder
- type EncoderImpl
- type ReadCloser
- type ReadCloserImpl
- type ReadWriteCloser
- type ReadWriteCloserImpl
- type ReadWriter
- type ReadWriterImpl
- type Reader
- type ReaderImpl
- type WriteCloser
- type WriteCloserImpl
- type Writer
- type WriterImpl
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewReaderFromBytes ¶
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 ¶
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 ¶
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 ¶
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 ¶
Decoder decodes values from binary form. Some commonly used encoders are:
- json.NewDecoder(bytes.NewBuffer(nil))
- gob.NewDecoder(bytes.NewBuffer(nil))
type DecoderImpl ¶
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 ¶
Encoder encodes values into binary form. Some commonly used encoders are:
- json.NewEncoder(bytes.NewBuffer(nil))
- gob.NewEncoder(bytes.NewBuffer(nil))
type EncoderImpl ¶
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 ¶
ReadCloser groups Reader with io.Closer.
type ReadCloserImpl ¶
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.
type ReadWriteCloser ¶
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.
type ReadWriter ¶
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):
type Reader ¶
Reader reads T, it is intended as a generic variant of io.Reader.
func NewReaderFrom ¶
NewReaderFrom returns a Reader which yields values from the given vals.
Examples (interactive):
func NewReaderWithBatching ¶
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 ¶
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 ¶
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.
},
}
}
type WriteCloser ¶
WriteCloser groups Writer with io.Closer.
type WriteCloserImpl ¶
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.
type Writer ¶
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 ¶
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 ¶
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 ¶
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.
},
}
}