sv

package
v0.0.10 Latest Latest
Warning

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

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

Documentation

Overview

Package sv includes iterators for reading from separated-values sources and writing using separated-values formats, such as CSV and TSV.

Index

Constants

View Source
const (
	Wildcard = "*"
)

Variables

View Source
var (
	DefaultType = reflect.TypeOf(map[string]string{})
)

Functions

func CreateHeaderAndKnownKeys

func CreateHeaderAndKnownKeys(object interface{}, sorted bool, reversed bool) ([]interface{}, map[interface{}]struct{})

CreateHeaderAndKnownKeys returns an object's keys or field names as a slice and set.

func CreateHeaderAndKnownKeysFromValue

func CreateHeaderAndKnownKeysFromValue(objectValue reflect.Value, sorted bool, reversed bool) ([]interface{}, map[interface{}]struct{})

CreateHeaderAndKnownKeysFromValue returns an object's keys or field names as a slice and set.

func ExpandHeader

func ExpandHeader(header []interface{}, knownKeys map[interface{}]struct{}, object reflect.Value, sorted bool, reversed bool) ([]interface{}, map[interface{}]struct{})

ExpandHeader expands a table header with the keys from the given object.

func ExpandHeaderWithWildcard

func ExpandHeaderWithWildcard(header []interface{}, knownKeys map[interface{}]struct{}, object reflect.Value, sorted bool, reversed bool) ([]interface{}, map[interface{}]struct{})

ExpandHeaderWithWildcard expands a table header at the wildcard position with keys from the object.

func FillRight

func FillRight(row []string, n int) []string

FillRight fills a slice on the right with blanks strings until it has length n.

func FormatToSeparator

func FormatToSeparator(format string) (rune, error)

FormatToSeparator converts a format into its corresponding separator.

func GetKeys

func GetKeys(object interface{}, sorted bool) []interface{}

GetKeys returns the keys for a map as an []interface{}. If you want the keys to be sorted in alphabetical order, pass sorted equal to true.

func GetKeysFromValue

func GetKeysFromValue(m reflect.Value, sorted bool) []interface{}

GetKeysFromValue returns the keys for a map as an []interface{}. If you want the keys to be sorted in alphabetical order, pass sorted equal to true.

func Read

func Read(input *ReadInput) (interface{}, error)

Read reads the separated values from the input reader into a slice.

func RemoveWildcard

func RemoveWildcard(header []interface{}) []interface{}

func SortTable

func SortTable(header []string, rows [][]string, reversed bool) ([]string, [][]string)

SortTable sorts the table columns alphabetically. If reversed is true, then sorts in reverse alphabetical order.

func TestFillRight

func TestFillRight(t *testing.T)

func ToRow

func ToRow(object interface{}, columns []interface{}, valueSerializer func(object interface{}) (string, error)) ([]string, error)

ToRow converts an object into a row of strings and returns an error, if any.

func ToRowFromValue

func ToRowFromValue(objectValue reflect.Value, columns []interface{}, valueSerializer func(object interface{}) (string, error)) ([]string, error)

ToRow converts an object into a row of strings and returns an error, if any.

func Write

func Write(input *WriteInput) error

Write writes the given object(s) as separated values, e.g., CSV or T If the type of the input object is of kind Array or Slice, then writes each object on its own line. Otherwise, just writes a CSV with a header and one row for the object.

func WriteTable

func WriteTable(input *WriteTableInput) error

WriteTable writes the given rows as separated values.

Types

type ErrInvalidFormat

type ErrInvalidFormat struct {
	Format string
}

ErrInvalidFormat indicates an invalid format was given

func (*ErrInvalidFormat) Error

func (e *ErrInvalidFormat) Error() string

Error returns the string representation of the error.

type Flusher

type Flusher interface {
	Flush() error
}

type Iterator

type Iterator struct {
	Reader *csv.Reader
	Type   reflect.Type
	// contains filtered or unexported fields
}

func NewIterator

func NewIterator(input *NewIteratorInput) (*Iterator, error)

func (*Iterator) Header

func (it *Iterator) Header() []interface{}

func (*Iterator) Next

func (it *Iterator) Next() (interface{}, error)

Next reads from the underlying reader and returns the next object and error, if any. When finished, returns (nil, io.EOF).

type NewIteratorInput

type NewIteratorInput struct {
	Reader     io.Reader
	Type       reflect.Type // required
	Separator  rune         // the values separator
	Header     []interface{}
	SkipLines  int
	Comment    string
	LazyQuotes bool
	Limit      int
}

Input for NewIterator function.

type ReadInput

type ReadInput struct {
	Type       reflect.Type // the output type
	Reader     io.Reader
	Separator  rune // the values separator
	Header     []interface{}
	SkipLines  int
	Comment    string
	LazyQuotes bool
	Limit      int
}

ReadInput provides the input for the Read function.

type Row

type Row []string

func (Row) FillRight

func (r Row) FillRight(n int) Row

func (Row) Index

func (r Row) Index(str string) int

func (Row) Sort

func (r Row) Sort(reversed bool)

func (Row) Transform

func (r Row) Transform(m map[int]int) Row

type WriteInput

type WriteInput struct {
	Writer          io.Writer // the underlying writer
	Separator       rune      // the values separator
	Header          []interface{}
	KeySerializer   stringify.Stringer
	ValueSerializer stringify.Stringer
	Object          interface{} // the object to write
	ExpandHeader    bool        // dynamically expand header, requires caching output in memory
	Sorted          bool        // sort columns
	Reversed        bool        // if sorted, sort in reverse alphabetical order.
	Limit           int
}

WriteInput provides the input for the Write function.

type WriteTableInput

type WriteTableInput struct {
	Writer    io.Writer  // the underlying writer
	Separator rune       // the values separator
	Header    []string   // the table header
	Rows      [][]string // the row of values to write to the underlying writer
	Sorted    bool       // sort the columns
	Reversed  bool       // if sorted, sort in reverse order
}

WriteTableInput provides the input for the WriteTable function.

type Writer

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

func NewWriter

func NewWriter(underlying io.Writer, separator rune, columns []interface{}, keySerializer stringify.Stringer, valueSerializer stringify.Stringer, sorted bool, reversed bool) *Writer

NewWriter returns a new Writer for writing objects to an underlying writer formatted as separated values. NewWriter is a streaming writer, so cannot dynamically expand the header. To dynamically expand the header, then use the Write function with ExpandHeader set to true.

func (*Writer) Close added in v0.0.9

func (w *Writer) Close() error

Close closes the underlying writer, if it has a Close method.

func (*Writer) Flush

func (w *Writer) Flush() error

func (*Writer) ToRow

func (w *Writer) ToRow(obj interface{}) ([]string, error)

func (*Writer) WriteHeader

func (w *Writer) WriteHeader() error

func (*Writer) WriteObject

func (w *Writer) WriteObject(obj interface{}) error

func (*Writer) WriteObjects

func (w *Writer) WriteObjects(objects interface{}) error

Jump to

Keyboard shortcuts

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