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 ¶
RowIterator defines a function type for iterating over rows.