gocsv

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2022 License: Apache-2.0 Imports: 7 Imported by: 1

README

gocsv

Go package for parsing CSV records into structs. Currently, it supports only the following types and its references:

  • int, int8, int16, int32, int64
  • uint, uint8, uint16, uint32, uint64
  • float32, float64
  • string

In addition, you may provide own function to be used for parsing values. The function should accept string parameter and return error if there are any errors when parsing.

Example

package main

import (
	"fmt"
	"io"
	"strings"

	"github.com/vchezganov/gocsv"
)

type Person struct {
	Name string `csv:"name"`
	Age  uint   `csv:"age"`
	ID   string `csv:"pass,ParseID"`
}

func (p *Person) ParseID(value string) error {
	p.ID = fmt.Sprintf("ABC-%s", value)
	return nil
}

func main() {
	stringReader := strings.NewReader("age,pass,name\n32,12345,Vitaly\n45,54321,Alexey")

	reader, err := gocsv.NewReader[Person](stringReader)
	if err != nil {
		panic(err)
	}

	for {
		model, err := reader.Next()
		if err == io.EOF {
			break
		} else if err != nil {
			continue
		}

		fmt.Printf("Person: %v\n", *model)
	}
}

Or Marshaller could be used directly:

package main

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

	"github.com/vchezganov/gocsv"
)

type Person struct {
	Name     string `csv:"name"`
	Age      int    `csv:"age"`
	Location string `csv:"city"`
	ID       int    `csv:"passport,ParseID"`
}

func (p *Person) ParseID(value string) error {
	s, err := strconv.Atoi(value)
	if err != nil {
		return err
	}

	if 10000 <= s && s <= 99999 {
		p.ID = s
		return nil
	}

	return errors.New("ID is not valid")
}

func main() {
	s := strings.NewReader("name,age,city,passport\nVitaly,25,Bonn,10000")
	csvReader := csv.NewReader(s)
	headers, err := csvReader.Read()
	if err != nil {
		panic(err)
	}

	marshaller, err := gocsv.NewMarshaller(headers)
	if err != nil {
		panic(err)
	}

	model := new(Person)
	records, err := csvReader.Read()
	if err != nil {
		panic(err)
	}

	err = marshaller.Unmarshal(records, model)
	if err != nil {
		panic(err)
	}

	fmt.Printf("Person: %v\n", *model)
}

Next steps

  • Support composition
type A struct {
	ID        int    `csv:"id"`
	Timestamp string `csv:"timestamp"`
}

// id, timestamp, name
type B struct {
	*A
	Name int `csv:"name"`
}

// prefix_id, prefix_timestamp, name
type C struct {
	Base *A  `csv:"prefix"`
	Name int `csv:"name"`
}
  • Parsing function to accept not only string but int, float, etc.
  • Converting structs into CSV dictionary, slice or string

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewMarshaller

func NewMarshaller(headers []string) (*marshaller, error)

NewMarshaller creates an instance of gocsv.Marshaller with defined headers.

Types

type Marshaller added in v1.2.0

type Marshaller interface {
	Unmarshal(values []string, model interface{}) (err error)
}

Marshaller is an object to parse and convert CSV records into Go object.

type Reader added in v1.2.0

type Reader[T any] struct {
	CSVReader *csv.Reader
	// contains filtered or unexported fields
}

Reader is an object to read and parse CSV files into Go objects.

func NewReader added in v1.2.0

func NewReader[T any](csvFile io.Reader) (*Reader[T], error)

NewReader creates an instance of gocsv.Reader from io.Reader. The first row values are considered as headers. If it is required to set headers manually, you might use NewReaderWithHeaders method.

func NewReaderWithHeaders added in v1.2.0

func NewReaderWithHeaders[T any](csvFile io.Reader, headers []string) (*Reader[T], error)

NewReaderWithHeaders creates an instance of gocsv.Reader from io.Reader and defined headers. If no headers are provider, the first row values are considered as headers.

func (*Reader[T]) Next added in v1.2.0

func (r *Reader[T]) Next() (*T, error)

Next method returns next instance or error if any. io.EOF error means that there are no more records.

Directories

Path Synopsis
cmd
example_reader command

Jump to

Keyboard shortcuts

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