Sources & I/O¶
Sources turn Go collections, channels, iterators, pull callbacks, and io.Reader inputs into lazy Stream[T] values.
Core Constructors¶
From¶
From[T any](src Source[T]) Stream[T] wraps any implementation of Source[T]. Passing nil returns a stream that yields ErrNilSource. See Custom Sources & Sinks for implementing Source[T].
FromSlice¶
FromSlice[T any](xs []T) Stream[T] emits slice elements in index order while checking ctx.Err() before each yield. An empty or nil slice yields zero items without error.
FromChan¶
FromChan[T any](ch <-chan T) Stream[T] reads values from ch until the channel closes or ctx cancels. Passing a nil channel returns an empty stream immediately.
FromChan checks ctx.Err() before selecting so an already-canceled context wins deterministically even when ch has a buffered value ready.
FromSeq and FromSeq2¶
FromSeq[T any](seq iter.Seq[T]) Stream[T] adapts a single-value standard Go iterator (slices.Values, maps.Keys, strings.Lines). FromSeq2[T any](seq iter.Seq2[T, error]) Stream[T] adapts a fallible iterator: items paired with ErrSkip are dropped silently, while any other non-nil error stops iteration and propagates downstream.
Passing a nil iterator yields an empty stream.
FromFunc¶
FromFunc[T any](next func(context.Context) (T, error)) Stream[T] pulls items repeatedly by calling next(ctx):
- Return
io.EOFto end the stream cleanly. - Return
ErrSkipto skip the current turn and pull again. - Return any other error to stop iteration and surface that error to the terminal.
- Passing
nilyieldsErrNilFunc.
messages := automi.FromFunc(func(ctx context.Context) (gRPCMessage, error) {
msg, err := rpcStream.Recv()
if err != nil {
return gRPCMessage{}, err // io.EOF closes stream cleanly
}
if msg.Heartbeat {
return gRPCMessage{}, automi.ErrSkip
}
return msg, nil
})
Standard I/O Sources¶
FromLines¶
FromLines(r io.Reader) Stream[string] streams newline-delimited text lines using bufio.Scanner. Passing nil yields ErrNilReader. Scanner errors propagate when iteration finishes.
errorsOnly := automi.FromLines(logFile).
Filter(func(line string) bool { return strings.Contains(line, `"level":"error"`) })
FromCSV¶
FromCSV(r io.Reader, opts ...CSVOption) Stream[[]string] streams parsed CSV records as []string slices. Passing nil yields ErrNilReader.
| Option | Effect |
|---|---|
Comma(r rune) |
Sets field delimiter rune (ignored if r <= 0; defaults to ,). |
SkipHeader() |
Discards the first non-empty record before yielding data rows. |
// Read tab-delimited inventory data while discarding the header row.
skuRows := automi.FromCSV(tsvReader, automi.Comma('\t'), automi.SkipHeader())
FromReader¶
FromReader(r io.Reader, chunkSize int) Stream[[]byte] reads chunks of up to chunkSize bytes from r. When chunkSize <= 0, it defaults to 32 KiB (32 * 1024). Each emitted []byte is a freshly cloned slice (slices.Clone) so retaining slices across stages or workers never aliases the internal read buffer.
Passing nil yields ErrNilReader. Non-io.EOF read errors yield any trailing n > 0 bytes first, then emit the error.
hasher := sha256.New()
err := automi.FromReader(resp.Body, 64*1024).
Tap(func(chunk []byte) { hasher.Write(chunk) }).
Into(ctx, automi.ToWriter[[]byte](dstFile))
Contract Summary¶
| Constructor | Nil / Invalid Input Behavior | Cancellation Check |
|---|---|---|
From |
nil Source yields ErrNilSource |
Delegated to Source.Stream(ctx) |
FromSlice |
nil / empty slice yields 0 items |
Checked before every element |
FromChan |
nil channel yields 0 items |
Checked before and inside select |
FromSeq |
nil iterator yields 0 items |
Checked before every element |
FromSeq2 |
nil iterator yields 0 items |
Checked before every element |
FromFunc |
nil function yields ErrNilFunc |
Checked before every next(ctx) call |
FromLines |
nil reader yields ErrNilReader |
Checked per scanned line |
FromCSV |
nil reader yields ErrNilReader |
Checked before header and every row |
FromReader |
nil reader yields ErrNilReader |
Checked before every Read |