cswizard

package module
v0.0.0-...-63a8a0f Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2017 License: MIT Imports: 2 Imported by: 0

README

go-cswizard GoDoc Go Report Card

CSWizard is a CSV writer that doesn't stand in your way as your system evolves. Using encoding/csv directly is fine when you want to just do the thing and forget it. Long-living projects, however, are rarely done this way: every day business demands to add new columns, remove and reorder them, and under this conditions encoding/csv becomes too fragile due to nature of its API: you just can't change one thing and be sure that everything else would keep working. With CSWizard, you can.

So, in a nutshell, that's a small wrapper around encoding/csv for reports that change often in various ways.

See example.go for typical usage.

Use it in your project

go get users

Make sure your GOPATH is set, and run:

go get -u github.com/m1kc/go-cswizard
gb users
gb vendor fetch github.com/m1kc/go-cswizard

Documentation

Overview

Package cswizard is a CSV writer that doesn't stand in your way as your system evolves. Using `encoding/csv` directly is fine when you want to just do the thing and forget it. Long-living projects, however, are rarely done this way: every day business demands to add new columns, remove and reorder them, and under this conditions `encoding/csv` becomes too fragile due to nature of its API: you just can't change one thing and be sure that everything else would keep working. With CSWizard, you can.

So, in a nutshell, that's a small wrapper around `encoding/csv` for reports that change often in various ways.

A typical usage would be something like that:

cw := csv.NewWriter(os.Stdout)
w := cswizard.New(cw)

colName := w.AddHeader("Client name")
colAge := w.AddHeader("Client age")
colHeight := w.AddHeader("Client height (predicted)")

err := w.LockHeaders()
if err != nil {
	return
}

for _, c := range clients {
	row := w.CreateRow()
	row[colName] = c.Name
	row[colAge] = strconv.FormatUint(c.Age, 10)
	row[colHeight] = strconv.FormatUint(c.Height, 10)

	err := w.CommitRow(row)
	if err != nil {
		return
	}
}
cw.Flush()

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Writer

type Writer interface {
	// AddHeader adds a new header and returns its index which you can
	// use when you'll be filling a row.
	AddHeader(string) (id uint64)
	// LockHeaders writes all headers added before into CSV and locks
	// header list. You must call LockHeaders before you can start adding
	// the rows. Returns an error (if any).
	LockHeaders() error
	// CreateRow creates a new row and returns a buffer which you can populate
	// using indexes you've got from AddHeader calls, and later commit with
	// CommitRow.
	//
	// Internally, this buffer is reused between calls. This may change
	// in the future.
	CreateRow() []string
	// CommitRow accepts a buffer of columns (which you normally obtain
	// from CreateRow) and adds it into CSV. Returns an error (if any).
	CommitRow([]string) error
}

Writer provides methods for composing a CSV.

func New

func New(w *csv.Writer) Writer

New wraps an existing csv.Writer into cswizard.Writer.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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