writer

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: May 12, 2022 License: BSD-3-Clause Imports: 13 Imported by: 10

README

go-whosonfirst-writer

Go package that provides a common interface for writing data to multiple sources.

Documentation

Go Reference

Example

Writers are instantiated with the writer.NewWriter method which takes as its arguments a context.Context instance and a URI string. The URI's scheme represents the type of writer it implements and the remaining (URI) properties are used by that writer type to instantiate itself. For example:

import (
       "context"
       "github.com/whosonfirst/go-writer"
       "strings"
)

func main() {

	ctx := context.Background()
	
	r := strings.NewReader("Hello world")
	
	wr, _ := writer.NewWriter(ctx, "fs:///usr/local/example")
	wr.Write(ctx, wr, "hello/world.txt", r)
}

Error handling omitted for the sake of brevity.

Writers

The following writers are exported by this package. Additional writers are defined in separate go-writer-* packages.

cwd://

Implements the Writer interface for writing documents to the current working directory.

fs://

Implements the Writer interface for writing documents as files on a local disk.

io://

Implements the Writer interface for writing documents to an io.Writer instance.

multi://

Implements the Writer interface for writing documents to multiple Writer instances.

null://

Implements the Writer interface for writing documents to nowhere.

repo://

Implements the Writer interface for writing documents to a Who's On First "data" directory.

stdout://

Implements the Writer interface for writing documents to STDOUT.

See also

Documentation

Overview

Package writer provides a common interface for writing data to one or more sources.

Index

Constants

View Source
const IOWRITER_TARGET_KEY string = "github.com/whosonfirst/go-writer#io_writer"

IOWRITER_TARGET_KEY is the key used to store an `io.Writer` instance in a `context.Context` instance.

Variables

This section is empty.

Functions

func GetIOWriterFromContext added in v0.2.0

func GetIOWriterFromContext(ctx context.Context) (io.Writer, error)

GetIOWriterFromContext returns the `io.Writer` instance associated with the `IOWRITER_TARGET_KEY` value in 'ctx'.

func RegisterWriter added in v0.1.0

func RegisterWriter(ctx context.Context, scheme string, init_func WriterInitializationFunc) error

RegisterWriter registers 'scheme' as a key pointing to 'init_func' in an internal lookup table used to create new `Writer` instances by the `NewWriter` method.

func Schemes added in v0.4.1

func Schemes() []string

Schemes returns the list of schemes that have been registered.

func SetIOWriterWithContext added in v0.2.0

func SetIOWriterWithContext(ctx context.Context, wr io.Writer) (context.Context, error)

SetIOWriterWithContext returns a new `context.Context` instance with 'wr' assigned to the `IOWRITER_TARGET_KEY` value.

Types

type CwdWriter added in v0.7.0

type CwdWriter struct {
	Writer
	// contains filtered or unexported fields
}

CwdWriter is a struct that implements the `Writer` interface for writing documents to the current working directory.

func (*CwdWriter) Close added in v0.7.0

func (wr *CwdWriter) Close(ctx context.Context) error

Close closes the underlying writer mechanism.

func (*CwdWriter) Write added in v0.7.0

func (wr *CwdWriter) Write(ctx context.Context, path string, fh io.ReadSeeker) (int64, error)

Write copies the content of 'fh' to 'path'.

func (*CwdWriter) WriterURI added in v0.7.0

func (wr *CwdWriter) WriterURI(ctx context.Context, path string) string

WriterURI returns the final URI for 'path'

type FileWriter

type FileWriter struct {
	Writer
	// contains filtered or unexported fields
}

FileWriter is a struct that implements the `Writer` interface for writing documents as files on a local disk.

func (*FileWriter) Close added in v0.3.0

func (wr *FileWriter) Close(ctx context.Context) error

Close closes the underlying writer mechanism.

func (*FileWriter) Write

func (wr *FileWriter) Write(ctx context.Context, path string, fh io.ReadSeeker) (int64, error)

Write copies the content of 'fh' to 'path', where 'path' is assumed to be relative to the root path defined in the constuctor. If the root directory for 'path' does not exist it will be created.

func (*FileWriter) WriterURI added in v0.3.0

func (wr *FileWriter) WriterURI(ctx context.Context, path string) string

WriterURI returns the absolute URL for 'path' relative to the root directory defined in the `FileWriter` constuctor.

type IOWriter added in v0.2.0

type IOWriter struct {
	Writer
}

IOWriter is a struct that implements the `Writer` interface for writing documents to an `io.Writer` instance.

func (*IOWriter) Close added in v0.3.0

func (wr *IOWriter) Close(ctx context.Context) error

Close closes the underlying writer mechanism.

func (*IOWriter) Write added in v0.2.0

func (wr *IOWriter) Write(ctx context.Context, path string, fh io.ReadSeeker) (int64, error)

Write copies the content of 'fh' to 'path'. It is assumed that 'ctx' contains a valid `io.Writer` instance that has been assigned by the `SetIOWriterWithContext` method.

func (*IOWriter) WriterURI added in v0.3.0

func (wr *IOWriter) WriterURI(ctx context.Context, path string) string

WriterURI returns the final URI for path.

type MultiWriter added in v0.5.0

type MultiWriter struct {
	Writer
	// contains filtered or unexported fields
}

Type MultiWriter implements the `Writer` interface for writing documents to multiple `Writer` instances.

func (*MultiWriter) Close added in v0.5.0

func (mw *MultiWriter) Close(ctx context.Context) error

Closes closes each of the underlying `Writer` instances (in the order they were specified to the 'mw' instance).

func (*MultiWriter) Write added in v0.5.0

func (mw *MultiWriter) Write(ctx context.Context, key string, fh io.ReadSeeker) (int64, error)

Write copies the contents of 'fh' to each of the writers contained by 'mw' in the order they were specified.

func (*MultiWriter) WriterURI added in v0.5.0

func (mw *MultiWriter) WriterURI(ctx context.Context, key string) string

WriteURI returns an empty string. Because 'mw' has multiple underlying `Writer` instances each of which specifies their own `WriteURI` methods it's either a choice of returning a concatenated string (with all the values) or an empty string. The decision was made to opt for the latter.

type NullWriter

type NullWriter struct {
	Writer
}

NullWriter is a struct that implements the `Writer` interface for writing documents to nowhere.

func (*NullWriter) Close added in v0.3.0

func (wr *NullWriter) Close(ctx context.Context) error

Close is a no-op to conform to the `Writer` instance and returns nil.

func (*NullWriter) Write

func (wr *NullWriter) Write(ctx context.Context, path string, fh io.ReadSeeker) (int64, error)

Write copies the content of 'fh' to 'path' using an `io.Discard` writer.

func (*NullWriter) WriterURI added in v0.3.0

func (wr *NullWriter) WriterURI(ctx context.Context, path string) string

WriterURI returns the value of 'path'

type StdoutWriter

type StdoutWriter struct {
	Writer
}

StdoutWriter is a struct that implements the `Writer` interface for writing documents to STDOUT.

func (*StdoutWriter) Close added in v0.3.0

func (wr *StdoutWriter) Close(ctx context.Context) error

Close is a no-op to conform to the `Writer` instance and returns nil.

func (*StdoutWriter) Write

func (wr *StdoutWriter) Write(ctx context.Context, path string, fh io.ReadSeeker) (int64, error)

Write copies the content of 'fh' to 'path' using an `os.Stdout` writer.

func (*StdoutWriter) WriterURI added in v0.3.0

func (wr *StdoutWriter) WriterURI(ctx context.Context, path string) string

WriterURI returns the value of 'path'

type Writer

type Writer interface {
	// Writer copies the contents of an `io.ReadSeeker` instance to a relative path.
	// The absolute path for the file is determined by the instance implementing the `Writer` interface.
	Write(context.Context, string, io.ReadSeeker) (int64, error)
	// WriterURI returns the absolute URI for an instance implementing the `Writer` interface.
	WriterURI(context.Context, string) string
	// Close closes any underlying writing mechnisms for an instance implementing the `Writer` interface.
	Close(context.Context) error
}

Writer is an interface for writing data to multiple sources or targets.

func NewCwdWriter added in v0.7.0

func NewCwdWriter(ctx context.Context, uri string) (Writer, error)

NewCwdWriter returns a new `CwdWriter` instance for writing documents to the current working directory configured by 'uri' in the form of:

cwd://

Technically 'uri' can also be an empty string.

func NewFileWriter

func NewFileWriter(ctx context.Context, uri string) (Writer, error)

NewFileWriter returns a new `FileWriter` instance for writing documents as files on a local disk, configured by 'uri' in the form of:

fs://{PATH}

Where {PATH} is an absolute path to an existing directory where files will be written.

func NewIOWriter added in v0.2.0

func NewIOWriter(ctx context.Context, uri string) (Writer, error)

NewIOWriter returns a new `IOWriter` instance for writing documents to the current working directory configured by 'uri' in the form of:

io://

In order to assign the actual `io.Writer` instance to use you will need to call the `SetIOWriterWithContext` method and pass the resultant `context.Context` instance to the `Write` method.

func NewMultiWriter added in v0.5.0

func NewMultiWriter(writers ...Writer) Writer

NewMultiWriter returns a Writer instance that will send all writes to each instance in 'writers'. Writes happen synchronolously in the order in which the underlying Writer instances are specified.

func NewNullWriter

func NewNullWriter(ctx context.Context, uri string) (Writer, error)

NewNullWriter returns a new `CwdWriter` instance for writing documents to nowhere configured by 'uri' in the form of:

null://

Technically 'uri' can also be an empty string.

func NewRepoWriter added in v0.8.0

func NewRepoWriter(ctx context.Context, uri string) (Writer, error)

NewRepoWriter is a convenience method to update 'uri' by appending a `data` directory to its path and changing its scheme to `fs://` before invoking NewWriter with the updated URI.

func NewStdoutWriter

func NewStdoutWriter(ctx context.Context, uri string) (Writer, error)

NewStdoutWriter returns a new `CwdWriter` instance for writing documents to STDOUT configured by 'uri' in the form of:

stdout://

Technically 'uri' can also be an empty string.

func NewWriter

func NewWriter(ctx context.Context, uri string) (Writer, error)

NewWriter returns a new `Writer` instance configured by 'uri'. The value of 'uri' is parsed as a `url.URL` and its scheme is used as the key for a corresponding `WriterInitializationFunc` function used to instantiate the new `Writer`. It is assumed that the scheme (and initialization function) have been registered by the `RegisterWriter` method.

type WriterInitializationFunc added in v0.1.0

type WriterInitializationFunc func(ctx context.Context, uri string) (Writer, error)

WriterInitializationFunc is a function defined by individual writer package and used to create an instance of that writer

Jump to

Keyboard shortcuts

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