reader

package
v0.0.0-...-cf8d022 Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2026 License: MIT Imports: 25 Imported by: 0

Documentation

Overview

Package reader loads tabular data files into frames. Each supported file format registers itself in the format registry via init().

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsURL

func IsURL(s string) bool

IsURL reports whether s looks like an http(s) URL.

func Register

func Register(f Format, r Reader)

Register installs a reader for a format; called from init() in format files.

func SupportedFormats

func SupportedFormats() []string

SupportedFormats lists registered format names, sorted.

Types

type Format

type Format string

Format names a supported input format.

const (
	FormatDSV      Format = "dsv"
	FormatCSV      Format = "csv"
	FormatTSV      Format = "tsv"
	FormatJSON     Format = "json"
	FormatJSONL    Format = "jsonl"
	FormatParquet  Format = "parquet"
	FormatFWF      Format = "fwf"
	FormatSQLite   Format = "sqlite"
	FormatExcel    Format = "excel"
	FormatLogfmt   Format = "logfmt"
	FormatMarkdown Format = "markdown"
	FormatHTML     Format = "html"
)

func Detect

func Detect(path string) Format

Detect guesses the format from a file name; empty string when unknown. For URLs the query string and fragment are ignored, so presigned or tokenized links like "https://host/data.csv?token=..." still detect.

type InferMode

type InferMode string

InferMode controls schema/type inference while parsing.

const (
	InferNo   InferMode = "no"   // everything stays string
	InferFast InferMode = "fast" // sample first 128 rows
	InferSafe InferMode = "safe" // full scan
)

type NamedFrame

type NamedFrame struct {
	Name  string // tab title, e.g. file stem or sheet/table name
	Frame *data.Frame
}

NamedFrame is one table loaded from a source; multi-table sources (spreadsheets, database files) produce several.

type Options

type Options struct {
	Format          Format
	Separator       rune      // dsv field separator (default ',')
	Quote           rune      // dsv quote char (default '"')
	NoHeader        bool      // first row is data, synthesize column names
	IgnoreErrors    bool      // drop unparsable rows instead of failing
	InferSchema     InferMode // default InferSafe
	InferTypes      []string  // subset of: int float boolean date datetime, or "all"
	TruncateRagged  bool      // truncate rows longer than the header
	Widths          []int     // fwf column widths ([] = auto-detect)
	SeparatorLength int       // fwf separator width (default 1)
	FlexibleWidth   bool      // fwf: allow last column to overflow
	Key             string    // decryption key for encrypted database files
}

Options carries all parsing knobs; format readers pick what applies.

func DefaultOptions

func DefaultOptions() Options

DefaultOptions returns the option defaults shared by all formats.

type Reader

type Reader interface {
	// Read consumes the source and returns one or more frames.
	Read(src *Source, opt Options) ([]NamedFrame, error)
}

Reader parses one format.

func For

func For(f Format) (Reader, error)

For returns the reader registered for a format.

type Source

type Source struct {
	// Path is the original user-supplied location (file path or URL).
	Path string
	// LocalPath is the on-disk file to read. For plain files it equals Path;
	// for stdin and URLs it points at a temporary spool file.
	LocalPath string
	// Stdin marks input piped from standard input ("-").
	Stdin bool
	// URL marks input downloaded from http(s).
	URL bool
}

Source is one input to load: a local file, standard input, or a URL that has already been downloaded to a temporary file.

func FromFile

func FromFile(path string) (*Source, error)

FromFile builds a Source for a local file path.

func FromStdin

func FromStdin() (*Source, error)

FromStdin spools standard input to a temporary file.

func FromURL

func FromURL(rawurl string) (*Source, error)

FromURL downloads an http(s) URL to a temporary file and wraps it in a Source. Redirects are followed; the request carries a plain user agent.

func (*Source) Bytes

func (s *Source) Bytes() ([]byte, error)

Bytes reads the whole source into memory.

func (*Source) Cleanup

func (s *Source) Cleanup()

Cleanup removes the temporary spool file backing a stdin or URL source. It is a no-op for plain local files. Safe to call once every reader is done: readers fully materialize their frames before returning.

func (*Source) Name

func (s *Source) Name() string

Name returns a short display name for tab titles.

func (*Source) Open

func (s *Source) Open() (io.ReadCloser, error)

Open opens the underlying local file.

Jump to

Keyboard shortcuts

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