csvext

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

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

Go to latest
Published: Nov 11, 2019 License: Apache-2.0 Imports: 6 Imported by: 0

README

CSV Extended (csvext)

CSV Extended provides marshaling and unmarshaling for CSV data in Go.

Examples

Bulk
type Record struct {
    Foo string `csv:"foo"`
    Bar string `csv:"bar"`
}

const data = []byte("foo,bar\nlorem,ipsum")
var records []Record
if err := csvext.Unmarshal(data, records); err != nil {
    log.Fatal(err)
}
fmt.Printf("%+v\n", records)
Streaming
type Record struct {
    Foo string `csv:"foo"`
    Bar string `csv:"bar"`
}

r := strings.NewReader("foo,bar\nlorem,ipsum")
if err := csvext.ReadTable(r, func(header, record []string) error {
    var rec Record
    if err := csvext.UnmarshalRecord(header, record, &rec); err != nil {
        return err
    }
    fmt.Printf("%+v\n", rec)
    return nil
}); err != nil {
    log.Fatal(err)
}

Documentation

Documentation is available here.

License

This project is released under the Apache License, Version 2.0.

Documentation

Overview

Package csvext provides marshaling and unmarshaling for CSV data in Go.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Marshal

func Marshal(v interface{}) ([]byte, error)

Marshal returns the CSV encoding of v.

func MarshalHeader

func MarshalHeader(v interface{}) ([]string, error)

MarshalHeader marshals a struct v into a CSV header.

func MarshalRecord

func MarshalRecord(v interface{}) ([]string, error)

MarshalRecord marshals a struct v into a CSV record.

func ReadTable

func ReadTable(r io.Reader, f func([]string, []string) error) error

ReadTable reads a CSV table from r, calling f for each record.

Example
package main

import (
	"fmt"
	"log"
	"strings"

	"github.com/tradyfinance/csvext"
)

func main() {
	type Record struct {
		Foo string `csv:"foo"`
		Bar string `csv:"bar"`
	}

	r := strings.NewReader("foo,bar\nlorem,ipsum")
	if err := csvext.ReadTable(r, func(header, record []string) error {
		var rec Record
		if err := csvext.UnmarshalRecord(header, record, &rec); err != nil {
			return err
		}
		fmt.Printf("%+v\n", rec)
		return nil
	}); err != nil {
		log.Fatal(err)
	}
}
Output:

func Unmarshal

func Unmarshal(data []byte, v interface{}) error

Unmarshal parses the CSV-encoded data and stores the result in the value pointed to by v.

Example
package main

import (
	"fmt"
	"log"

	"github.com/tradyfinance/csvext"
)

func main() {
	type Record struct {
		Foo string `csv:"foo"`
		Bar string `csv:"bar"`
	}

	data := []byte("foo,bar\nlorem,ipsum")
	var records []Record
	if err := csvext.Unmarshal(data, records); err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%+v\n", records)
}
Output:

func UnmarshalRecord

func UnmarshalRecord(header, record []string, v interface{}) error

UnmarshalRecord unmarshals a CSV record into a struct v. It will unmarshal fields by name when header is not nil, otherwise it will unmarshal fields by index.

Types

type Decoder

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

A Decoder reads and decodes a CSV table from an input stream.

func NewDecoder

func NewDecoder(r io.Reader) *Decoder

NewDecoder returns a new decoder that reads from r.

func (*Decoder) Decode

func (dec *Decoder) Decode(v interface{}) error

Decode reads a CSV table from its input stream and stores it in the value pointed to by v.

Example
package main

import (
	"fmt"
	"log"
	"strings"

	"github.com/tradyfinance/csvext"
)

func main() {
	type Record struct {
		Foo string `csv:"foo"`
		Bar string `csv:"bar"`
	}

	r := strings.NewReader("foo,bar\nlorem,ipsum")
	var records []Record
	if err := csvext.NewDecoder(r).Decode(&records); err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%+v\n", records)
}
Output:

type Encoder

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

A Encoder writes a CSV table to an output stream.

func NewEncoder

func NewEncoder(w io.Writer) *Encoder

NewEncoder returns a new encoder that writes to w.

func (*Encoder) Encode

func (enc *Encoder) Encode(v interface{}) error

Encode writes the CSV encoding of v to the stream.

type Marshaler

type Marshaler interface {
	MarshalCSV() ([]byte, error)
}

Marshaler is the inteface implemented by types that can marshal themselves into valid CSV data.

type Unmarshaler

type Unmarshaler interface {
	UnmarshalCSV([]byte) error
}

Unmarshaler is the interface implemented by types that can unmarshal themselves from valid CSV data.

Jump to

Keyboard shortcuts

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