writer

package
v0.0.0-...-02bf512 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2024 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package writer provides writers

SPDX-License-Identifier: Apache-2.0

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IOWriterAsLinesGen

func IOWriterAsLinesGen(dst io.Writer) func(string) error

IOWriterAsLinesGen generates a writing function for writing lines to an io.Writer. The idea is that each string is one line, but does not end in any line ending sequence. The OS line ending sequence is used. Returns an error if any rune is not written: - If the Writer returns an error, it is returned as is - If the Writer wrote fewer bytes than the UTF-8 encoding requires, a custom error containing the number of bytes written is returned

func IOWriterAsRunesGen

func IOWriterAsRunesGen(dst io.Writer) func(rune) error

IOWriterAsRunesGen generates a writing function for writing runes to an io.Writer. Returns an error if the rune is not written: - If the Writer returns an error, it is returned as is - If the Writer wrote fewer bytes than the UTF-8 encoding requires, a custom error containing the hex value of the rune is returned

func IOWriterAsStringsGen

func IOWriterAsStringsGen(dst io.Writer) func(string) error

IOWriterAsStringsGen generates a writing function for writing strings to an io.Writer. Returns an error if any rune is not written: - If the Writer returns an error, it is returned as is - If the Writer wrote fewer bytes than the UTF-8 encoding requires, a custom error containing the number of bytes written is returned

func IOWriterGen

func IOWriterGen(dst io.Writer) func(byte) error

IOWriterGen generates a writing function for writing bytes to an io.Writer. Returns an error if the byte is not written: - If the Writer returns an error, it is returned as is - If the Writer wrote 0 bytes with no error, an custom error containing the hex value of the byte is returned

func MapWriterGen

func MapWriterGen[K comparable, V any](m map[K]V) func(tuple.Two[K, V]) error

MapWriterGen generates a writing function for a map[K]V. A pointer to the map is not required, as the map internally reallocates as needed, so that the caller map address never changes.

func SliceWriterGen

func SliceWriterGen[T any](slc *[]T) func(T) error

SliceWritergen generates a writing function for a slice of type T. A pointer to the slice is required so that if the slice has to be reallocated, the address of the caller slice can be changed.

Types

type Writer

type Writer[T any] interface {
	Write(vals ...T) error
}

Writer defines a single method for writing zero or more values of type T to some destination. Returns an error if any value of type T cannot be written to the destination successfully. The error object contains whatever value the underlying storage function provides that could not be written.

func NewWriter

func NewWriter[T any](writerFn func(T) error) Writer[T]

NewWriter constructs a Writer[T] from a writing function that accepts T and returns error. Panics if writerFn is nil.

func OfIOWriterAsBytes

func OfIOWriterAsBytes(dst io.Writer) Writer[byte]

OfIOWriterAsBytes returns a Writer[byte] that writes bytes to the given io.Writer. Panics if writing a byte fails.

func OfIOWriterAsLines

func OfIOWriterAsLines(dst io.Writer) Writer[string]

OfIOWriterAsLines returns a Writer[string] that writes UTF-8 encoded strings to the given io.Writer. Each string is followed by the OS line ending sequence, where Windows uses CRLF and all other systems use LF. Panics if writing any byte of the string fails.

func OfIOWriterAsRunes

func OfIOWriterAsRunes(dst io.Writer) Writer[rune]

OfIOWriterAsRunes returns a Writer[rune] that writes UTF-8 encoded runes to the given io.Writer. Panics if writing any byte of a rune fails.

func OfIOWriterAsStrings

func OfIOWriterAsStrings(dst io.Writer) Writer[string]

OfIOWriterAsStrings returns a Writer[string] that writes UTF-8 encoded strings to the given io.Writer. Panics if writing any byte of the string fails.

func OfMapWriter

func OfMapWriter[K comparable, V any](dst map[K]V) Writer[tuple.Two[K, V]]

OfMapWriter returns a Writer[tuple.Two[K, V]] that writes to the given map

func OfSliceWriter

func OfSliceWriter[T any](dst *[]T) Writer[T]

OfSliceWriter returns a Writer[T] that writes to the given slice

type WriterImpl

type WriterImpl[T any] struct {
	// contains filtered or unexported fields
}

WriterImpl is the base implementation of Writer[T]

func (WriterImpl[T]) Write

func (w WriterImpl[T]) Write(vals ...T) error

Write returns nil unless the value given cannot be written to the destination.

Jump to

Keyboard shortcuts

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