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
- Variables
- func ReaderSchemes() []string
- func RegisterReader(ctx context.Context, scheme string, init_func ReaderInitializationFunc) error
- type FileReader
- type HTTPReader
- type MultiReader
- type NullReader
- type Reader
- func NewCwdReader(ctx context.Context, uri string) (Reader, error)
- func NewFileReader(ctx context.Context, uri string) (Reader, error)
- func NewHTTPReader(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 NewSQLReader(ctx context.Context, uri string) (Reader, error)
- func NewStdinReader(ctx context.Context, uri string) (Reader, error)
- type ReaderInitializationFunc
- type SQLReader
- type StdinReader
Constants ¶
const STDIN string = "-"
Constant string value representing STDIN.
Variables ¶
var URI_QUERYFUNC queryFunc
URI_QUERYFUNC is a custom function to convert paths passed to the `Read` or `Exists` methods to query condintions used to perform record searches. The default is nil.
var URI_READFUNC readFunc
URI_READFUNC is a custom function to convert paths passed to the `Read` or `Exists` methods to values stored in the underlying database's "ID" column. The default is nil.
var VALID_BODY *regexp.Regexp
VALID_BODY is a `regexp.Regexp` for validating "body" column names. The default is `^[a-zA-Z0-9-_]+$`.
var VALID_ID *regexp.Regexp
VALID_ID is a `regexp.Regexp` for validating "ID" column names. The default is `^[a-zA-Z0-9-_]+$`.
var VALID_TABLE *regexp.Regexp
VALID_TABLE is a `regexp.Regexp` for validating table names. The default is `^[a-zA-Z0-9-_]+$`.
Functions ¶
func ReaderSchemes ¶
func ReaderSchemes() []string
ReaderSchemes returns the list of schemes that have been registered.
func RegisterReader ¶
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) Exists ¶
Exists returns a boolean value indicating whether 'path' already exists.
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 HTTPReader ¶
type HTTPReader struct { Reader // contains filtered or unexported fields }
HTTPReader is a struct that implements the `Reader` interface for reading documents from an HTTP(S) resource.
func (*HTTPReader) Exists ¶
Exists returns a boolean value indicating whether 'path' already exists.
func (*HTTPReader) Read ¶
func (r *HTTPReader) Read(ctx context.Context, uri string) (io.ReadSeekCloser, error)
Read will open a `io.ReadSeekCloser` for the resource located at 'uri'.
type MultiReader ¶
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) Exists ¶
Exists returns a boolean value indicating whether 'path' already exists.
func (*MultiReader) Read ¶
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) Exists ¶
Exists returns a boolean value indicating whether 'path' already exists (meaning it will always return false).
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) // Exists returns a boolean value indicating whether a URI already exists. Exists(context.Context, string) (bool, 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 NewCwdReader ¶
NewFileReader returns a new `FileReader` instance for reading documents from the current working directory, configured by 'uri' in the form of:
cwd://
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 NewHTTPReader ¶
NewStdinReader returns a new `Reader` instance for reading documents from an HTTP(s) resource, configured by 'uri' in the form of:
http(s)://{HOST}?{PARAMS}
Where {PARAMS} can be: * user-agent - An optional user agent string to include with requests.
func NewMultiReader ¶
NewMultiReaderFromURIs returns a new `Reader` instance for reading documents from one or more `Reader` instances.
func NewMultiReaderFromURIs ¶
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 ¶
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.
func NewSQLReader ¶
NewSQLReader returns a new `SQLReader` instance for reading documents from from a `database/sql` compatible database engine configured by 'uri' in the form of:
sql://{ENGINE}/{TABLE}/{ID_COLUMN}/{BODY_COLUMN}?dsn={DSN}
For example:
sql://sqlite/geojson/id/body?dsn=test.db
The expectation is that `{TABLE}` will have a `{BODY_COLUMN}` column containing a Who's On First record which can be retrieved with a unique identifer defined in the `{ID_COLUMN}` column.
type ReaderInitializationFunc ¶
ReaderInitializationFunc is a function defined by individual reader package and used to create an instance of that reader
type SQLReader ¶
type SQLReader struct { Reader // contains filtered or unexported fields }
SQLReader is a struct that implements the `Reader` interface for reading documents from a `database/sql` compatible database engine.
func (*SQLReader) Exists ¶
Exists returns a boolean value indicating whether 'path' already exists (meaning it will always return false). Read will open a `io.ReadSeekCloser` instance for the record whose "ID" column matches 'raw_uri'. See notes about `URI_READFUNC` and `URI_QUERYFUNC` for modifying, or deriving query criteria from, 'raw_uri' before database queries are performed.
type StdinReader ¶
type StdinReader struct {
Reader
}
StdinReader is a struct that implements the `Reader` interface for reading documents from STDIN.
func (*StdinReader) Exists ¶
Exists returns a boolean value indicating whether 'path' already exists (meaning it will always return false).
func (*StdinReader) Read ¶
func (r *StdinReader) Read(ctx context.Context, uri string) (io.ReadSeekCloser, error)
Read will open a `io.ReadSeekCloser` instance wrapping `os.Stdin`.