Documentation ¶
Overview ¶
Example:
package main import ( "context" "github.com/whosonfirst/go-reader" "io" "os" ) func main() { ctx := context.Background() r, _ := reader.NewReader(ctx, "fs:///usr/local/data") fh, _ := r.Read(ctx, "example.txt") defer fh.Close() io.Copy(os.Stdout, fh) }
Package reader provides a common interface for reading from a variety of sources. It has the following interface:
type Reader interface { Read(context.Context, string) (io.ReadSeekCloser, error) ReaderURI(string) string }
Reader intstances are created either by calling a package-specific New{SOME_READER}Reader method or by invoking the reader.NewReader method passing in a context.Context instance and a URI specific to the reader class. For example:
r, _ := reader.NewReader(ctx, "fs:///usr/local/data")
Custom reader packages implement the reader.Reader interface and register their availability by calling the reader.RegisterRegister method on initialization. For example:
func init() { ctx := context.Background() err = RegisterReader(ctx, "file", NewFileReader) if err != nil { panic(err) } }
Index ¶
- Constants
- func RegisterReader(ctx context.Context, scheme string, init_func ReaderInitializationFunc) error
- func Schemes() []string
- type FileReader
- type MultiReader
- type NullReader
- type Reader
- func NewFileReader(ctx context.Context, uri string) (Reader, error)
- func NewMultiReader(ctx context.Context, readers ...Reader) (Reader, error)
- func NewMultiReaderFromURIs(ctx context.Context, uris ...string) (Reader, error)
- func NewNullReader(ctx context.Context, uri string) (Reader, error)
- func NewReader(ctx context.Context, uri string) (Reader, error)
- func NewRepoReader(ctx context.Context, uri string) (Reader, error)
- func NewStdinReader(ctx context.Context, uri string) (Reader, error)
- type ReaderInitializationFunc
- type StdinReader
Constants ¶
const STDIN string = "-"
Constant string value representing STDIN.
Variables ¶
This section is empty.
Functions ¶
func RegisterReader ¶ added in v0.1.1
func RegisterReader(ctx context.Context, scheme string, init_func ReaderInitializationFunc) error
RegisterReader registers 'scheme' as a key pointing to 'init_func' in an internal lookup table used to create new `Reader` instances by the `NewReader` method.
Types ¶
type FileReader ¶
type FileReader struct { Reader // contains filtered or unexported fields }
FileReader is a struct that implements the `Reader` interface for reading documents from files on a local disk.
func (*FileReader) Read ¶
func (r *FileReader) Read(ctx context.Context, path string) (io.ReadSeekCloser, error)
Read will open an `io.ReadSeekCloser` for a file matching 'path'.
type MultiReader ¶ added in v0.2.1
type MultiReader struct { Reader // contains filtered or unexported fields }
MultiReader is a struct that implements the `Reader` interface for reading documents from one or more `Reader` instances.
func (*MultiReader) Read ¶ added in v0.2.1
func (mr *MultiReader) Read(ctx context.Context, path string) (io.ReadSeekCloser, error)
Read will open an `io.ReadSeekCloser` for a file matching 'path'. In the case of multiple underlying `Reader` instances the first instance to successfully load 'path' will be returned.
type NullReader ¶
type NullReader struct {
Reader
}
NullReader is a struct that implements the `Reader` interface for reading documents from nowhere.
func (*NullReader) Read ¶
func (r *NullReader) Read(ctx context.Context, path string) (io.ReadSeekCloser, error)
Read will open and return an empty `io.ReadSeekCloser` for any value of 'path'.
type Reader ¶
type Reader interface { // Reader returns a `io.ReadSeekCloser` instance for a URI resolved by the instance implementing the `Reader` interface. Read(context.Context, string) (io.ReadSeekCloser, error) // The absolute path for the file is determined by the instance implementing the `Reader` interface. ReaderURI(context.Context, string) string }
Reader is an interface for reading data from multiple sources or targets.
func NewFileReader ¶
NewFileReader returns a new `FileReader` instance for reading documents from local files on disk, configured by 'uri' in the form of:
fs://{PATH}
Where {PATH} is an absolute path to an existing directory where files will be read from.
func NewMultiReader ¶ added in v0.2.1
NewMultiReaderFromURIs returns a new `Reader` instance for reading documents from one or more `Reader` instances.
func NewMultiReaderFromURIs ¶ added in v0.2.1
NewMultiReaderFromURIs returns a new `Reader` instance for reading documents from one or more `Reader` instances. 'uris' is assumed to be a list of URIs each of which will be used to invoke the `NewReader` method.
func NewNullReader ¶
NewNullReader returns a new `FileReader` instance for reading documents from nowhere, configured by 'uri' in the form of:
null://
Technically 'uri' can also be an empty string.
func NewReader ¶
NewReader returns a new `Reader` 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 `ReaderInitializationFunc` function used to instantiate the new `Reader`. It is assumed that the scheme (and initialization function) have been registered by the `RegisterReader` method.
func NewRepoReader ¶ added in v0.10.0
NewRepoReader is a convenience method to update 'uri' by appending a `data` directory to its path and changing its scheme to `fs://` before invoking NewReader with the updated URI.
type ReaderInitializationFunc ¶ added in v0.1.1
ReaderInitializationFunc is a function defined by individual reader package and used to create an instance of that reader
type StdinReader ¶ added in v0.8.0
type StdinReader struct {
Reader
}
StdinReader is a struct that implements the `Reader` interface for reading documents from STDIN.
func (*StdinReader) Read ¶ added in v0.8.0
func (r *StdinReader) Read(ctx context.Context, uri string) (io.ReadSeekCloser, error)
Read will open a `io.ReadSeekCloser` instance wrapping `os.Stdin`.