chain

package module
v0.1.8 Latest Latest
Warning

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

Go to latest
Published: May 2, 2021 License: MIT Imports: 4 Imported by: 0

README

chain

Chain readers and writers with ease

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type NopWriteCloser added in v0.1.7

type NopWriteCloser struct {
	io.Writer
}

func (NopWriteCloser) Close added in v0.1.7

func (NopWriteCloser) Close() error

type OS added in v0.1.5

type OS struct {
	RootDir string
}

func (OS) Close added in v0.1.5

func (o OS) Close() error

func (OS) Create added in v0.1.7

func (o OS) Create(name string) (io.WriteCloser, error)

func (OS) Open added in v0.1.5

func (o OS) Open(name string) (io.ReadCloser, error)

type ReadChain

type ReadChain func(io.ReadCloser) (io.ReadCloser, error)

ReadChain represents a common pattern in go packages. For examples, see: https://pkg.go.dev/compress/gzip#NewReader

While this type only expects an io.Reader to be returned, it will detect if the result is a Closer and make sure the types Close function is run.

type ReadCloser added in v0.1.7

type ReadCloser struct {
	io.Reader
	io.Closer
}

type ReadCloser2 added in v0.1.7

type ReadCloser2 struct {
	io.ReadCloser
	Closer io.Closer
}

func (ReadCloser2) Close added in v0.1.7

func (wc ReadCloser2) Close() error

type ReadFS added in v0.1.4

type ReadFS interface {
	Open(path string) (io.ReadCloser, error)
	io.Closer
}

type ReadFSChain added in v0.1.4

type ReadFSChain func(io.ReadCloser) (ReadFS, error)

type ReaderBuilder added in v0.1.1

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

ReaderBuilder lets you build a chain of io.Readers in a more natural way

func ReadingFrom

func ReadingFrom(r io.ReadCloser) *ReaderBuilder

ReadingFrom creates a new ReaderBuilder where the contents of the chain comes from the given io.Reader

If r is an io.ReadCloser, the resulting Reader after building the chain will call r.Close for you

func (*ReaderBuilder) AsFS added in v0.1.4

func (chain *ReaderBuilder) AsFS(next ReadFSChain) *ReaderFSBuilder
Example
package main

import (
	"encoding/base64"
	"io"
	"os"

	"github.com/conradludgate/chain"
	"github.com/conradludgate/chain/archive"
	"github.com/conradludgate/chain/encoding"
)

func main() {
	zip := archive.ZipConfig{}
	b64 := encoding.Base64Config{Encoding: base64.RawStdEncoding}

	fs, _ := chain.ReadingFromFS(chain.OS{RootDir: "./example"}).
		Open("archive.zip").
		AsFS(zip.FSReader).
		Finally(b64.Decode)
	defer fs.Close()

	r, _ := fs.Open("hello.txt")
	defer r.Close()
	_, _ = io.Copy(os.Stdout, r)
}
Output:

func (*ReaderBuilder) Finally added in v0.1.1

func (chain *ReaderBuilder) Finally(next ReadChain) (io.ReadCloser, error)

Finally adds the last ReadChain to the current builder chain, then builds it into an io.ReadCloser

func (*ReaderBuilder) Then added in v0.1.1

func (chain *ReaderBuilder) Then(next ReadChain) *ReaderBuilder

Then adds the next ReadChain to the current builder chain

type ReaderFSBuilder added in v0.1.4

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

func ReadingFromFS added in v0.1.5

func ReadingFromFS(fs ReadFS) *ReaderFSBuilder
Example
package main

import (
	"encoding/base64"
	"io"
	"os"

	"github.com/conradludgate/chain"
	"github.com/conradludgate/chain/encoding"
)

func main() {
	b64 := encoding.Base64Config{Encoding: base64.RawStdEncoding}

	fs, _ := chain.ReadingFromFS(chain.OS{RootDir: "./example"}).Finally(b64.Decode)
	defer fs.Close()

	r, _ := fs.Open("hello.txt")
	defer r.Close()
	_, _ = io.Copy(os.Stdout, r)
}
Output:

func (*ReaderFSBuilder) Finally added in v0.1.4

func (chain *ReaderFSBuilder) Finally(next ReadChain) (ReadFS, error)

func (*ReaderFSBuilder) Open added in v0.1.6

func (chain *ReaderFSBuilder) Open(name string) *ReaderBuilder
Example
package main

import (
	"encoding/base64"
	"io"
	"os"

	"github.com/conradludgate/chain"
	"github.com/conradludgate/chain/archive"
	"github.com/conradludgate/chain/encoding"
)

func main() {
	zip := archive.ZipConfig{}
	b64 := encoding.Base64Config{Encoding: base64.RawStdEncoding}

	r, _ := chain.ReadingFromFS(chain.OS{RootDir: "./example"}).
		Open("archive.zip").
		AsFS(zip.FSReader).
		Open("hello.txt").
		Finally(b64.Decode)

	defer r.Close()
	_, _ = io.Copy(os.Stdout, r)
}
Output:

func (*ReaderFSBuilder) Then added in v0.1.4

func (chain *ReaderFSBuilder) Then(next ReadChain) *ReaderFSBuilder

type WriteChain

type WriteChain func(io.WriteCloser) (io.WriteCloser, error)

WriteChain represents a common pattern in go packages. For examples, see: https://pkg.go.dev/compress/gzip#NewWriter https://pkg.go.dev/golang.org/x/crypto/openpgp#Encrypt

While this type only expects an io.Writer to be returned, it will detect if the result is a Closer and make sure the types Close function is run.

type WriteCloser2 added in v0.1.7

type WriteCloser2 struct {
	io.WriteCloser
	Closer io.Closer
}

func (WriteCloser2) Close added in v0.1.7

func (wc WriteCloser2) Close() error

type WriteFS added in v0.1.4

type WriteFS interface {
	Create(path string) (io.WriteCloser, error)
	io.Closer
}

type WriteFSBuilder added in v0.1.4

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

func (*WriteFSBuilder) Then added in v0.1.4

func (wc *WriteFSBuilder) Then(next WriteChain) *WriteFSBuilder

func (*WriteFSBuilder) WritingTo added in v0.1.4

func (wc *WriteFSBuilder) WritingTo(w io.WriteCloser) (WriteFS, error)

type WriteFSChain added in v0.1.4

type WriteFSChain func(io.WriteCloser) (WriteFS, error)

type WriterBuilder added in v0.1.1

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

WriterBuilder lets you build a chain of io.Writers in a more natural way

func NewWriteBuilder added in v0.1.1

func NewWriteBuilder(first WriteChain) *WriterBuilder

NewWriteBuilder creates a new WriteBuilder with the given WriteChain being the first in the chain

Example
package main

import (
	"bytes"
	"encoding/hex"
	"io"
	"os"

	"github.com/conradludgate/chain"
	"github.com/conradludgate/chain/cipher"
	"github.com/conradludgate/chain/compress"
)

func main() {
	key, _ := hex.DecodeString("6368616e676520746869732070617373")
	aes := cipher.AESConfig{Key: key}
	gzip := compress.GZIPConfig{}

	output := bytes.NewBuffer(nil)

	w, _ := chain.NewWriteBuilder(aes.Encrypt).
		Then(gzip.Compress).
		WritingTo(chain.NopWriteCloser{Writer: output})

	_, _ = io.WriteString(w, "hello world")
	w.Close()

	r, _ := chain.ReadingFrom(io.NopCloser(output)).
		Then(gzip.Decompress).
		Finally(aes.Decrypt)

	_, _ = io.Copy(os.Stdout, r)
	r.Close()

}
Output:

hello world

func (*WriterBuilder) IntoFS added in v0.1.4

func (wc *WriterBuilder) IntoFS(next WriteFSChain) *WriteFSBuilder
Example
package main

import (
	"bytes"
	"encoding/hex"
	"io"
	"os"

	"github.com/conradludgate/chain"
	"github.com/conradludgate/chain/archive"
	"github.com/conradludgate/chain/cipher"
)

func main() {
	key, _ := hex.DecodeString("6368616e676520746869732070617373")
	aes := cipher.AESConfig{Key: key}
	zip := archive.ZipConfig{}

	output := bytes.NewBuffer(nil)

	w, _ := chain.NewWriteBuilder(aes.Encrypt).
		Open("hello.txt").
		InFS(zip.FSWriter).
		WritingTo(chain.NopWriteCloser{Writer: output})

	_, _ = io.WriteString(w, "hello world")
	w.Close()

	r, _ := chain.ReadingFrom(io.NopCloser(output)).
		AsFS(zip.FSReader).
		Open("hello.txt").
		Finally(aes.Decrypt)

	_, _ = io.Copy(os.Stdout, r)
	r.Close()

}
Output:

hello world

func (*WriterBuilder) Open added in v0.1.8

func (wc *WriterBuilder) Open(name string) *WriterFileBuilder

func (*WriterBuilder) Then added in v0.1.1

func (wc *WriterBuilder) Then(next WriteChain) *WriterBuilder

Then adds the next WriteChain to the current builder chain. Returns self

func (*WriterBuilder) WritingTo added in v0.1.1

func (wc *WriterBuilder) WritingTo(w io.WriteCloser) (io.WriteCloser, error)

WritingTo builds the chain. The resulting data from the chain is written to the io.Writer provided.

If w is a Closer type too, calling the returned writer's Close function will also close w.

func (*WriterBuilder) WritingToFS added in v0.1.5

func (wc *WriterBuilder) WritingToFS(fs WriteFS) WriteFS

type WriterFileBuilder added in v0.1.8

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

func (*WriterFileBuilder) InFS added in v0.1.8

func (builder *WriterFileBuilder) InFS(next WriteFSChain) *WriterBuilder

func (*WriterFileBuilder) WritingToFS added in v0.1.8

func (builder *WriterFileBuilder) WritingToFS(fs WriteFS) (io.WriteCloser, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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