go-tablib: github.com/agrison/go-tablib Index | Files

package tablib

import "github.com/agrison/go-tablib"

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

Package Files

tablib_csv.go tablib_databook.go tablib_dataset.go tablib_errors.go tablib_exportable.go tablib_html.go tablib_json.go tablib_sort.go tablib_sql.go tablib_tabular.go tablib_util.go tablib_xlsx.go tablib_xml.go tablib_yaml.go

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"
)

type ColumnConstraint Uses

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 Uses

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

Databook represents a Databook which is an array of sheets.

func LoadDatabookJSON Uses

func LoadDatabookJSON(jsonContent []byte) (*Databook, error)

LoadDatabookJSON loads a Databook from a JSON source.

func LoadDatabookYAML Uses

func LoadDatabookYAML(yamlContent []byte) (*Databook, error)

LoadDatabookYAML loads a Databook from a YAML source.

func NewDatabook Uses

func NewDatabook() *Databook

NewDatabook constructs a new Databook.

func (*Databook) AddSheet Uses

func (d *Databook) AddSheet(title string, dataset *Dataset)

AddSheet adds a sheet to the Databook.

func (*Databook) HTML Uses

func (d *Databook) HTML() *Exportable

HTML returns a HTML representation of the Databook as an Exportable.

func (*Databook) JSON Uses

func (d *Databook) JSON() (*Exportable, error)

JSON returns a JSON representation of the Databook as an Exportable.

func (*Databook) Sheet Uses

func (d *Databook) Sheet(title string) Sheet

Sheet returns the sheet with a specific title.

func (*Databook) Sheets Uses

func (d *Databook) Sheets() map[string]Sheet

Sheets returns the sheets in the Databook.

func (*Databook) Size Uses

func (d *Databook) Size() int

Size returns the number of sheets in the Databook.

func (*Databook) Wipe Uses

func (d *Databook) Wipe()

Wipe removes all Dataset objects from the Databook.

func (*Databook) XLSX Uses

func (d *Databook) XLSX() (*Exportable, error)

XLSX returns a XLSX representation of the Databook as an exportable.

func (*Databook) XML Uses

func (d *Databook) XML() (*Exportable, error)

XML returns a XML representation of the Databook as an Exportable.

func (*Databook) YAML Uses

func (d *Databook) YAML() (*Exportable, error)

YAML returns a YAML representation of the Databook as an Exportable.

type Dataset Uses

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
    // contains filtered or unexported fields
}

Dataset represents a set of data, which is a list of data and header for each column.

func LoadCSV Uses

func LoadCSV(input []byte) (*Dataset, error)

LoadCSV loads a Dataset by its CSV representation.

func LoadJSON Uses

func LoadJSON(jsonContent []byte) (*Dataset, error)

LoadJSON loads a dataset from a YAML source.

func LoadTSV Uses

func LoadTSV(input []byte) (*Dataset, error)

LoadTSV loads a Dataset by its TSV representation.

func LoadXML Uses

func LoadXML(input []byte) (*Dataset, error)

LoadXML loads a Dataset from an XML source.

func LoadYAML Uses

func LoadYAML(yamlContent []byte) (*Dataset, error)

LoadYAML loads a dataset from a YAML source.

func NewDataset Uses

func NewDataset(headers []string) *Dataset

NewDataset creates a new Dataset.

func NewDatasetWithData Uses

func NewDatasetWithData(headers []string, data [][]interface{}) *Dataset

NewDatasetWithData creates a new Dataset.

func (*Dataset) Append Uses

func (d *Dataset) Append(row []interface{}) error

Append appends a row of values to the Dataset.

func (*Dataset) AppendColumn Uses

func (d *Dataset) AppendColumn(header string, cols []interface{}) error

AppendColumn appends a new column with values to the Dataset.

func (*Dataset) AppendColumnValues Uses

func (d *Dataset) AppendColumnValues(header string, cols ...interface{}) error

AppendColumnValues appends a new column with values to the Dataset.

func (*Dataset) AppendConstrainedColumn Uses

func (d *Dataset) AppendConstrainedColumn(header string, constraint ColumnConstraint, cols []interface{}) error

AppendConstrainedColumn appends a constrained column to the Dataset.

func (*Dataset) AppendDynamicColumn Uses

func (d *Dataset) AppendDynamicColumn(header string, fn DynamicColumn)

AppendDynamicColumn appends a dynamic column to the Dataset.

func (*Dataset) AppendTagged Uses

func (d *Dataset) AppendTagged(row []interface{}, tags ...string) error

AppendTagged appends a row of values to the Dataset with one or multiple tags for filtering purposes.

func (*Dataset) AppendValues Uses

func (d *Dataset) AppendValues(row ...interface{}) error

AppendValues appends a row of values to the Dataset.

func (*Dataset) AppendValuesTagged Uses

func (d *Dataset) AppendValuesTagged(row ...interface{}) error

AppendValuesTagged appends a row of values to the Dataset with one or multiple tags for filtering purposes.

func (*Dataset) CSV Uses

func (d *Dataset) CSV() (*Exportable, error)

CSV returns a CSV representation of the Dataset an Exportable.

func (*Dataset) Column Uses

func (d *Dataset) Column(header string) []interface{}

Column returns all the values for a specific column returns nil if column is not found.

func (*Dataset) ConstrainColumn Uses

func (d *Dataset) ConstrainColumn(header string, constraint ColumnConstraint)

ConstrainColumn adds a constraint to a column in the Dataset.

func (*Dataset) DeleteColumn Uses

func (d *Dataset) DeleteColumn(header string) error

DeleteColumn deletes a column from the Dataset.

func (*Dataset) DeleteRow Uses

func (d *Dataset) DeleteRow(row int) error

DeleteRow deletes a row at a specific index

func (*Dataset) Dict Uses

func (d *Dataset) Dict() []interface{}

Dict returns the Dataset as an array of map where each key is a column.

func (*Dataset) Filter Uses

func (d *Dataset) Filter(tags ...string) *Dataset

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 Uses

func (d *Dataset) HTML() *Exportable

HTML returns the HTML representation of the Dataset as an Exportable.

func (*Dataset) HasAnyConstraint Uses

func (d *Dataset) HasAnyConstraint() bool

HasAnyConstraint returns whether the Dataset has any constraint set.

func (*Dataset) Headers Uses

func (d *Dataset) Headers() []string

Headers return the headers of the Dataset.

func (*Dataset) Height Uses

func (d *Dataset) Height() int

Height returns the number of rows in the Dataset.

func (*Dataset) Insert Uses

func (d *Dataset) Insert(index int, row []interface{}) error

Insert inserts a row at a given index.

func (*Dataset) InsertColumn Uses

func (d *Dataset) InsertColumn(index int, header string, cols []interface{}) error

InsertColumn insert a new column at a given index.

func (*Dataset) InsertConstrainedColumn Uses

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 Uses

func (d *Dataset) InsertDynamicColumn(index int, header string, fn DynamicColumn) error

InsertDynamicColumn insert a new dynamic column at a given index.

func (*Dataset) InsertTagged Uses

func (d *Dataset) InsertTagged(index int, row []interface{}, tags ...string) error

InsertTagged inserts a row at a given index with specific tags.

func (*Dataset) InsertValues Uses

func (d *Dataset) InsertValues(index int, values ...interface{}) error

InsertValues inserts a row of values at a given index.

func (*Dataset) InvalidSubset Uses

func (d *Dataset) InvalidSubset() *Dataset

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 Uses

func (d *Dataset) JSON() (*Exportable, error)

JSON returns a JSON representation of the Dataset as an Exportable.

func (*Dataset) Markdown Uses

func (d *Dataset) Markdown() *Exportable

Markdown returns a Markdown table Exportable representation of the Dataset.

func (*Dataset) MySQL Uses

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 Uses

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 Uses

func (d *Dataset) Records() [][]string

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 Uses

func (d *Dataset) Row(index int) (map[string]interface{}, error)

Row returns a map representing a specific row of the Dataset. returns tablib.ErrInvalidRowIndex if the row cannot be found

func (*Dataset) Rows Uses

func (d *Dataset) Rows(index ...int) ([]map[string]interface{}, error)

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) Slice Uses

func (d *Dataset) Slice(lower, upperNonInclusive int) (*Dataset, error)

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) Sort Uses

func (d *Dataset) Sort(column string) *Dataset

Sort sorts the Dataset by a specific column. Returns a new Dataset.

func (*Dataset) SortReverse Uses

func (d *Dataset) SortReverse(column string) *Dataset

SortReverse sorts the Dataset by a specific column in reverse order. Returns a new Dataset.

func (*Dataset) Stack Uses

func (d *Dataset) Stack(other *Dataset) (*Dataset, error)

Stack stacks two Dataset by joining at the row level, and return new combined Dataset.

func (*Dataset) StackColumn Uses

func (d *Dataset) StackColumn(other *Dataset) (*Dataset, error)

StackColumn stacks two Dataset by joining them at the column level, and return new combined Dataset.

func (*Dataset) TSV Uses

func (d *Dataset) TSV() (*Exportable, error)

TSV returns a TSV representation of the Dataset as string.

func (*Dataset) Tabular Uses

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 Uses

func (d *Dataset) Tag(index int, tags ...string) error

Tag tags a row at a given index with specific tags. Returns ErrInvalidRowIndex if the row does not exist.

func (*Dataset) Tags Uses

func (d *Dataset) Tags(index int) ([]string, error)

Tags returns the tags of a row at a given index. Returns ErrInvalidRowIndex if the row does not exist.

func (*Dataset) Transpose Uses

func (d *Dataset) Transpose() *Dataset

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 Uses

func (d *Dataset) Valid() bool

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 Uses

func (d *Dataset) ValidFailFast() bool

ValidFailFast returns whether the Dataset is valid regarding constraints that have been previously set on columns.

func (*Dataset) ValidSubset Uses

func (d *Dataset) ValidSubset() *Dataset

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) Width Uses

func (d *Dataset) Width() int

Width returns the number of columns in the Dataset.

func (*Dataset) XLSX Uses

func (d *Dataset) XLSX() (*Exportable, error)

XLSX exports the Dataset as a byte array representing the .xlsx format.

func (*Dataset) XML Uses

func (d *Dataset) XML() (*Exportable, error)

XML returns a XML representation of the Dataset as an Exportable.

func (*Dataset) XMLWithTagNamePrefixIndent Uses

func (d *Dataset) XMLWithTagNamePrefixIndent(tagName, prefix, indent string) (*Exportable, error)

XMLWithTagNamePrefixIndent returns a XML representation with custom tag, prefix and indent.

func (*Dataset) YAML Uses

func (d *Dataset) YAML() (*Exportable, error)

YAML returns a YAML representation of the Dataset as an Exportable.

type DynamicColumn Uses

type DynamicColumn func([]interface{}) interface{}

DynamicColumn represents a function that can be evaluated dynamically when exporting to a predefined format.

type Exportable Uses

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 Uses

func (e *Exportable) Bytes() []byte

Bytes returns the contentes of the exported dataset as a byte array.

func (*Exportable) String Uses

func (e *Exportable) String() string

String returns the contents of the exported dataset as a string.

func (*Exportable) WriteFile Uses

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.

func (*Exportable) WriteTo Uses

func (e *Exportable) WriteTo(w io.Writer) (int64, error)

WriteTo writes the exported dataset to w.

type Sheet Uses

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

Sheet represents a sheet in a Databook, holding a title (if any) and a dataset.

func (Sheet) Dataset Uses

func (s Sheet) Dataset() *Dataset

Dataset returns the dataset of the sheet.

func (Sheet) Title Uses

func (s Sheet) Title() string

Title return the title of the sheet.

type ValidationError Uses

type ValidationError struct {
    Row    int
    Column int
}

ValidationError holds the position of a value in the Dataset that have failed to validate a constraint.

Package tablib imports 18 packages (graph) and is imported by 5 packages. Updated 2016-07-19. Refresh now. Tools for package owners.