parser

package module
v0.0.2 Latest Latest
Warning

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

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

README

CSV Parser Build Status codecov.io

Simple library that parse a CSV file and returns an array of pointers to the given type.

Limitations
  • Works only with a struct that contains string, int, uint, bool, time and float fields.

Getting started

Install
go get -u github.com/empatica/csvparser
Usage

Define your struct:

type YourStruct struct{
  Field1 string
  Field2 int
  Field3 bool
  Field4 float64
  Field5 time.Time `csvDate:"2006-05-07"`
}

If you don't add 'csv' tags close to each struct's field, the lib will set the first field using the first column of csv's row, and so on. So the previous struct is the same as:

type YourStruct struct{
  Field1 string    `csv:"0"`
  Field2 int       `csv:"1"`
  Field3 bool      `csv:"2"`
  Field4 float64   `csv:"3"`
  Field5 time.Time `csv:"4" csvDate:"2006-05-07"`
}

You can always define 'csv' tags (for all or some of the struct's fields) that will tell the lib which column to use:

type YourStruct struct{
  Field1 string    `csv:"4"`
  Field2 int       `csv:"0"`
  Field3 bool      `csv:"3"`
  Field4 float64   `csv:"2"`
  Field5 time.Time `csv:"1" csvDate:"2006-05-07"`
}
Note for time.Time fields:

It's required to specify a csvDate tag that will be used for parsing, following the rules describere here

Parse the file:
var csvParser = parser.CsvParser{
    CsvFile:      "path_to_your_file.csv",
    CsvSeparator: ',',
    SkipFirstLine : true, //default:false
    SkipEmptyValues : true, //default:false. It will skip empty values and won't try to parse them
}

var parsedItems, err = csvParser.Parse(YourStruct{})

for i := 0; i < len(parsedItems); i++ {
  log.Print(parsedItems[i].(*YourStruct))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetMetaType

func GetMetaType(obj interface{}) reflect.Type

获取obj的反射类型, 如果obj是指针,则返回指向的类型

Types

type CSVReader

type CSVReader interface {
	Reader() (io.Reader, error)
}

func NewFileLoader

func NewFileLoader(filename string) CSVReader

type ContactGetter

type ContactGetter interface {
	GetFirstName() string
	GetLastName() string
	GetWorking() bool
	GetAge() int
	GetSalary32() float32
	GetSalary64() float64
	GetVacationDays() uint
	GetTitle() string
	GetEmail() string
	GetBirthdate() time.Time
	GetDescription() string
}

ContactGetter is a wrapper for the different types of contact structs we want to test

type CsvParser

type CsvParser struct {
	CsvReader    CSVReader
	CsvSeparator rune
	BindObject   interface{}
	Setter       func(field reflect.Value, colName string, raw string) bool
}

CsvParser parses a csv file and returns an array of pointers the type specified

func (CsvParser) Parse

func (parser CsvParser) Parse() (interface{}, error)

Parse creates the array of the given type from the csv file

type ExampleContact1

type ExampleContact1 struct {
	FirstName    string    `csv:"0"`
	LastName     string    `csv:"1"`
	Working      bool      `csv:"2"`
	Age          int       `csv:"3"`
	Salary32     float32   `csv:"4"`
	Salary64     float64   `csv:"5"`
	VacationDays uint      `csv:"6"`
	Title        string    `csv:"7"`
	Email        string    `csv:"8"`
	Birthdate    time.Time `csv:"9" csvDate:"2006-01-02"`
	Description  string    `csv:"10"`
}

ExampleContact1 specifies all the csv struct tag fields

func (ExampleContact1) GetAge

func (c1 ExampleContact1) GetAge() int

func (ExampleContact1) GetBirthdate

func (c1 ExampleContact1) GetBirthdate() time.Time

func (ExampleContact1) GetDescription

func (c1 ExampleContact1) GetDescription() string

func (ExampleContact1) GetEmail

func (c1 ExampleContact1) GetEmail() string

func (ExampleContact1) GetFirstName

func (c1 ExampleContact1) GetFirstName() string

func (ExampleContact1) GetLastName

func (c1 ExampleContact1) GetLastName() string

func (ExampleContact1) GetSalary32

func (c1 ExampleContact1) GetSalary32() float32

func (ExampleContact1) GetSalary64

func (c1 ExampleContact1) GetSalary64() float64

func (ExampleContact1) GetTitle

func (c1 ExampleContact1) GetTitle() string

func (ExampleContact1) GetVacationDays

func (c1 ExampleContact1) GetVacationDays() uint

func (ExampleContact1) GetWorking

func (c1 ExampleContact1) GetWorking() bool

type ExampleContact2

type ExampleContact2 struct {
	FirstName    string
	LastName     string `csv:"1"`
	Working      bool
	Age          int
	Salary32     float32
	Salary64     float64
	VacationDays uint
	Title        string `csv:"7"`
	Email        string
	Birthdate    time.Time `csv:"9" csvDate:"2006-01-02"`
	Description  string    `csv:"10"`
}

ExampleContact2 specifies some of the csv struct tag fields

func (ExampleContact2) GetAge

func (c2 ExampleContact2) GetAge() int

func (ExampleContact2) GetBirthdate

func (c2 ExampleContact2) GetBirthdate() time.Time

func (ExampleContact2) GetDescription

func (c2 ExampleContact2) GetDescription() string

func (ExampleContact2) GetEmail

func (c2 ExampleContact2) GetEmail() string

func (ExampleContact2) GetFirstName

func (c2 ExampleContact2) GetFirstName() string

func (ExampleContact2) GetLastName

func (c2 ExampleContact2) GetLastName() string

func (ExampleContact2) GetSalary32

func (c2 ExampleContact2) GetSalary32() float32

func (ExampleContact2) GetSalary64

func (c2 ExampleContact2) GetSalary64() float64

func (ExampleContact2) GetTitle

func (c2 ExampleContact2) GetTitle() string

func (ExampleContact2) GetVacationDays

func (c2 ExampleContact2) GetVacationDays() uint

func (ExampleContact2) GetWorking

func (c2 ExampleContact2) GetWorking() bool

type ExampleContact3

type ExampleContact3 struct {
	FirstName    string
	LastName     string
	Working      bool
	Age          int
	Salary32     float32
	Salary64     float64
	VacationDays uint
	Title        string
	Email        string
	Birthdate    time.Time `csvDate:"2006-01-02"`
	Description  string
}

ExampleContact3 specifies any of the csv struct tag fields

func (ExampleContact3) GetAge

func (c3 ExampleContact3) GetAge() int

func (ExampleContact3) GetBirthdate

func (c3 ExampleContact3) GetBirthdate() time.Time

func (ExampleContact3) GetDescription

func (c3 ExampleContact3) GetDescription() string

func (ExampleContact3) GetEmail

func (c3 ExampleContact3) GetEmail() string

func (ExampleContact3) GetFirstName

func (c3 ExampleContact3) GetFirstName() string

func (ExampleContact3) GetLastName

func (c3 ExampleContact3) GetLastName() string

func (ExampleContact3) GetSalary32

func (c3 ExampleContact3) GetSalary32() float32

func (ExampleContact3) GetSalary64

func (c3 ExampleContact3) GetSalary64() float64

func (ExampleContact3) GetTitle

func (c3 ExampleContact3) GetTitle() string

func (ExampleContact3) GetVacationDays

func (c3 ExampleContact3) GetVacationDays() uint

func (ExampleContact3) GetWorking

func (c3 ExampleContact3) GetWorking() bool

type ExampleContactInvalidBoolean

type ExampleContactInvalidBoolean struct {
	FirstName    string
	LastName     bool //this is not a bool!
	Working      bool
	Age          int
	Salary32     float32
	Salary64     float64
	VacationDays uint
	Title        string
	Email        string
	Birthdate    time.Time
	Description  string
}

ExampleContactInvalidBoolean is made for testing bool wrong field

type ExampleContactInvalidFloat32

type ExampleContactInvalidFloat32 struct {
	FirstName    string
	LastName     float32 //this is not an int!
	Working      bool
	Age          int
	Salary32     float32
	Salary64     float64
	VacationDays uint
	Title        string
	Email        string
	Birthdate    time.Time
	Description  string
}

ExampleContactInvalidFloat32 is made for testing uint wrong field

type ExampleContactInvalidFloat64

type ExampleContactInvalidFloat64 struct {
	FirstName    string
	LastName     float64 //this is not an int!
	Working      bool
	Age          int
	Salary32     float32
	Salary64     float64
	VacationDays uint
	Title        string
	Email        string
	Birthdate    time.Time
	Description  string
}

ExampleContactInvalidFloat64 is made for testing uint wrong field

type ExampleContactInvalidInt

type ExampleContactInvalidInt struct {
	FirstName    string
	LastName     int //this is not an int!
	Working      bool
	Age          int
	Salary32     float32
	Salary64     float64
	VacationDays uint
	Title        string
	Email        string
	Birthdate    time.Time
	Description  string
}

ExampleContactInvalidInt is made for testing uint wrong field

type ExampleContactInvalidTime

type ExampleContactInvalidTime struct {
	FirstName    string    `csv:"0"`
	LastName     string    `csv:"1"`
	Working      bool      `csv:"2"`
	Age          int       `csv:"3"`
	Salary32     float32   `csv:"4"`
	Salary64     float64   `csv:"5"`
	VacationDays uint      `csv:"6"`
	Title        string    `csv:"7"`
	Email        string    `csv:"8"`
	Birthdate    time.Time `csv:"9" csvDate:"invalidDateFormat"`
	Description  string    `csv:"10"`
}

ExampleContactInvalidTime is made for testing an invalid date format

type ExampleContactInvalidUint

type ExampleContactInvalidUint struct {
	FirstName    string
	LastName     uint //this is not an uint!
	Working      bool
	Age          int
	Salary32     float32
	Salary64     float64
	VacationDays uint
	Title        string
	Email        string
	Birthdate    time.Time
	Description  string
}

ExampleContactInvalidUint is made for testing uint wrong field

type ExampleContactWithCsvColumnFieldTooHigh

type ExampleContactWithCsvColumnFieldTooHigh struct {
	LastName string `csv:"1000"`
}

ExampleContactWithCsvColumnFieldTooHigh has a csv column tag that exceed the number of csv columns in a row

type ExampleContactWithCsvTagLessThanZero

type ExampleContactWithCsvTagLessThanZero struct {
	LastName string `csv:"-2"`
}

ExampleContactWithCsvTagLessThanZero has a csv column tag that is negative

type ExampleContactWithCsvTagNotAnInteger

type ExampleContactWithCsvTagNotAnInteger struct {
	LastName string `csv:"notAnInteger"`
}

ExampleContactWithCsvTagNotAnInteger has a csv column tag that is not an integer

type NoMarshalFuncError

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

NoMarshalFuncError is the custom error type to be raised in case there is no marshal function defined on type

func (NoMarshalFuncError) Error

func (e NoMarshalFuncError) Error() string

type NoUnmarshalFuncError

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

NoUnmarshalFuncError is the custom error type to be raised in case there is no unmarshal function defined on type

func (NoUnmarshalFuncError) Error

func (e NoUnmarshalFuncError) Error() string

type Stringer

type Stringer interface {
	String() string
}

Stringer is implemented by any value that has a String method This converter is used to convert the value to it string representation This converter will be used if your value does not implement TypeMarshaller

type TypeMarshaller

type TypeMarshaller interface {
	MarshalCSV() (string, error)
}

TypeMarshaller is implemented by any value that has a MarshalCSV method This converter is used to convert the value to it string representation

type TypeUnmarshaller

type TypeUnmarshaller interface {
	UnmarshalCSV(string) error
}

TypeUnmarshaller is implemented by any value that has an UnmarshalCSV method This converter is used to convert a string to your value representation of that string

Jump to

Keyboard shortcuts

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