tabular

package module
Version: v0.0.0-...-1646335 Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2020 License: MIT Imports: 12 Imported by: 0

README

tabular GoDoc Build Status Coverage Status

Tabular data generator written in Go.

Install

go get github.com/jbub/tabular

Docs

http://godoc.org/github.com/jbub/tabular

Example

package main

import (
    "log"
    "os"

    "github.com/jbub/tabular"
)

func main() {
    d := tabular.NewDataSet()
    d.AddHeader("firstname", "First name")
    d.AddHeader("lastname", "Last name")

    r1 := tabular.NewRow("Julia", "Roberts")
    r2 := tabular.NewRow("John", "Malkovich")
    err := d.Append(r1, r2)
    if err != nil {
        log.Fatal(err)
    }

    opts := &tabular.CSVOpts{
        Comma:   ';',
        UseCRLF: true,
    }
    csv := tabular.NewCSVWriter(opts)

    err = d.Write(csv, os.Stdout)
    if err != nil {
        log.Fatal(err)
    }
}

CSV

opts := &tabular.CSVOpts{
    Comma:   ';',
    UseCRLF: true,
}
csvw := tabular.NewCSVWriter(opts)
Output
Firstname;Lastname;Age
Julia;Roberts;40
John;Malkovich;42

HTML

opts := &tabular.HTMLOpts{
    Caption:    "Popular people",
    Indent:     2,
    TableClass: "",
    HeadClass:  "",
    RowClass:   "",
    DataClass:  "",
}
htmlw := tabular.NewHTMLWriter(opts)
Output
<table>
  <caption>Popular people</caption>
  <thead>
    <tr>
      <th>Firstname</th>
      <th>Lastname</th>
      <th>Age</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Julia</td>
      <td>Roberts</td>
      <td>40</td>
    </tr>
    <tr>
      <td>John</td>
      <td>Malkovich</td>
      <td>42</td>
    </tr>
  </tbody>
</table>

JSON

opts := &tabular.JSONOpts{
    Indent: 4,
}
jsonw := tabular.NewJSONWriter(opts)
Output
[
    {
        "firstname": "Julia",
        "lastname": "Roberts",
        "age": "40"
    },
    {
        "firstname": "John",
        "lastname": "Malkovich",
        "age": "42"
    }
]

LaTeX

opts := &tabular.LatexOpts{
  Caption:  "",
  Center:   "",
  TabularX: false,
}
latexw := tabular.NewLatexWriter(opts)
Output
\begin{table}[h]
\begin{tabular}{|l|l|l|}
\hline
Firstname & Lastname  & Age \\ \hline
Julia     & Roberts   & 40  \\ \hline
John      & Malkovich & 42  \\ \hline
\end{tabular}
\end{table}

XML

opts := &tabular.XMLOpts{
    Indent:     4,
    RowElem:    "row",
    ParentElem: "rows",
}
xmlw := tabular.NewXMLWriter(opts)
Output
<rows>
    <row>
        <firstname>Julia</firstname>
        <lastname>Roberts</lastname>
        <age>40</age>
    </row>
    <row>
        <firstname>John</firstname>
        <lastname>Malkovich</lastname>
        <age>42</age>
    </row>
</rows>

YAML

opts := &tabular.YAMLOpts{}
yamlw := tabular.NewYAMLWriter(opts)
Output
- firstname: Julia
  lastname: Roberts
  age: 40
- firstname: John
  lastname: Malkovich
  age: 42

SQL

db, _ := sql.Open("postgres", "postgres://localhost/mydb?sslmode=disable")
opts := &tabular.SQLOpts{
    Driver: "postgres",
    DB:     db,
    Table: "my_table",
}
sqlw := tabular.NewSQLWriter(opts)
Output

SQL queries are performed in transaction, these are example queries:

BEGIN
INSERT INTO my_table (firstname,lastname,age) VALUES ($1,$2,$3)
INSERT INTO my_table (firstname,lastname,age) VALUES ($1,$2,$3)
COMMIT

Documentation

Overview

Package tabular implements a tabular data generator.

package main

import (
    "log"
    "os"

    "github.com/jbub/tabular"
)

func main() {
    set := tabular.NewDataSet()
    set.AddHeader("firstname", "First name")
    set.AddHeader("lastname", "Last name")

    r1 := tabular.NewRow("Julia", "Roberts")
    r2 := tabular.NewRow("John", "Malkovich")
    err := set.Append(r1, r2)
    if err != nil {
        log.Fatal(err)
    }

    opts := &tabular.CSVOpts{
        Comma: ';',
        UseCRLF: true,
    }
    csv := tabular.NewCSVWriter(opts)

    err = set.Write(csv, os.Stdout)
    if err != nil {
        log.Fatal(err)
    }
}

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrEmptyDataset is returned when operations are applied to empty dataset.
	ErrEmptyDataset = errors.New("dataset is empty")
)

Functions

This section is empty.

Types

type CSVOpts

type CSVOpts struct {
	Comma   rune
	UseCRLF bool
}

CSVOpts represents options passed to the CSV writer.

type CSVWriter

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

CSVWriter represents a CSV dataset writer.

func NewCSVWriter

func NewCSVWriter(opts *CSVOpts) *CSVWriter

NewCSVWriter creates a new CSV dataset writer.

func (*CSVWriter) Name

func (wc *CSVWriter) Name() string

Name returns name of the writer.

func (*CSVWriter) NeedsHeaders

func (wc *CSVWriter) NeedsHeaders() bool

NeedsHeaders returns true if headers are required.

func (*CSVWriter) Write

func (wc *CSVWriter) Write(d *Dataset, w io.Writer) error

Write writes dataset to writer.

type Dataset

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

Dataset represents a set of data.

func NewDataSet

func NewDataSet() *Dataset

NewDataSet creates new dataset.

func (*Dataset) AddHeader

func (d *Dataset) AddHeader(key string, title string)

AddHeader adds new header.

func (*Dataset) Append

func (d *Dataset) Append(rows ...*Row) error

Append appends new rows to the dataset.

func (*Dataset) Find

func (d *Dataset) Find(tag string) *Dataset

Find filters dataset for tag.

func (*Dataset) FindAll

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

FindAll filters dataset for all tags.

func (*Dataset) FindAny

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

FindAny filters dataset for any tags.

func (*Dataset) Get

func (d *Dataset) Get(idx int) (*Row, bool)

Get returns a row on given index.

func (*Dataset) GetColValues

func (d *Dataset) GetColValues(key string) []string

GetColValues returns values of given column.

func (*Dataset) GetHeader

func (d *Dataset) GetHeader(idx int) (*Header, bool)

GetHeader returns header by its index.

func (*Dataset) GetIdxWidth

func (d *Dataset) GetIdxWidth(idx int) int

GetIdxWidth returns maximum column width.

func (*Dataset) GetKeyWidth

func (d *Dataset) GetKeyWidth(key string) int

GetKeyWidth returns maximum column width.

func (*Dataset) HasCol

func (d *Dataset) HasCol(key string) bool

HasCol checks the presence of given column.

func (*Dataset) HasHeaders

func (d *Dataset) HasHeaders() bool

HasHeaders returns true if headers was set.

func (*Dataset) HeaderCount

func (d *Dataset) HeaderCount() int

HeaderCount returns header count.

func (*Dataset) Headers

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

Headers returns a slice of headers.

func (*Dataset) Len

func (d *Dataset) Len() int

Len returns the row count.

func (*Dataset) Rows

func (d *Dataset) Rows() []*Row

Rows returns all rows of dataset.

func (*Dataset) Slice

func (d *Dataset) Slice(start int, end int) *Dataset

Slice returns sliced dataset.

func (*Dataset) Sort

func (d *Dataset) Sort(key string, reverse bool) *Dataset

Sort sorts dataset by key, set reverse to inverse the order direction.

func (*Dataset) Write

func (d *Dataset) Write(dw Writer, w io.Writer) error

Write writes dataset using dataset writer to writer.

type ErrHeadersRequired

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

ErrHeadersRequired is error returned when writer is missing the headers.

func (ErrHeadersRequired) Error

func (e ErrHeadersRequired) Error() string

type ErrInvalidHeaderIndex

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

ErrInvalidHeaderIndex is error returned when writer is missing the headers.

func (ErrInvalidHeaderIndex) Error

func (e ErrInvalidHeaderIndex) Error() string

type ErrInvalidRowWidth

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

ErrInvalidRowWidth is error returned when adding new row with invalid width.

func (ErrInvalidRowWidth) Error

func (e ErrInvalidRowWidth) Error() string

type HTMLOpts

type HTMLOpts struct {
	Caption string
	Indent  int

	TableClass string
	RowClass   string
	HeadClass  string
	DataClass  string
}

HTMLOpts represents options passed to the HTML writer.

type HTMLWriter

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

HTMLWriter represents a HTML dataset writer.

func NewHTMLWriter

func NewHTMLWriter(opts *HTMLOpts) *HTMLWriter

NewHTMLWriter creates a new HTML dataset writer.

func (*HTMLWriter) Name

func (wh *HTMLWriter) Name() string

Name returns name of the writer.

func (*HTMLWriter) NeedsHeaders

func (wh *HTMLWriter) NeedsHeaders() bool

NeedsHeaders returns true if headers are required.

func (*HTMLWriter) Write

func (wh *HTMLWriter) Write(d *Dataset, w io.Writer) error

Write writes dataset to writer.

type Header struct {
	Key   string
	Title string
}

Header represents dataset header.

type Headers

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

Headers represents dataset headers.

func (*Headers) Add

func (h *Headers) Add(key string, title string)

Add appends a new header.

func (*Headers) Empty

func (h *Headers) Empty() bool

Empty checks emptiness of headers.

func (*Headers) Get

func (h *Headers) Get(idx int) (*Header, bool)

Get returns header on given index.

func (*Headers) Items

func (h *Headers) Items() []*Header

Items returns slice of headers.

func (*Headers) Len

func (h *Headers) Len() int

Len returns header count.

type JSONOpts

type JSONOpts struct {
	Indent int
}

JSONOpts represents options passed to the JSON writer.

type JSONWriter

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

JSONWriter represents a JSON dataset writer.

func NewJSONWriter

func NewJSONWriter(opts *JSONOpts) *JSONWriter

NewJSONWriter creates a new JSON dataset writer.

func (*JSONWriter) Name

func (wj *JSONWriter) Name() string

Name returns name of the writer.

func (*JSONWriter) NeedsHeaders

func (wj *JSONWriter) NeedsHeaders() bool

NeedsHeaders returns true if headers are required.

func (*JSONWriter) Write

func (wj *JSONWriter) Write(d *Dataset, w io.Writer) error

Write writes dataset to writer.

type LatexOpts

type LatexOpts struct {
	Caption  string
	Center   bool
	TabularX bool
}

LatexOpts represents options passed to the LaTeX writer.

type LatexWriter

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

LatexWriter represents a LaTeX dataset writer.

func NewLatexWriter

func NewLatexWriter(opts *LatexOpts) *LatexWriter

NewLatexWriter creates a new LaTeX dataset writer.

func (*LatexWriter) Name

func (wl *LatexWriter) Name() string

Name returns name of the writer.

func (*LatexWriter) NeedsHeaders

func (wl *LatexWriter) NeedsHeaders() bool

NeedsHeaders returns true if headers are required.

func (*LatexWriter) Write

func (wl *LatexWriter) Write(d *Dataset, w io.Writer) error

Write writes dataset to writer.

type Row

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

Row represents a row of dataset.

func NewRow

func NewRow(items ...string) *Row

NewRow creates new row with optional items.

func NewRowFromSlice

func NewRowFromSlice(items []string) *Row

NewRowFromSlice creates new row from slice of items.

func (*Row) Add

func (r *Row) Add(items ...string)

Add appends new items to the row.

func (*Row) AddTag

func (r *Row) AddTag(tag string) bool

AddTag append new tag to the row.

func (*Row) Get

func (r *Row) Get(idx int) string

Get returns tow item on given index .

func (*Row) HasAllTags

func (r *Row) HasAllTags(tags ...string) bool

HasAllTags checks for presence of all tags in the row.

func (*Row) HasAnyTags

func (r *Row) HasAnyTags(tags ...string) bool

HasAnyTags checks for presence of any tags in the row.

func (*Row) HasTag

func (r *Row) HasTag(tag string) bool

HasTag checks for tag presence of row.

func (*Row) Items

func (r *Row) Items() []string

Items returns slice of row items.

func (*Row) Len

func (r *Row) Len() int

Len returns row item count.

func (*Row) Tags

func (r *Row) Tags() []string

Tags returns slice of row tags.

type RowSorter

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

RowSorter implements a row sorter.

func (RowSorter) Len

func (r RowSorter) Len() int

Len returns the row count.

func (RowSorter) Less

func (r RowSorter) Less(i, j int) bool

Less compares two rows.

func (RowSorter) Sort

func (r RowSorter) Sort() []*Row

Sort sorts rows and returns sorted slice.

func (RowSorter) Swap

func (r RowSorter) Swap(i, j int)

Swap swaps two rows.

type SQLOpts

type SQLOpts struct {
	DB         *sql.DB
	Driver     string
	Table      string
	ColMapping map[string]string
}

SQLOpts represents options passed to the SQL writer.

type SQLWriter

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

SQLWriter represents a SQL dataset writer.

func NewSQLWriter

func NewSQLWriter(opts *SQLOpts) *SQLWriter

NewSQLWriter creates a new SQL dataset writer.

func (*SQLWriter) Name

func (sw *SQLWriter) Name() string

Name returns name of the writer.

func (*SQLWriter) NeedsHeaders

func (sw *SQLWriter) NeedsHeaders() bool

NeedsHeaders returns true if headers are required.

func (*SQLWriter) Write

func (sw *SQLWriter) Write(d *Dataset, w io.Writer) error

Write writes dataset to writer.

type SetTagger

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

SetTagger implements a Tagger using string map set.

func (*SetTagger) Add

func (t *SetTagger) Add(tag string) bool

Add adds new tag. Returns true when new tag was added, false otherwise.

func (*SetTagger) Has

func (t *SetTagger) Has(tag string) bool

Has checks if tag in present in the set.

func (*SetTagger) HasAll

func (t *SetTagger) HasAll(tags ...string) bool

HasAll checks if set has all of the tags.

func (*SetTagger) HasAny

func (t *SetTagger) HasAny(tags ...string) bool

HasAny checks if at least one of the tags is present.

func (*SetTagger) Items

func (t *SetTagger) Items() []string

Items returns all tags as a slice of strings.

func (*SetTagger) Len

func (t *SetTagger) Len() int

Len returns tag count.

type Tagger

type Tagger interface {
	Add(tag string) bool
	Has(tag string) bool
	HasAll(tags ...string) bool
	HasAny(tags ...string) bool
	Items() []string
	Len() int
}

Tagger represents set of tags.

func NewTagger

func NewTagger() Tagger

NewTagger returns a new Tagger.

type Writer

type Writer interface {
	// Name returns name of the writer.
	Name() string

	// NeedsHeaders returns true if headers are required.
	NeedsHeaders() bool

	// Write writes dataset to writer.
	Write(d *Dataset, w io.Writer) error
}

Writer represents a dataset writer.

type XMLOpts

type XMLOpts struct {
	Indent int

	RowElem    string
	ParentElem string
}

XMLOpts represents options passed to the XML writer.

type XMLWriter

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

XMLWriter represents a XML dataset writer.

func NewXMLWriter

func NewXMLWriter(opts *XMLOpts) *XMLWriter

NewXMLWriter creates a new XML dataset writer.

func (*XMLWriter) Name

func (wx *XMLWriter) Name() string

Name returns name of the writer.

func (*XMLWriter) NeedsHeaders

func (wx *XMLWriter) NeedsHeaders() bool

NeedsHeaders returns true if headers are required.

func (*XMLWriter) Write

func (wx *XMLWriter) Write(d *Dataset, w io.Writer) error

Write writes dataset to writer.

type YAMLOpts

type YAMLOpts struct {
}

YAMLOpts represents options passed to the YAML writer.

type YAMLWriter

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

YAMLWriter represents a YAML dataset writer.

func NewYAMLWriter

func NewYAMLWriter(opts *YAMLOpts) *YAMLWriter

NewYAMLWriter creates a new YAML dataset writer.

func (*YAMLWriter) Name

func (wy *YAMLWriter) Name() string

Name returns name of the writer.

func (*YAMLWriter) NeedsHeaders

func (wy *YAMLWriter) NeedsHeaders() bool

NeedsHeaders returns true if headers are required.

func (*YAMLWriter) Write

func (wy *YAMLWriter) Write(d *Dataset, w io.Writer) error

Write writes dataset to writer.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL