csvlib

package module
v0.0.0-...-1b05af5 Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2018 License: Apache-2.0 Imports: 5 Imported by: 1

README

csvlib

A simple CSV parsing library for Go.

How to use

One can define a parser:

var parser = &csvlib.RowParser{
	[]csvlib.Parser{
		csvlib.Int64Parser{"ID"},
		csvlib.Int32Parser{Name: "Number"}, // CKK
		csvlib.TimeParser{"Timestamp", "2006-01-02 15:04:05"},
		csvlib.StringParser{"Name"},
	},
}

type Data struct {
	ID int64
	Number int32
	Time time.Time
	Name string
}

// and use it to parse row:
reader := csv.NewReader(r)
record, err := reader.Read()          // reads one line
values, err := parser.Parse(record)   // converts values
dat := Data{
	values[0].Int64(),
	values[1].Int32(),
	values[2].Time(),
	values[3].String(),
}

The most common usecase is when a you want to give the user the possibility to choose columns. It can be configured though a config file or UI.

Also it's pretty easy to create your own type of data (e.g. Decimal with fixed precision), you must implement Parser interface.

If you want to parse directly to structure, you may be interested in: github.com/gocarina/gocsv which is using a reflection mechanism and custom structure tag.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrCannotParse = errors.New("cannot parse")
View Source
var ErrNumberOfElemNotMatch = fmt.Errorf("number of elements does NOT match number of parser")

Functions

This section is empty.

Types

type BoolParser

type BoolParser struct {
	Name string
}

func (BoolParser) Defaults

func (BoolParser) Defaults() []ParsedValue

func (BoolParser) Parse

func (p BoolParser) Parse(val string) ([]ParsedValue, error)

type DecimalParser

type DecimalParser struct {
	Name string
	// contains filtered or unexported fields
}

func (DecimalParser) Defaults

func (DecimalParser) Defaults() []ParsedValue

func (DecimalParser) Parse

func (d DecimalParser) Parse(val string) ([]ParsedValue, error)

type ElemNotMatch

type ElemNotMatch struct {
	Want, Got int
}

func (ElemNotMatch) Error

func (e ElemNotMatch) Error() string

type Float32Parser

type Float32Parser struct {
	Name     string
	Optional bool
}

func (Float32Parser) Defaults

func (Float32Parser) Defaults() []ParsedValue

func (Float32Parser) Parse

func (p Float32Parser) Parse(val string) ([]ParsedValue, error)

type Int32Parser

type Int32Parser struct {
	Name     string
	Optional bool  // if "" then nil
	Default  int32 // as defautl value
}

func (Int32Parser) Defaults

func (p Int32Parser) Defaults() []ParsedValue

func (Int32Parser) Parse

func (p Int32Parser) Parse(val string) ([]ParsedValue, error)

type Int64Parser

type Int64Parser struct {
	Name string
}

func (Int64Parser) Defaults

func (Int64Parser) Defaults() []ParsedValue

func (Int64Parser) Parse

func (p Int64Parser) Parse(val string) ([]ParsedValue, error)

type ParsedBool

type ParsedBool struct {
	Value bool
	Valid bool
}

func (ParsedBool) Bool

func (p ParsedBool) Bool() bool

func (ParsedBool) F32

func (ParsedBool) F32() float32

func (ParsedBool) F64

func (ParsedBool) F64() float64

func (ParsedBool) I32

func (ParsedBool) I32() int32

func (ParsedBool) I64

func (ParsedBool) I64() int64

func (ParsedBool) Ifc

func (ParsedBool) Ifc() interface{}

func (ParsedBool) IsValid

func (p ParsedBool) IsValid() bool

func (ParsedBool) String

func (ParsedBool) String() string

func (ParsedBool) Time

func (ParsedBool) Time() time.Time

type ParsedDecimal

type ParsedDecimal struct {
	ParsedDefault
	// contains filtered or unexported fields
}

func (ParsedDecimal) Ifc

func (p ParsedDecimal) Ifc() interface{}

func (ParsedDecimal) IsValid

func (p ParsedDecimal) IsValid() bool

type ParsedDefault

type ParsedDefault struct{}

func (ParsedDefault) Bool

func (p ParsedDefault) Bool() bool

func (ParsedDefault) F32

func (ParsedDefault) F32() float32

func (ParsedDefault) F64

func (ParsedDefault) F64() float64

func (ParsedDefault) I32

func (ParsedDefault) I32() int32

func (ParsedDefault) I64

func (ParsedDefault) I64() int64

func (ParsedDefault) Ifc

func (ParsedDefault) Ifc() interface{}

func (ParsedDefault) IsValid

func (p ParsedDefault) IsValid() bool

func (ParsedDefault) String

func (ParsedDefault) String() string

func (ParsedDefault) Time

func (ParsedDefault) Time() time.Time

type ParsedFloat32

type ParsedFloat32 float32

func (ParsedFloat32) Bool

func (ParsedFloat32) Bool() bool

func (ParsedFloat32) F32

func (p ParsedFloat32) F32() float32

func (ParsedFloat32) F64

func (ParsedFloat32) F64() float64

func (ParsedFloat32) I32

func (ParsedFloat32) I32() int32

func (ParsedFloat32) I64

func (ParsedFloat32) I64() int64

func (ParsedFloat32) Ifc

func (ParsedFloat32) Ifc() interface{}

func (ParsedFloat32) IsValid

func (ParsedFloat32) IsValid() bool

func (ParsedFloat32) String

func (ParsedFloat32) String() string

func (ParsedFloat32) Time

func (ParsedFloat32) Time() time.Time

type ParsedFloat64

type ParsedFloat64 float64

func (ParsedFloat64) Bool

func (ParsedFloat64) Bool() bool

func (ParsedFloat64) F32

func (ParsedFloat64) F32() float32

func (ParsedFloat64) F64

func (p ParsedFloat64) F64() float64

func (ParsedFloat64) I32

func (ParsedFloat64) I32() int32

func (ParsedFloat64) I64

func (ParsedFloat64) I64() int64

func (ParsedFloat64) Ifc

func (ParsedFloat64) Ifc() interface{}

func (ParsedFloat64) IsValid

func (ParsedFloat64) IsValid() bool

func (ParsedFloat64) String

func (ParsedFloat64) String() string

func (ParsedFloat64) Time

func (ParsedFloat64) Time() time.Time

type ParsedInt32

type ParsedInt32 int32

func (ParsedInt32) Bool

func (ParsedInt32) Bool() bool

func (ParsedInt32) F32

func (ParsedInt32) F32() float32

func (ParsedInt32) F64

func (ParsedInt32) F64() float64

func (ParsedInt32) I32

func (p ParsedInt32) I32() int32

func (ParsedInt32) I64

func (ParsedInt32) I64() int64

func (ParsedInt32) Ifc

func (ParsedInt32) Ifc() interface{}

func (ParsedInt32) IsValid

func (ParsedInt32) IsValid() bool

func (ParsedInt32) String

func (ParsedInt32) String() string

func (ParsedInt32) Time

func (ParsedInt32) Time() time.Time

type ParsedInt64

type ParsedInt64 int64

func (ParsedInt64) Bool

func (ParsedInt64) Bool() bool

func (ParsedInt64) F32

func (ParsedInt64) F32() float32

func (ParsedInt64) F64

func (ParsedInt64) F64() float64

func (ParsedInt64) I32

func (ParsedInt64) I32() int32

func (ParsedInt64) I64

func (p ParsedInt64) I64() int64

func (ParsedInt64) Ifc

func (ParsedInt64) Ifc() interface{}

func (ParsedInt64) IsValid

func (ParsedInt64) IsValid() bool

func (ParsedInt64) String

func (ParsedInt64) String() string

func (ParsedInt64) Time

func (ParsedInt64) Time() time.Time

type ParsedString

type ParsedString string

func (ParsedString) Bool

func (ParsedString) Bool() bool

func (ParsedString) F32

func (ParsedString) F32() float32

func (ParsedString) F64

func (ParsedString) F64() float64

func (ParsedString) I32

func (ParsedString) I32() int32

func (ParsedString) I64

func (ParsedString) I64() int64

func (ParsedString) Ifc

func (ParsedString) Ifc() interface{}

func (ParsedString) IsValid

func (ParsedString) IsValid() bool

func (ParsedString) String

func (p ParsedString) String() string

func (ParsedString) Time

func (ParsedString) Time() time.Time

type ParsedTime

type ParsedTime time.Time

func (ParsedTime) Bool

func (ParsedTime) Bool() bool

func (ParsedTime) F32

func (ParsedTime) F32() float32

func (ParsedTime) F64

func (ParsedTime) F64() float64

func (ParsedTime) I32

func (ParsedTime) I32() int32

func (ParsedTime) I64

func (ParsedTime) I64() int64

func (ParsedTime) Ifc

func (ParsedTime) Ifc() interface{}

func (ParsedTime) IsValid

func (ParsedTime) IsValid() bool

func (ParsedTime) String

func (ParsedTime) String() string

func (ParsedTime) Time

func (p ParsedTime) Time() time.Time

type ParsedValue

type ParsedValue interface {
	I32() int32
	I64() int64
	F32() float32
	F64() float64
	String() string
	Bool() bool
	Time() time.Time
	Ifc() interface{}
	IsValid() bool
}

type Parser

type Parser interface {
	Parse(val string) ([]ParsedValue, error)
	Defaults() []ParsedValue
}

type RowParser

type RowParser struct {
	P []Parser
}

func (*RowParser) AppendColumn

func (r *RowParser) AppendColumn(p Parser)

func (*RowParser) Len

func (r *RowParser) Len() int

func (*RowParser) Parse

func (r *RowParser) Parse(row []string) ([]ParsedValue, error)

type SkipParser

type SkipParser struct {
	Name string
}

Skips parsing one value.

func (SkipParser) Defaults

func (SkipParser) Defaults() []ParsedValue

func (SkipParser) Parse

func (SkipParser) Parse(val string) ([]ParsedValue, error)

type StringParser

type StringParser struct {
	Name string
}

func (StringParser) Defaults

func (StringParser) Defaults() []ParsedValue

func (StringParser) Parse

func (StringParser) Parse(val string) ([]ParsedValue, error)

type TimeParser

type TimeParser struct {
	Name   string
	Layout string
}

func (TimeParser) Defaults

func (TimeParser) Defaults() []ParsedValue

func (TimeParser) Parse

func (t TimeParser) Parse(val string) ([]ParsedValue, error)

Jump to

Keyboard shortcuts

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