csvd

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

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

Go to latest
Published: Oct 11, 2021 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CanonicalHeaderKey

func CanonicalHeaderKey(key string) string

CanonicalHeaderKey returns the canonical format of the header key. For example, the canonical key for " User\t Id " is "user id".

Types

type CellError

type CellError struct {
	// The key used to get the cell value in the row.
	Key string
	// The value obtained from the cell by the given key in the row.
	// The value is empty string, if the cell cannot be found by the key.
	Val string

	// The row number of cell in the csv file.
	//
	// Numbering of rows starts at 1;
	Row int
	// The column number of cell in the csv file.
	//
	// Numbering of column starts at 1;
	// The column is -1, if the cell cannot be found by the key.
	Column int
	// The line number of cell in csv file.
	//
	// Numbering of lines starts at 1;
	// A row can span multiple lines.
	// The line is -1, if the cell cannot be found by the key.
	Line int

	// ParseErr occurs when parsing cell value.
	ParseErr error
}

CellError uses context to wrap cell value parse error.

func (*CellError) Err

func (e *CellError) Err() error

Err returns a not nil error if both of e and ParseErr of e are not nil.

func (*CellError) Error

func (e *CellError) Error() string

func (*CellError) Unwrap

func (e *CellError) Unwrap() error

type Decoder

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

Decoder reads rows from a CSV-encoded file.

Example
package main

import (
	"bytes"
	"encoding/csv"
	"errors"
	"fmt"
	"io"
	"strconv"
	"strings"

	"github.com/go-camp/csvd"
)

func main() {
	type User struct {
		Name string
		Age  int8
	}

	var csvFile = "User  \tName , Age\ninvalid int,a\ngood age , 18\ntoo yong,17\ntoo old,121\n"
	r := bytes.NewReader([]byte(csvFile))
	decoder, err := csvd.NewDecoder(csvd.Options{
		Reader: csv.NewReader(r),
	})
	if err != nil {
		panic(err)
	}
	decoder.ParseHeader()
	for {
		var user User

		row, err := decoder.Next()
		if err != nil {
			if err == io.EOF {
				break
			}
			panic(err)
		}

		row.Parse("user name", func(val string) error {
			val = strings.TrimSpace(val)
			user.Name = val
			return nil
		})
		row.Parse("age", func(val string) error {
			val = strings.TrimSpace(val)

			age, err := strconv.ParseInt(val, 10, 8)
			if err != nil {
				return err
			}
			if age < 18 {
				return errors.New("too yong")
			}
			if age > 120 {
				return errors.New("too old")
			}
			user.Age = int8(age)
			return nil
		})

		if err := row.Error().Err(); err != nil {
			fmt.Println(err)
		}
	}
}
Output:

1 cell error(s) found.
- row: 2, column: 2, key: "age", val: "a", err: strconv.ParseInt: parsing "a": invalid syntax.

1 cell error(s) found.
- row: 4, column: 2, key: "age", val: "17", err: too yong.

1 cell error(s) found.
- row: 5, column: 2, key: "age", val: "121", err: too old.

func NewDecoder

func NewDecoder(opts Options) (*Decoder, error)

NewDecoder returns a new Decoder that reads from opts.Reader.

func (*Decoder) Next

func (d *Decoder) Next() (*Row, error)

Next reads one row of csv file.

If there is no row left to be read, Next returns nil, io.EOF.

func (*Decoder) ParseHeader

func (d *Decoder) ParseHeader() error

ParseHeader parses a row of csv file into header.

type Header map[string]int

Header represents the key-index pairs in the csv file header. The keys should be in canonical form, as returned by CanonicalHeaderKey.

func ParseHeader

func ParseHeader(record []string) Header

ParseHeader parses a csv file header.

func (Header) Del

func (h Header) Del(key string)

Del deletes the index associated with the given key.

The key is canonicalized by CanonicalHeaderKey.

func (Header) Get

func (h Header) Get(key string) int

Get gets the index associated with the given key. If there are no index associated with the key, Get returns -1.

The key is canonicalized by CanonicalHeaderKey.

func (Header) Has

func (h Header) Has(key string) bool

Get checks if has a index associated with the given key. If there are no index associated with the key, Get returns -1.

The key is canonicalized by CanonicalHeaderKey.

func (Header) Set

func (h Header) Set(key string, index int)

Set sets the header entries associated with key to the single index. It replaces any existing values associated with key.

The key is canonicalized by CanonicalHeaderKey.

type Options

type Options struct {
	Reader *csv.Reader
}

type Row

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

Row represents a row in csv file.

func (*Row) Error

func (r *Row) Error() *RowError

Error returns all errors that occurred when Parse was called.

func (*Row) Get

func (r *Row) Get(key string) string

Get gets the value of cell associated with the given key. If there are no cell associated with the key, Get returns empty string.

The key is canonicalized by CanonicalHeaderKey.

func (*Row) Has

func (r *Row) Has(key string) bool

Has checks if has a cell associated with the given key. If there are no cell associated with the key, Has returns false.

The key is canonicalized by CanonicalHeaderKey.

func (*Row) LastParseError

func (r *Row) LastParseError() *CellError

LastParseError returns the error that occurred the last time the Parse method was called.

func (*Row) Parse

func (r *Row) Parse(key string, parse func(val string) error)

Parse gets the value of cell associated with the given key then call parse method with that value.

The error returned by the parse method is wrapped as a CellError and can be obtained through the LastParseError method.

If you want to get all parse errors, call the Error() method.

func (*Row) Record

func (r *Row) Record() []string

Record returns the original data in the row.

type RowError

type RowError struct {
	CellErrors []*CellError
}

RowError represents a set of cell value parse errors.

func (*RowError) Add

func (e *RowError) Add(cellErr *CellError)

Add adds cellErr to e if cellErr is not nil.

func (*RowError) Err

func (e *RowError) Err() error

Err returns a not nil error if e is not nil and length of CellErrors is not zero.

func (*RowError) Error

func (e *RowError) Error() string

Jump to

Keyboard shortcuts

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