table

package
v1.5.2 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2022 License: MIT Imports: 3 Imported by: 1

Documentation

Overview

Package table provides the main interfaces used to manipulate tabular data. To understand why we need interfaces to process tabular data, it is useful to introduce the concepts of the physical and the logical representation of data.

The physical representation of data refers to the representation of data as text on disk, for example, in a CSV, JSON or XML file. This representation may have some type information (JSON, where the primitive types that JSON supports can be used) or not (CSV, where all data is represented in string form). In this project, those are going to be presented as packages that provide structs which implement those interfaces. For instance, csv.NewTable creates a Table which is backed up by a CSV.

The logical representation of data refers to the "ideal" representation of the data in terms of primitive types, data structures, and relations, all as defined by the specification. We could say that the specification is about the logical representation of data. That said, functions exported for data processing should deal with logic representations. That functionality is represented by interfaces in this package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Iterator

type Iterator interface {
	// Next advances the table interator to the next row, which will be available through the Cast or Row methods.
	// It returns false when the iterator stops, either by reaching the end of the table or an error.
	// After Next returns false, the Err method will return any error that ocurred during the iteration, except if it was io.EOF, Err
	// will return nil.
	// Next could automatically buffer some data, improving reading performance. It could also block, if necessary.
	Next() bool

	// Row returns the most recent row fetched by a call to Next as a newly allocated string slice
	// holding its fields.
	Row() []string

	// Err returns nil if no errors happened during iteration, or the actual error
	// otherwise.
	Err() error

	// Close frees up any resources used during the iteration process.
	Close() error
}

Iterator is an interface which provides method to interating over tabular data. It is heavly inspired by bufio.Scanner. Iterating stops unrecoverably at EOF, the first I/O error, or a token too large to fit in the buffer.

type SliceTable added in v0.1.2

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

SliceTable offers a simple table implementation backed by slices.

func FromSlices added in v0.1.2

func FromSlices(headers []string, content [][]string) *SliceTable

FromSlices creates a new SliceTable using passed-in arguments.

func (*SliceTable) Headers added in v0.1.2

func (t *SliceTable) Headers() []string

Headers returns the headers of the tabular data.

func (*SliceTable) Iter added in v0.1.2

func (t *SliceTable) Iter() (Iterator, error)

Iter provides a convenient way to iterate over table's data. The iteration process always start at the beginning of the table and is backed by a new reading process.

func (*SliceTable) ReadAll added in v0.1.2

func (t *SliceTable) ReadAll() ([][]string, error)

ReadAll reads all rows from the table and return it as strings.

func (*SliceTable) ReadColumn added in v1.1.2

func (t *SliceTable) ReadColumn(name string) ([]string, error)

ReadColumn reads a specific column from the table and return it as strings.

type StringWriter added in v0.1.2

type StringWriter struct {
	csv.Writer
	// contains filtered or unexported fields
}

StringWriter is a simple Writer implementation which is backed up by an in memory bytes.Buffer.

func NewStringWriter added in v0.1.2

func NewStringWriter() *StringWriter

NewStringWriter returns a Writer that writes CSV to a string. It exports a String() method, which returns its contents.

func (*StringWriter) String added in v0.1.2

func (s *StringWriter) String() string

String returns the content that has been written so far as raw CSV.

type Table

type Table interface {
	// Headers returns the headers of the tabular data.
	Headers() []string

	// Iter provides a convenient way to iterate over table's data.
	// The iteration process always start at the beginning of the table and
	// is backed by a new reading.
	Iter() (Iterator, error)

	// ReadAll reads all rows from the table and return it as strings.
	ReadAll() ([][]string, error)

	// ReadColumn reads a specific column from the table and return it as strings.
	ReadColumn(string) ([]string, error)
}

Table provides functionality to iterate and write tabular data. This is the logical representation and is meant to be encoding/format agnostic.

type Writer added in v0.1.2

type Writer interface {
	// Write writes a single row to w along with any necessary quoting.
	// A record is a slice of strings with each string being one field.
	Write(record []string) error
	// Flush writes any buffered data to the underlying io.Writer.
	// To check if an error occurred during the Flush, call Error.
	Flush()
	// Error reports any error that has occurred during a previous Write or Flush.
	Error() error
	// WriteAll writes multiple CSV records to w using Write and then calls Flush.
	WriteAll(records [][]string) error
}

A Writer writes rows to a table file. The idea behind the writer is to abstract out the physical representation of the table. Which can have many formats, for instance, CSV, XML and JSON

Jump to

Keyboard shortcuts

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