go-simple-serializer: github.com/spatialcurrent/go-simple-serializer/pkg/sv Index | Files

package sv

import "github.com/spatialcurrent/go-simple-serializer/pkg/sv"

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

Index

Package Files

CreateHeaderAndKnownKeys.go CreateHeaderAndKnownKeysFromValue.go ErrInvalidFormat.go ExpandHeader.go ExpandHeaderWithWildcard.go FillRight.go FillRight_Test.go Flusher.go FormatToSeparator.go GetKeys.go GetKeysFromValue.go Iterator.go Read.go RemoveWildcard.go Row.go SortTable.go ToRow.go ToRowFromValue.go Write.go WriteTable.go Writer.go sv.go

Constants

const (
    Wildcard = "*"
)

func CreateHeaderAndKnownKeys Uses

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 Uses

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 Uses

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 Uses

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 Uses

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

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

func FormatToSeparator Uses

func FormatToSeparator(format string) (rune, error)

FormatToSeparator converts a format into its corresponding separator.

func GetKeys Uses

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 Uses

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 Uses

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

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

func RemoveWildcard Uses

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

func SortTable Uses

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 Uses

func TestFillRight(t *testing.T)

func ToRow Uses

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 Uses

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 Uses

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 Uses

func WriteTable(input *WriteTableInput) error

WriteTable writes the given rows as separated values.

type ErrInvalidFormat Uses

type ErrInvalidFormat struct {
    Format string
}

ErrInvalidFormat indicates an invalid format was given

func (*ErrInvalidFormat) Error Uses

func (e *ErrInvalidFormat) Error() string

Error returns the string representation of the error.

type Flusher Uses

type Flusher interface {
    Flush() error
}

type Iterator Uses

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

func NewIterator Uses

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

func (*Iterator) Header Uses

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

func (*Iterator) Next Uses

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 Uses

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 Uses

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 Uses

type Row []string

func (Row) FillRight Uses

func (r Row) FillRight(n int) Row

func (Row) Index Uses

func (r Row) Index(str string) int

func (Row) Sort Uses

func (r Row) Sort(reversed bool)

func (Row) Transform Uses

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

type WriteInput Uses

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 Uses

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 Uses

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

func NewWriter Uses

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 Uses

func (w *Writer) Close() error

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

func (*Writer) Flush Uses

func (w *Writer) Flush() error

func (*Writer) ToRow Uses

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

func (*Writer) WriteHeader Uses

func (w *Writer) WriteHeader() error

func (*Writer) WriteObject Uses

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

func (*Writer) WriteObjects Uses

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

Package sv imports 12 packages (graph) and is imported by 2 packages. Updated 2019-08-25. Refresh now. Tools for package owners.