linebufio

package module
v0.0.0-...-532a82e Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2021 License: MIT Imports: 5 Imported by: 0

README

linebufio

GoDoc Go Report Card CI codecov

Wrapper around io.Writer that is able to call hooks before or after writing a line to the writer:

package linebufio_test

import (
	"bytes"
	"github.com/gogolibs/linebufio"
	"github.com/stretchr/testify/require"
	"testing"
)

func TestExample(t *testing.T) {
	// This writer will be wrapped by LineWriter
	underlyingWriter := bytes.NewBufferString("")
	// Add prefix & suffix hooks
	lineWriter := linebufio.NewLineWriter(underlyingWriter).
		WithPrefixString("=>").
		WithSuffixString("<=")

	data1 := []byte("one\ntwo\r\nthree")
	bytesWritten, err := lineWriter.Write(data1)
	require.Nil(t, err)
	require.Equal(t, len(data1), bytesWritten)

	data2 := []byte(" four\nfive\nsix")
	bytesWritten, err = lineWriter.Write(data2)
	require.Nil(t, err)
	require.Equal(t, len(data2), bytesWritten)

	err = lineWriter.Close()
	require.Nil(t, err)

	expectedResult := "=>one<=\n=>two<=\r\n=>three four<=\n=>five<=\n=>six"
	actualResult := underlyingWriter.String()
	require.Equal(t, expectedResult, actualResult)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type HookFunc

type HookFunc = func(writer io.Writer) error

HookFunc is a type definition for a hook calls that LineWriter does at certain moments of operation. Examples are: before writing a line to the underlying writer or after writing a new line. HookFunc receives an underlying writer as an argument.

type LineWriter

type LineWriter struct {
	// contains filtered or unexported fields
}

LineWriter is a wrapper around io.Writer with main use case to have something done before or after writing a line to the underlying writer (for example, adding a prefix or a suffix).

func NewLineWriter

func NewLineWriter(writer io.Writer) *LineWriter

NewLineWriter creates a new LineWriter. A single argument is a underlying writer.

func (*LineWriter) Close

func (w *LineWriter) Close() error

Close flushes what's left in the buffer to the w.writer and calls before line hook if it's set.

func (*LineWriter) WithAfterLine

func (w *LineWriter) WithAfterLine(f HookFunc) *LineWriter

WithAfterLine is a builder method that sets a hook to run every time after a line is written to the writer.

func (*LineWriter) WithBeforeLine

func (w *LineWriter) WithBeforeLine(f HookFunc) *LineWriter

WithBeforeLine is a builder method that sets a hook to run every time before a line is written to the writer.

func (*LineWriter) WithPrefix

func (w *LineWriter) WithPrefix(p []byte) *LineWriter

WithPrefix is a builder method that sets a hook that adds a prefix to each written line.

func (*LineWriter) WithPrefixString

func (w *LineWriter) WithPrefixString(p string) *LineWriter

WithPrefixString is a convenience method for WithPrefix that receives a string argument.

func (*LineWriter) WithSuffix

func (w *LineWriter) WithSuffix(s []byte) *LineWriter

WithSuffix is a builder method that sets a hook that adds a suffix to each written line.

func (*LineWriter) WithSuffixString

func (w *LineWriter) WithSuffixString(s string) *LineWriter

WithSuffixString is a convenience method for WithSuffix that receives a string argument.

func (*LineWriter) Write

func (w *LineWriter) Write(data []byte) (int, error)

Write is a wrapper around w.writer.Write: it buffers lines before writing them. It allows to call arbitrary hooks before or after writing a line to w.writer. This can be used to add a prefix or a suffix.

Jump to

Keyboard shortcuts

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