Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
937 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package tablib | ||
|
||
import ( | ||
"bytes" | ||
"encoding/csv" | ||
) | ||
|
||
// CSV returns a CSV representation of the Dataset as string. | ||
func (d *Dataset) CSV() (string, error) { | ||
records := d.Records() | ||
var b bytes.Buffer | ||
|
||
w := csv.NewWriter(&b) | ||
w.WriteAll(records) // calls Flush internally | ||
|
||
if err := w.Error(); err != nil { | ||
return "", err | ||
} | ||
|
||
return b.String(), nil | ||
} | ||
|
||
// TSV returns a TSV representation of the Dataset as string. | ||
func (d *Dataset) TSV() (string, error) { | ||
records := d.Records() | ||
var b bytes.Buffer | ||
|
||
w := csv.NewWriter(&b) | ||
w.Comma = '\t' | ||
w.WriteAll(records) // calls Flush internally | ||
|
||
if err := w.Error(); err != nil { | ||
return "", err | ||
} | ||
|
||
return b.String(), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package tablib | ||
|
||
// Sheet represents a sheet in a Databook, holding a title (if any) and a dataset. | ||
type Sheet struct { | ||
title string | ||
dataset *Dataset | ||
} | ||
|
||
// Title return the title of the sheet. | ||
func (s Sheet) Title() string { | ||
return s.title | ||
} | ||
|
||
// Dataset returns the dataset of the sheet. | ||
func (s Sheet) Dataset() *Dataset { | ||
return s.dataset | ||
} | ||
|
||
// Databook represents a Databook which is an array of sheets. | ||
type Databook struct { | ||
sheets map[string]Sheet | ||
} | ||
|
||
// NewDatabook constructs a new Databook. | ||
func NewDatabook() *Databook { | ||
return &Databook{make(map[string]Sheet)} | ||
} | ||
|
||
// Sheets returns the sheets in the Databook. | ||
func (d *Databook) Sheets() map[string]Sheet { | ||
return d.sheets | ||
} | ||
|
||
// Sheet returns the sheet with a specific title. | ||
func (d *Databook) Sheet(title string) Sheet { | ||
return d.sheets[title] | ||
} | ||
|
||
// AddSheet adds a sheet to the Databook. | ||
func (d *Databook) AddSheet(title string, dataset *Dataset) { | ||
d.sheets[title] = Sheet{title, dataset} | ||
} | ||
|
||
// Size returns the number of sheets in the Databook. | ||
func (d *Databook) Size() int { | ||
return len(d.sheets) | ||
} | ||
|
||
// Wipe removes all Dataset objects from the Databook. | ||
func (d *Databook) Wipe() { | ||
for k := range d.sheets { | ||
delete(d.sheets, k) | ||
} | ||
} |
Oops, something went wrong.