Documentation ¶
Overview ¶
Package tablib is a format-agnostic tabular Dataset library, written in Go. It allows you to import, export, and manipulate tabular data sets. Advanced features include, dynamic columns, tags & filtering, and seamless format import & export.
Index ¶
- Constants
- Variables
- type ColumnConstraint
- type Databook
- func (d *Databook) AddSheet(title string, dataset *Dataset)
- func (d *Databook) HTML() *Exportable
- func (d *Databook) JSON() (*Exportable, error)
- func (d *Databook) Sheet(title string) Sheet
- func (d *Databook) Sheets() map[string]Sheet
- func (d *Databook) Size() int
- func (d *Databook) Wipe()
- func (d *Databook) XLSX() (*Exportable, error)
- func (d *Databook) XML() (*Exportable, error)
- func (d *Databook) YAML() (*Exportable, error)
- type Dataset
- func LoadCSV(input []byte) (*Dataset, error)
- func LoadJSON(jsonContent []byte) (*Dataset, error)
- func LoadTSV(input []byte) (*Dataset, error)
- func LoadXML(input []byte) (*Dataset, error)
- func LoadYAML(yamlContent []byte) (*Dataset, error)
- func NewDataset(headers []string) *Dataset
- func NewDatasetWithData(headers []string, data [][]interface{}) *Dataset
- func (d *Dataset) Append(row []interface{}) error
- func (d *Dataset) AppendColumn(header string, cols []interface{}) error
- func (d *Dataset) AppendColumnValues(header string, cols ...interface{}) error
- func (d *Dataset) AppendConstrainedColumn(header string, constraint ColumnConstraint, cols []interface{}) error
- func (d *Dataset) AppendDynamicColumn(header string, fn DynamicColumn)
- func (d *Dataset) AppendTagged(row []interface{}, tags ...string) error
- func (d *Dataset) AppendValues(row ...interface{}) error
- func (d *Dataset) AppendValuesTagged(row ...interface{}) error
- func (d *Dataset) CSV() (*Exportable, error)
- func (d *Dataset) Column(header string) []interface{}
- func (d *Dataset) ConstrainColumn(header string, constraint ColumnConstraint)
- func (d *Dataset) DeleteColumn(header string) error
- func (d *Dataset) DeleteRow(row int) error
- func (d *Dataset) Dict() []interface{}
- func (d *Dataset) Filter(tags ...string) *Dataset
- func (d *Dataset) HTML() *Exportable
- func (d *Dataset) HasAnyConstraint() bool
- func (d *Dataset) Headers() []string
- func (d *Dataset) Height() int
- func (d *Dataset) Insert(index int, row []interface{}) error
- func (d *Dataset) InsertColumn(index int, header string, cols []interface{}) error
- func (d *Dataset) InsertConstrainedColumn(index int, header string, constraint ColumnConstraint, cols []interface{}) error
- func (d *Dataset) InsertDynamicColumn(index int, header string, fn DynamicColumn) error
- func (d *Dataset) InsertTagged(index int, row []interface{}, tags ...string) error
- func (d *Dataset) InsertValues(index int, values ...interface{}) error
- func (d *Dataset) InvalidSubset() *Dataset
- func (d *Dataset) JSON() (*Exportable, error)
- func (d *Dataset) Markdown() *Exportable
- func (d *Dataset) MySQL(table string) *Exportable
- func (d *Dataset) Postgres(table string) *Exportable
- func (d *Dataset) Records() [][]string
- func (d *Dataset) Row(index int) (map[string]interface{}, error)
- func (d *Dataset) Rows(index ...int) ([]map[string]interface{}, error)
- func (d *Dataset) SetAlign(align string)
- func (d *Dataset) SetDenseMode(mode bool)
- func (d *Dataset) SetEmptyString(empty string)
- func (d *Dataset) SetFloatFormat(format byte)
- func (d *Dataset) SetMaxCellSize(max int)
- func (d *Dataset) SetSplitConcat(split string)
- func (d *Dataset) SetWrapDelimiter(delim rune)
- func (d *Dataset) SetWrapStrings(wrap bool)
- func (d *Dataset) Slice(lower, upperNonInclusive int) (*Dataset, error)
- func (d *Dataset) Sort(column string) *Dataset
- func (d *Dataset) SortReverse(column string) *Dataset
- func (d *Dataset) Stack(other *Dataset) (*Dataset, error)
- func (d *Dataset) StackColumn(other *Dataset) (*Dataset, error)
- func (d *Dataset) TSV() (*Exportable, error)
- func (d *Dataset) Tabular(format string) *Exportable
- func (d *Dataset) Tag(index int, tags ...string) error
- func (d *Dataset) Tags(index int) ([]string, error)
- func (d *Dataset) Transpose() *Dataset
- func (d *Dataset) Valid() bool
- func (d *Dataset) ValidFailFast() bool
- func (d *Dataset) ValidSubset() *Dataset
- func (d *Dataset) Width() int
- func (d *Dataset) XLSX() (*Exportable, error)
- func (d *Dataset) XML() (*Exportable, error)
- func (d *Dataset) XMLWithTagNamePrefixIndent(tagName, prefix, indent string) (*Exportable, error)
- func (d *Dataset) YAML() (*Exportable, error)
- type DynamicColumn
- type Exportable
- type Sheet
- type ValidationError
Constants ¶
const ( AlignLeft = "left" AlighMiddle = "center" AlignRight = "right" )
Variables ¶
var ( // ErrInvalidDimensions is returned when trying to append/insert too much // or not enough values to a row or column ErrInvalidDimensions = errors.New("tablib: Invalid dimension") // ErrInvalidColumnIndex is returned when trying to insert a column at an // invalid index ErrInvalidColumnIndex = errors.New("tablib: Invalid column index") // ErrInvalidRowIndex is returned when trying to insert a row at an // invalid index ErrInvalidRowIndex = errors.New("tablib: Invalid row index") // ErrInvalidDataset is returned when trying to validate a Dataset against // the constraints that have been set on its columns. ErrInvalidDataset = errors.New("tablib: Invalid dataset") // ErrInvalidTag is returned when trying to add a tag which is not a string. ErrInvalidTag = errors.New("tablib: A tag must be a string") )
var ( // TabularGrid is the value to be passed to gotabulate to render the table // as ASCII table with grid format TabularGrid = "grid" // TabularSimple is the value to be passed to gotabulate to render the table // as ASCII table with simple format TabularSimple = "simple" // TabularCondensed is the value to be passed to gotabulate to render the table // as ASCII table with condensed format TabularCondensed = "condensed" // TabularMarkdown is the value to be passed to gotabulate to render the table // as ASCII table with Markdown format TabularMarkdown = "markdown" TabularUtf8 = "utf8" TabularMickMake = "mickmake" )
Functions ¶
This section is empty.
Types ¶
type ColumnConstraint ¶
type ColumnConstraint func(interface{}) bool
ColumnConstraint represents a function that is bound as a constraint to the column so that it can validate its value
type Databook ¶
type Databook struct {
// contains filtered or unexported fields
}
Databook represents a Databook which is an array of sheets.
func LoadDatabookJSON ¶
LoadDatabookJSON loads a Databook from a JSON source.
func LoadDatabookYAML ¶
LoadDatabookYAML loads a Databook from a YAML source.
func (*Databook) HTML ¶
func (d *Databook) HTML() *Exportable
HTML returns a HTML representation of the Databook as an Exportable.
func (*Databook) JSON ¶
func (d *Databook) JSON() (*Exportable, error)
JSON returns a JSON representation of the Databook as an Exportable.
func (*Databook) Wipe ¶
func (d *Databook) Wipe()
Wipe removes all Dataset objects from the Databook.
func (*Databook) XLSX ¶
func (d *Databook) XLSX() (*Exportable, error)
XLSX returns a XLSX representation of the Databook as an exportable.
func (*Databook) XML ¶
func (d *Databook) XML() (*Exportable, error)
XML returns a XML representation of the Databook as an Exportable.
func (*Databook) YAML ¶
func (d *Databook) YAML() (*Exportable, error)
YAML returns a YAML representation of the Databook as an Exportable.
type Dataset ¶
type Dataset struct { // EmptyValue represents the string value to b output if a field cannot be // formatted as a string during output of certain formats. EmptyValue string ValidationErrors []ValidationError Align string EmptyString string FloatFormat byte MaxCellSize int WrapDelimiter rune WrapStrings bool DenseMode bool SplitConcat string // contains filtered or unexported fields }
Dataset represents a set of data, which is a list of data and header for each column.
func NewDatasetWithData ¶
NewDatasetWithData creates a new Dataset.
func (*Dataset) AppendColumn ¶
AppendColumn appends a new column with values to the Dataset.
func (*Dataset) AppendColumnValues ¶
AppendColumnValues appends a new column with values to the Dataset.
func (*Dataset) AppendConstrainedColumn ¶
func (d *Dataset) AppendConstrainedColumn(header string, constraint ColumnConstraint, cols []interface{}) error
AppendConstrainedColumn appends a constrained column to the Dataset.
func (*Dataset) AppendDynamicColumn ¶
func (d *Dataset) AppendDynamicColumn(header string, fn DynamicColumn)
AppendDynamicColumn appends a dynamic column to the Dataset.
func (*Dataset) AppendTagged ¶
AppendTagged appends a row of values to the Dataset with one or multiple tags for filtering purposes.
func (*Dataset) AppendValues ¶
AppendValues appends a row of values to the Dataset.
func (*Dataset) AppendValuesTagged ¶
AppendValuesTagged appends a row of values to the Dataset with one or multiple tags for filtering purposes.
func (*Dataset) CSV ¶
func (d *Dataset) CSV() (*Exportable, error)
CSV returns a CSV representation of the Dataset an Exportable.
func (*Dataset) Column ¶
Column returns all the values for a specific column returns nil if column is not found.
func (*Dataset) ConstrainColumn ¶
func (d *Dataset) ConstrainColumn(header string, constraint ColumnConstraint)
ConstrainColumn adds a constraint to a column in the Dataset.
func (*Dataset) DeleteColumn ¶
DeleteColumn deletes a column from the Dataset.
func (*Dataset) Dict ¶
func (d *Dataset) Dict() []interface{}
Dict returns the Dataset as an array of map where each key is a column.
func (*Dataset) Filter ¶
Filter filters a Dataset, returning a fresh Dataset including only the rows previously tagged with one of the given tags. Returns a new Dataset.
func (*Dataset) HTML ¶
func (d *Dataset) HTML() *Exportable
HTML returns the HTML representation of the Dataset as an Exportable.
func (*Dataset) HasAnyConstraint ¶
HasAnyConstraint returns whether the Dataset has any constraint set.
func (*Dataset) InsertColumn ¶
InsertColumn insert a new column at a given index.
func (*Dataset) InsertConstrainedColumn ¶
func (d *Dataset) InsertConstrainedColumn(index int, header string, constraint ColumnConstraint, cols []interface{}) error
InsertConstrainedColumn insert a new constrained column at a given index.
func (*Dataset) InsertDynamicColumn ¶
func (d *Dataset) InsertDynamicColumn(index int, header string, fn DynamicColumn) error
InsertDynamicColumn insert a new dynamic column at a given index.
func (*Dataset) InsertTagged ¶
InsertTagged inserts a row at a given index with specific tags.
func (*Dataset) InsertValues ¶
InsertValues inserts a row of values at a given index.
func (*Dataset) InvalidSubset ¶
InvalidSubset return a new Dataset containing only the rows failing to validate their constraints. If no constraints are set, it returns the same instance. Note: The returned Dataset is free of any constraints, tags are conserved.
func (*Dataset) JSON ¶
func (d *Dataset) JSON() (*Exportable, error)
JSON returns a JSON representation of the Dataset as an Exportable.
func (*Dataset) Markdown ¶
func (d *Dataset) Markdown() *Exportable
Markdown returns a Markdown table Exportable representation of the Dataset.
func (*Dataset) MySQL ¶
func (d *Dataset) MySQL(table string) *Exportable
MySQL returns a string representing a suite of MySQL commands recreating the Dataset into a table.
func (*Dataset) Postgres ¶
func (d *Dataset) Postgres(table string) *Exportable
Postgres returns a string representing a suite of Postgres commands recreating the Dataset into a table.
func (*Dataset) Records ¶
Records returns the Dataset as an array of array where each entry is a string. The first row of the returned 2d array represents the columns of the Dataset.
func (*Dataset) Row ¶
Row returns a map representing a specific row of the Dataset. returns tablib.ErrInvalidRowIndex if the row cannot be found
func (*Dataset) Rows ¶
Rows returns an array of map representing a set of specific rows of the Dataset. returns tablib.ErrInvalidRowIndex if the row cannot be found.
func (*Dataset) SetDenseMode ¶
SetDenseMode - Mirrors the gotabulate function
func (*Dataset) SetEmptyString ¶
SetEmptyString - Mirrors the gotabulate function
func (*Dataset) SetFloatFormat ¶
SetFloatFormat - Mirrors the gotabulate function
func (*Dataset) SetMaxCellSize ¶
SetMaxCellSize - Mirrors the gotabulate function
func (*Dataset) SetSplitConcat ¶
SetSplitConcat - Mirrors the gotabulate function
func (*Dataset) SetWrapDelimiter ¶
SetWrapDelimiter - Mirrors the gotabulate function
func (*Dataset) SetWrapStrings ¶
SetWrapStrings - Mirrors the gotabulate function
func (*Dataset) Slice ¶
Slice returns a new Dataset representing a slice of the orignal Dataset like a slice of an array. returns tablib.ErrInvalidRowIndex if the lower or upper bound is out of range.
func (*Dataset) SortReverse ¶
SortReverse sorts the Dataset by a specific column in reverse order. Returns a new Dataset.
func (*Dataset) Stack ¶
Stack stacks two Dataset by joining at the row level, and return new combined Dataset.
func (*Dataset) StackColumn ¶
StackColumn stacks two Dataset by joining them at the column level, and return new combined Dataset.
func (*Dataset) TSV ¶
func (d *Dataset) TSV() (*Exportable, error)
TSV returns a TSV representation of the Dataset as string.
func (*Dataset) Tabular ¶
func (d *Dataset) Tabular(format string) *Exportable
Tabular returns a tabular Exportable representation of the Dataset. format is either grid, simple, condensed or markdown.
func (*Dataset) Tag ¶
Tag tags a row at a given index with specific tags. Returns ErrInvalidRowIndex if the row does not exist.
func (*Dataset) Tags ¶
Tags returns the tags of a row at a given index. Returns ErrInvalidRowIndex if the row does not exist.
func (*Dataset) Transpose ¶
Transpose transposes a Dataset, turning rows into columns and vice versa, returning a new Dataset instance. The first row of the original instance becomes the new header row. Tags, constraints and dynamic columns are lost in the returned Dataset. TODO
func (*Dataset) Valid ¶
Valid returns whether the Dataset is valid regarding constraints that have been previously set on columns. Its behaviour is different of ValidFailFast in a sense that it will validate the whole Dataset and all the validation errors will be available by using Dataset.ValidationErrors
func (*Dataset) ValidFailFast ¶
ValidFailFast returns whether the Dataset is valid regarding constraints that have been previously set on columns.
func (*Dataset) ValidSubset ¶
ValidSubset return a new Dataset containing only the rows validating their constraints. This is similar to what Filter() does with tags, but with constraints. If no constraints are set, it returns the same instance. Note: The returned Dataset is free of any constraints, tags are conserved.
func (*Dataset) XLSX ¶
func (d *Dataset) XLSX() (*Exportable, error)
XLSX exports the Dataset as a byte array representing the .xlsx format.
func (*Dataset) XML ¶
func (d *Dataset) XML() (*Exportable, error)
XML returns a XML representation of the Dataset as an Exportable.
func (*Dataset) XMLWithTagNamePrefixIndent ¶
func (d *Dataset) XMLWithTagNamePrefixIndent(tagName, prefix, indent string) (*Exportable, error)
XMLWithTagNamePrefixIndent returns a XML representation with custom tag, prefix and indent.
func (*Dataset) YAML ¶
func (d *Dataset) YAML() (*Exportable, error)
YAML returns a YAML representation of the Dataset as an Exportable.
type DynamicColumn ¶
type DynamicColumn func([]interface{}) interface{}
DynamicColumn represents a function that can be evaluated dynamically when exporting to a predefined format.
type Exportable ¶
type Exportable struct {
// contains filtered or unexported fields
}
Exportable represents an exportable dataset, it cannot be manipulated at this point and it can just be converted to a string, []byte or written to a io.Writer. The exportable struct just holds a bytes.Buffer that is used by the tablib library to write export formats content. Real work is delegated to bytes.Buffer.
func (*Exportable) Bytes ¶
func (e *Exportable) Bytes() []byte
Bytes returns the contentes of the exported dataset as a byte array.
func (*Exportable) String ¶
func (e *Exportable) String() string
String returns the contents of the exported dataset as a string.
func (*Exportable) WriteFile ¶
func (e *Exportable) WriteFile(filename string, perm os.FileMode) error
WriteFile writes the databook or dataset content to a file named by filename. If the file does not exist, WriteFile creates it with permissions perm; otherwise WriteFile truncates it before writing.
type Sheet ¶
type Sheet struct {
// contains filtered or unexported fields
}
Sheet represents a sheet in a Databook, holding a title (if any) and a dataset.
type ValidationError ¶
ValidationError holds the position of a value in the Dataset that have failed to validate a constraint.