csv

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2024 License: MIT Imports: 5 Imported by: 0

README

CSV Reader Library

This library is built on top of Go's encoding/csv package, providing additional functionality to read, process, and construct CSV files in a structured and easy-to-use manner. It enhances the basic CSV handling capabilities with features such as header management, row iteration, grouping, and object conversion, making it easier to work with CSV data in various applications.

Additionally, it leverages the github.com/jszwec/csvutil library for deserializing CSV rows into objects, simplifying the conversion process.

Features

  • Read CSV files with different separators.
  • Manage headers of CSV files.
  • Iterate over the rows of the CSV file.
  • Group rows by columns.
  • Convert rows to specific objects.
  • Handle CSV files with or without headers.
  • Construct CSV files with specified data and options.

Construction Methods

NewCSVReaderFromPath

Creates a new CSV reader from a file path with optional ReaderOptions.

func NewCSVReaderFromPath(path string, optFns ...func (*ReaderOptions)) (Reader, error)
  • path string: The path to the CSV file.
  • optFns ...func(*ReaderOptions): Optional functions to set ReaderOptions.
NewCSVReader

Creates a new CSV reader from an io.Reader with optional ReaderOptions.

func NewCSVReader(r io.Reader, optFns ...func(*ReaderOptions)) (Reader, error)
  • r io.Reader: The reader to read CSV data from.
  • optFns ...func(*ReaderOptions): Optional functions to set ReaderOptions.
ToReaderSeparator

Converts a string to a ReaderSeparator.

func ToReaderSeparator(separator string) (ReaderSeparator, bool)
  • separator string: The string representation of the separator.
  • Returns ReaderSeparator: The corresponding ReaderSeparator constant.
  • Returns bool: Indicates if the conversion was successful.

Additionally, you can create a ReaderSeparator by casting a rune type.

Example
separator := ReaderSeparator(',')

Available Methods

Reader
Methods
  • SetHeader(header []string): Sets the header of the CSV file.
  • Iterator() RowIterator: Returns an iterator for iterating over rows.
  • GetHeaders() []string: Returns the headers of the CSV file.
  • TotalRows() int: Returns the total number of rows in the CSV file.
  • GroupByColumnIndex(columnIndex int) map[string][]Row: Groups rows by the value at the specified column index.
  • GroupByColumnIndexes(columnIndexes ...int) map[string][]Row: Groups rows by the values at the specified column indexes.
  • GroupByColumnName(columnName string) map[string][]Row: Groups rows by the value of the specified column name.
  • GroupByColumnNames(columnNames ...string) map[string][]Row: Groups rows by the values of the specified column names.
  • GetRow(index int) (Row, bool): Returns the row at the specified index.
  • RowToObjet(index int, obj any) (bool, error): Converts the row at the specified index to the specified object.
  • GetNextIndex(currentIndex int, cycle bool) int: Returns the next index based on the current index and cycle option.
  • ToObjects(objs []any) error: Converts all rows to the specified slice of objects.
Row
Methods
  • Value(columnName string) (string, bool): Returns the value of the specified column name.
  • Fields() []*RowField: Returns the fields of the row.
  • Values() []string: Returns the values of the row.
  • AsMap() map[string]string: Returns the row as a map with column names as keys.
  • LineNumber() int: Returns the line number of the row in the CSV file.
  • ToObject(obj any) error: Converts the row to the specified object.
RowField
Fields
  • Name string: The name of the field.
  • Value string: The value of the field.
ReaderOptions
Fields
  • NoHeader bool: Indicates if the CSV file has no header.
  • Separator ReaderSeparator: The separator used in the CSV file.
ReaderSeparator
Constants
  • CommaSeparator: Separator for comma (,).
  • SemicolonSeparator: Separator for semicolon (;).
  • TabSeparator: Separator for tab (\t).
  • PipeSeparator: Separator for pipe (|).

Example Usage

Here is an example demonstrating how to use the CSV Reader Library to read a CSV file and convert its rows into objects.

type ExampleStruct struct {
	Value1 string `csv:"value1"`
	Value2 string `csv:"value2"`
}

func main() {
	path := "./example/test.csv"

	// Create a new CSV reader from a file path with optional ReaderOptions
	reader, err := csvreader.NewCSVReaderFromPath(path)
	if err != nil {
		log.Fatalf("Error creating CSV reader: %v", err)
	}

	// Iterate over the rows and convert each row to an ExampleStruct object
	for item := range reader.Iterator() {
		var example ExampleStruct
		if err := item.ToObject(&example); err != nil {
			log.Fatalf("Error converting row to object: %v", err)
		}
		fmt.Printf("Value1: %s, Value2: %s\n", example.Value1, example.Value2)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Reader

type Reader interface {
	// SetHeader sets the header of the CSV file.
	SetHeader(header []string)

	// Iterator returns a RowIterator for iterating over rows.
	Iterator() RowIterator

	// GetHeaders returns the headers of the CSV file.
	GetHeaders() []string

	// TotalRows returns the total number of rows in the CSV file.
	TotalRows() int

	// GroupByColumnIndex groups rows by the value at the specified column index.
	GroupByColumnIndex(columnIndex int) map[string][]Row

	// GroupByColumnIndexes groups rows by the values at the specified column indexes.
	GroupByColumnIndexes(columnIndexes ...int) map[string][]Row

	// GroupByColumnName groups rows by the value of the specified column name.
	GroupByColumnName(columnName string) map[string][]Row

	// GroupByColumnNames groups rows by the values of the specified column names.
	GroupByColumnNames(columnNames ...string) map[string][]Row

	// GetRow returns the row at the specified index.
	GetRow(index int) (Row, bool)

	// RowToObjet converts the row at the specified index to the specified object.
	RowToObjet(index int, obj any) (bool, error)

	// GetNextIndex returns the next index based on the current index and cycle option.
	GetNextIndex(currentIndex int, cycle bool) int

	// ToObjects converts all rows to the specified slice of objects.
	ToObjects(objs []any) error
}

Reader defines the interface for reading CSV files and provides various methods to work with the data.

func NewCSVReader

func NewCSVReader(r io.Reader, optFns ...func(*ReaderOptions)) (Reader, error)

NewCSVReader creates a new CSV Reader from an io.Reader with optional ReaderOptions.

func NewCSVReaderFromPath

func NewCSVReaderFromPath(path string, optFns ...func(*ReaderOptions)) (Reader, error)

NewCSVReaderFromPath creates a new CSV Reader from a file path with optional ReaderOptions.

type ReaderOptions

type ReaderOptions struct {
	NoHeader  bool
	Separator ReaderSeparator
}

ReaderOptions holds options for configuring the CSV Reader.

type ReaderSeparator

type ReaderSeparator rune

ReaderSeparator defines the type for the separator used in the CSV file.

const (
	// CommaSeparator is used to separate fields with a comma.
	CommaSeparator ReaderSeparator = ','

	// SemicolonSeparator is used to separate fields with a semicolon.
	SemicolonSeparator ReaderSeparator = ';'

	// TabSeparator is used to separate fields with a tab.
	TabSeparator ReaderSeparator = '\t'

	// PipeSeparator is used to separate fields with a pipe.
	PipeSeparator ReaderSeparator = '|'
)

func ToReaderSeparator

func ToReaderSeparator(separator string) (ReaderSeparator, bool)

ToReaderSeparator converts a string to a ReaderSeparator.

type Row

type Row interface {
	// Value returns the value of the specified column name.
	Value(columnName string) (string, bool)

	// Fields returns the fields of the row.
	Fields() []*RowField

	// Values returns the values of the row.
	Values() []string

	// AsMap returns the row as a map with column names as keys.
	AsMap() map[string]string

	// LineNumber returns the line number of the row in the CSV file.
	LineNumber() int

	// ToObject converts the row to the specified object.
	ToObject(obj any) error
}

Row defines the interface for a row in the CSV file.

type RowField

type RowField struct {
	Name  string `json:"name" bson:"name"`
	Value string `json:"value" bson:"value"`
}

RowField represents a field in a row with a name and value.

type RowIterator

type RowIterator func(yield func(Row) bool)

RowIterator defines a function type for iterating over rows.

Jump to

Keyboard shortcuts

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