reporter

package module
v0.0.0-...-7506108 Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2019 License: MIT Imports: 6 Imported by: 0

README

Reporter

Report is a package that simplifies creating CSV-formatted reports. To create a report, simply create a struct that implements the `Row` interface, populate the report rows, then write the report.

Features:
  • Define custom report structures via the Row interface
  • Generate report with or without CSV header
  • Save report to file, Stdout, output buffer - anything that implements a io.Writer
Examples
Basic use
...

// define reporter structure
type ReportRow struct {
	First  string
	Second string
}

// implement interface methods
func (r *ReportRow) Header() []string {
	return []string{"first_thing", "second_thing"}
}

func (r *ReportRow) Slice() []string {
	return []string{r.First, r.Second}
}

func main() {
	reportFile, err := os.Create("my_report.csv")
	if err != nil {
		log.Printf("error creating file - %v", err)
	}
	defer reportFile.Close()

	reportBuilder := reporter.New(reportFile)
	
	// do lots of work to generate and add reporter rows
	reportBuilder.AddRow(&ReportRow{
		First:  "first",
		Second: "second",
	})

	rowsWritten, err := reportBuilder.WriteAll()
	if err != nil {
		log.Fatalf("error writing reporter - %v", err)
	}
	log.Printf("wrote %d lines to %s", rowsWritten, reportFile.Name())

	...
}

...
Create report without header
...

reportFile, err := os.Create("my_report.csv")
if err != nil {
    log.Printf("error creating file - %v", err)
}
defer reportFile.Close()

reportBuilder := reporter.NewWithoutHeader(reportFile)

...
Send output to the console
...
reportBuilder := reporter.New(os.Stdout)
...
Save output in memory
...
buf := bytes.NewBuffer(nil)
reportBuilder := reporter.New(buf)
...

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateHeader

func CreateHeader(v interface{}) []string

CreateHeader uses the "csv" struct tags to build the CSV header v should be a struct with "csv" tags. Example:

type MyData struct {
	Field1   string    `csv:"field_1"`
	IgnoreMe chan int  `csv:"-"`
	Field2   time.Time `csv:"field_2"`
}

CreateHeader(MyData{}) will output []string{"field_1", "field_2"}, ignoring the "-" tag.

Experimental

func MarshalCSV

func MarshalCSV(v interface{}) []string

MarshalCSV returns the struct values as a slice of strings. Fields with the tag `csv:"-"` will be ignored.

Experimental

func SetDateTimeFormat

func SetDateTimeFormat(format string)

SetDateTimeFormat updates the default format string to the supplied string. Valid formats: https://golang.org/src/time/format.go This is only used by MarshalCSV.

Types

type Reporter

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

Reporter provides functionality for generating CSV-formatted reports using any struct that implements the Row interface.

func New

func New(out io.Writer) *Reporter

New returns a pointer to a Reporter.

func NewWithoutHeader

func NewWithoutHeader(out io.Writer) *Reporter

NewWithoutHeader returns a pointer to a Reporter that will not include headers in the output

func (*Reporter) AddRow

func (r *Reporter) AddRow(row Row)

AddRow appends the provided row to Reporter's []*Rows slice

func (*Reporter) Flush

func (r *Reporter) Flush() error

Flush calls the same method in Reporter's underlying csv.Writer, then checks for and returns any errors

func (*Reporter) Length

func (r *Reporter) Length() int

Length returns the number of rows currently saved in the Reporter

func (*Reporter) RemoveRow

func (r *Reporter) RemoveRow(idx int) error

RemoveRow removes the row at the specified index

func (*Reporter) Row

func (r *Reporter) Row(idx int) Row

Row retrieves the row at the specified idx, or nil if the index is out of bounds

func (*Reporter) WriteAll

func (r *Reporter) WriteAll() (int, error)

WriteAll writes all of the rows saved in the Reporter along with the header, if applicable

type Row

type Row interface {
	// Header should return a list of the column names to be included in the CSV report
	Header() []string
	// Marshal should return a list of the data points to be written to the CSV report
	Marshal() []string
}

Row provides the methods needed to convert a struct to a CSV row

Example (using experimental reporter.CreateHeader() and reporter.MarshalCSV()):

type MyData struct {
	Field1 string    `csv:"field_1"`
	Field2 time.Time `csv:"field_2"`
}

func (d *MyData) Header() []string {
	return reporter.CreateHeader(d)
}

func (d *MyData) Marshal() []string {
	return reporter.MarshalCSV(d)
}

Jump to

Keyboard shortcuts

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