encodingiter

package
v0.0.17 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

encodingiter defines iterator source/collector that corresponds to std library `encoding` and all its descendants (`encoding/*`)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CsvReader

func CsvReader(r *csv.Reader) iter.Seq2[[]string, error]

CsvReader returns an iterator over csv lines. Unlike JsonDecoder or XmlDecoder, the iterator does not stop on first error since it could yield csv.ErrFieldCount with partial record and continue to next line.

io.EOF is excluded from result.

func Decode

func Decode[V any, Dec interface{ Decode(any) error }](dec Dec) iter.Seq2[V, error]

Decode returns an iterator over consecutive decode results of dec.

The iterator stops if and only if dec returns io.EOF. Handling other errors is caller's responsibility. If the first error should stop the iterator, use hiter.LimitUntil, hiter.LimitAfter or even *errbox.Box.

func Encode

func Encode[V any, Enc interface{ Encode(v any) error }](enc Enc, seq iter.Seq[V]) error

Encode writes consecutive decode results of all values from seq. The first error stops the iteration.

func JsonDecoder

func JsonDecoder(dec *json.Decoder) iter.Seq2[json.Token, error]

JsonDecoder returns an iterator over json tokens. The first non-nil error encountered stops iteration after yielding it. io.EOF is excluded from result.

func Write

func Write[V any](w io.Writer, marshaler func(v V, written int) ([]byte, error), seq iter.Seq[V]) (n int, er error)

Write writes all values from seq by marshaling it through marshaler. It returns the number of byte written to w. If marshaler or io.Writer returns an error it stops the iteration and the function returns the error with numbers of written bytes so far.

The marshaler receives each value from seq and accumulative number of bytes written.

func Write2

func Write2[K, V any](w io.Writer, marshaler func(k K, v V, written int) ([]byte, error), seq iter.Seq2[K, V]) (n int, er error)

Write2 is like Write but accepts iter.Seq2.

func WriteBinaryMarshaler

func WriteBinaryMarshaler[Enc encoding.BinaryMarshaler](w io.Writer, sep []byte, seq iter.Seq[Enc]) (n int, err error)

WriteBinaryMarshaler writes all calling results of encoding.BinaryMarshaler.MarshalBinary from seq to w. It returns the number of bytes written to w and an error first encountered.

func WriteTextMarshaler

func WriteTextMarshaler[Enc encoding.TextMarshaler](w io.Writer, sep []byte, seq iter.Seq[Enc]) (n int, err error)

WriteTextMarshaler writes all calling results of encoding.TextMarshaler.MarshalText from seq to w. It returns the number of bytes written to w and an error first encountered.

func XmlDecoder

func XmlDecoder(dec *xml.Decoder) iter.Seq2[xml.Token, error]

XmlDecoder returns an iterator over xml tokens. The first non-nil error encountered stops iteration after yielding it. io.EOF is excluded from result. The caller should call xml.CopyToken before going to next iteration if they need to retain tokens.

Types

This section is empty.

Jump to

Keyboard shortcuts

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