doppelgangerreader

package module
v0.0.0-...-459d947 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2022 License: MIT Imports: 6 Imported by: 1

README

DoppelgangerReader Travis Codecov GoDoc go-report

DoppelgangerReader provides a way to read one io.Reader multiple times.

package main

import (
	"fmt"
	"bytes"
	"io/ioutil"
	"github.com/Eun/go-doppelgangerreader"
)

func main() {
	reader := bytes.NewBufferString("Hello World")
	factory := doppelgangerreader.NewFactory(reader)
	defer factory.Close()

	d1 := factory.NewDoppelganger()
	defer d1.Close()

	fmt.Println(ioutil.ReadAll(d1))

	d2 := factory.NewDoppelganger()
	defer d2.Close()

	fmt.Println(ioutil.ReadAll(d2))
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func HTTPMiddleware

func HTTPMiddleware(handler http.Handler, limit int64) http.Handler

HTTPMiddleware adds a doppelganger factory for the body to the request. At the same time it replaces the original body with a doppelganger reader. You can specify a size limit for the reader (0 disables the limit) The factory can be fetched by using HTTPBodyFactory()

func IsNilReaderError

func IsNilReaderError(e error) bool

IsNilReaderError returns true if the specified error is a NilReaderError

Types

type DoppelgangerFactory

type DoppelgangerFactory interface {
	NewDoppelganger() io.ReadCloser
	RemoveDoppelganger(r io.ReadCloser) error
	Close() error
}

DoppelgangerFactory is a reader that mimics the behaviour of an other reader it can be used to read readers multiple times

Example (HttpErrorHandler)
handler := http.NewServeMux()
handler.HandleFunc("/", func(w http.ResponseWriter, request *http.Request) {
	_, _ = ioutil.ReadAll(request.Body)
	panic("some random error")
})

http.ListenAndServe(":8000", errorHandler{handler})
Output:

func GetFactory

func GetFactory(reader io.Reader) DoppelgangerFactory

GetFactory returns the DoppelgangerFactory if the reader is a Doppelganger

func HTTPBodyFactory

func HTTPBodyFactory(r *http.Request) DoppelgangerFactory

HTTPBodyFactory returns a http body factory for a request. Notice that you have to use the HTTPMiddleware function.

func NewFactory

func NewFactory(readerToMimic io.Reader) DoppelgangerFactory

NewFactory creates a new DoppelgangerFactory with the original reader specified if the reader is already a Doppelganger it will return the original factory

type NilReaderError

type NilReaderError struct{}

NilReaderError will be reported if the provided reader is nil

func (NilReaderError) Error

func (NilReaderError) Error() string

Error returns the error message

Jump to

Keyboard shortcuts

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