csvParse

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2025 License: MIT Imports: 10 Imported by: 1

README

go-csvParse

Wrapper that provides easy parsing for CSV files into maps

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cell

type Cell struct {
	Row    int // start row is 0
	Column int // start column is 0
}

type CellLocation

type CellLocation struct {
	Location Cell
	DataType DataType
	Name     string // Alias that will be used if not blank
	NameCell Cell   // Location for the name cell. Will be ignored if Name is not blank
}

Represents data that is within a single cell (not a table)

The cell may contain an associated cell that is the name of the cell or the name can be supplied directly.

func NewCellLocation

func NewCellLocation(location Cell, dataType DataType, name string, nameCell Cell) (*CellLocation, error)

func (*CellLocation) Parse

func (c *CellLocation) Parse(records [][]string) (name string, data any, err error)

parses a cell's information from records of a csv file

type ConcatCellLocation

type ConcatCellLocation struct {
	Cells     []Cell // List of cells in order to be concatenated
	Delimiter string // Delimiter to use between cells
	Name      string // Alias that will be used if not blank
	NameCell  Cell   // Location for the name cell. Will be ignored if Name is not blank
	DataType  DataType
}

Represents cell data that spans multiple consecutive rows or columns

func NewConcatCellLocation

func NewConcatCellLocation(cells []Cell, delimiter string, name string, nameCell Cell) (*ConcatCellLocation, error)

func (*ConcatCellLocation) Parse

func (c *ConcatCellLocation) Parse(records [][]string) (string, any, error)

parses and concatenates multiple cells information from records of a csv file

type Csv

type Csv struct {
	FilePathData        []FilePathData
	PreProcessor        []Processor
	CellLocations       []CellLocation
	ConcatCellLocations []ConcatCellLocation
	TableLocations      []TableLocation
	TimeFields          []TimeField
	IdField             IdField
	FaultOnDuplicate    bool
	KeepSpaces          bool
	StoreFileTime       bool
	FileTimeName        string
}

func NewCsvFile

func NewCsvFile(cellLocations []CellLocation, concatCellLocations []ConcatCellLocation, tableLocations []TableLocation) *Csv

func (Csv) MarshalJSON

func (c Csv) MarshalJSON() ([]byte, error)

func (*Csv) ParseFile

func (c *Csv) ParseFile(filePath string) ([]map[string]any, []string, error)

Parse a file and return all the results grouped.

Will output either a map[string]any or []map[string]any

func (*Csv) ParseFileNames

func (c *Csv) ParseFileNames(filePath string) (map[string]string, error)

func (*Csv) ParseRecords

func (c *Csv) ParseRecords(records [][]string) (any, error)

func (*Csv) ParseRecordsSegmented

func (c *Csv) ParseRecordsSegmented(records [][]string) (cells map[string]any, concatCells map[string]any, tables map[string]any, timestamps map[string]time.Time, err error)

parse a file and return all results per type of search

func (*Csv) Process

func (c *Csv) Process(filepath string) (result [][]byte, id []string, err error)

func (*Csv) UnmarshalJSON

func (c *Csv) UnmarshalJSON(data []byte) error

type DataType

type DataType int

Type mapping

const (
	DataTypeAuto  DataType = iota // Infers data type from data
	DataTypeSplit                 // Creates a column with the type appended to the name. Is treated like auto on everything but tables
	DataTypeString
	DataTypeInt64
	DataTypeFloat64
	DataTypeBool
	DataTypeDateTimeStyle0 // Assumes local time | YYYY-MM-DD HH:MM:SS
	DataTypeDateTimeStyle1 // Assumes local time | YYYY/MM/DD HH:MM:SS
)

func (DataType) Read

func (dt DataType) Read(value string) (any, error)

func (DataType) String

func (dt DataType) String() string

type FilePathData

type FilePathData struct {
	Name          string
	StartLocation uint16
	EndLocation   uint16

	// must have capture groups defined or no data will be found
	//
	// will be ignored if empty
	// capture group names will be used as the key
	CaptureRegex string
	// contains filtered or unexported fields
}

func (*FilePathData) Parse

func (f *FilePathData) Parse(filePath string) (output map[string]string, err error)

type Float64

type Float64 float64 // Custom type to handle mapping with decimals

func (Float64) MarshalJSON

func (f Float64) MarshalJSON() ([]byte, error)

type IdField

type IdField struct {
	// a grouping of parameters tht can be any depth into the data
	Parameters []IdFieldParameter

	Delimiter string
}

func (*IdField) Process

func (i *IdField) Process(data []map[string]any) ([]string, error)

Processes data and appends the ID field to all objects in the array

type IdFieldParameter

type IdFieldParameter struct {
	Mapping []any
}

type Processor

type Processor interface {
	GetName() string
	Execute(records [][]string) ([][]string, error)
	GetType() ProcessorType
	SetType()
}

type ProcessorFillRight

type ProcessorFillRight struct {
	Type  ProcessorType
	Name  string
	Start Cell
	End   Cell // Set Row = -1 if you want to do all rows, Set Column = -1 if you want to do all columns
}

Fills blank spaces using data to the left of the blank column

Example:

input := [][]string{
	{"r0c0", "r0c1", "r0c2"},
	{"r1c0", "r1c1", "r1c2", "r1c3"},
	{"r2c0", "r2c1", , , "r2c2"},
	{"r3c0", "r3c1", , , "r3c2"},
	{"r4c0", "r4c1", , , "r4c2"},
}
action:= &ProcessorFillRight{
  Start: Cell{Row: 2, Column: 0},
  End: Cell{Row: 4, Column: 4},
}
output := [][]string{
	{"r0c0", "r0c1", "r0c2"},
	{"r1c0", "r1c1", "r1c2", "r1c3"},
	{"r2c0", "r2c1", "r2c1", , "r2c2"},
	{"r3c0", "r3c1", "r3c1", , "r3c2"},
	{"r4c0", "r4c1", , , "r4c2"},
}

func (*ProcessorFillRight) Execute

func (a *ProcessorFillRight) Execute(records [][]string) ([][]string, error)

func (*ProcessorFillRight) GetName

func (a *ProcessorFillRight) GetName() string

func (*ProcessorFillRight) GetType

func (a *ProcessorFillRight) GetType() ProcessorType

func (*ProcessorFillRight) SetType

func (a *ProcessorFillRight) SetType()

type ProcessorMergeColumns

type ProcessorMergeColumns struct {
	Type      ProcessorType
	Name      string
	Start     Cell
	End       Cell // Set Row = -1 if you want to do all rows, Set Column = -1 if you want to do all columns
	Delimiter string
}

Merge one or more columns together across defined rows

Example:

input := [][]string{
	{"r0c0", "r0c1", "r0c2"},
	{"r1c0", "r1c1", "r1c2", "r1c3"},
	{"r2c0", "r2c1", "r2c2"},
	{"r3c0", "r3c1", "r3c2"},
	{"r4c0", "r4c1", "r4c2"},
}

action:= &ProcessorMergeColumns{
	Start: Cell{Row: 2, Column: 0},
	End:   Cell{Row: 4, Column: 2},
}

output := [][]string{
	{"r0c0", "r0c1", "r0c2"},
	{"r1c0", "r1c1", "r1c2", "r1c3"},
	{"r2c0r2c1", "r2c2"},
	{"r3c0r3c1", "r3c2"},
	{"r4c0", "r4c1", "r4c2"},
}

func (*ProcessorMergeColumns) Execute

func (a *ProcessorMergeColumns) Execute(records [][]string) ([][]string, error)

func (*ProcessorMergeColumns) GetName

func (a *ProcessorMergeColumns) GetName() string

func (*ProcessorMergeColumns) GetType

func (a *ProcessorMergeColumns) GetType() ProcessorType

func (*ProcessorMergeColumns) SetType

func (a *ProcessorMergeColumns) SetType()

type ProcessorMergeRows

type ProcessorMergeRows struct {
	Type           ProcessorType
	Name           string
	StartRow       int
	EndRow         int
	Delimiter      string
	TrimWhitespace bool
}

Merge one or more rows together.

Note: All rows must be the same length

Example:

input := [][]string{
	{"r0c0", "r0c1", "r0c2"},
	{"r1c0", "r1c1", "r1c2", "r1c3"},
	{"r2c0", "r2c1", "r2c2"},
	{"r3c0", "r3c1", "r3c2"},
	{"r4c0", "r4c1", "r4c2"},
}
action:= &ProcessorMergeRows{
  StartRow: 2,
  EndRow: 4,
}
output := [][]string{
	{"r0c0", "r0c1", "r0c2"},
	{"r1c0", "r1c1", "r1c2", "r1c3"},
	{"r2c0r3c0", "r2c1r3c1", "r2c2r3c2"},
	{"r4c0", "r4c1", "r4c2"},
}

func (*ProcessorMergeRows) Execute

func (a *ProcessorMergeRows) Execute(records [][]string) ([][]string, error)

func (*ProcessorMergeRows) GetName

func (a *ProcessorMergeRows) GetName() string

func (*ProcessorMergeRows) GetType

func (a *ProcessorMergeRows) GetType() ProcessorType

func (*ProcessorMergeRows) SetType

func (a *ProcessorMergeRows) SetType()

type ProcessorRemoveCellLeft

type ProcessorRemoveCellLeft struct {
	Type ProcessorType
	Name string
	Cell Cell
}

Removes a cell and shifts left

Example:

input := [][]string{
	{"r0c0", "r0c1", "r0c2"},
	{"r1c0", "r1c1", "r1c2"},
}
action:= &ProcessorFillRight{
  Cell: Cell{Row: 0, Column: 0},
}
output := [][]string{
	{"r0c1", "r0c2"},
	{"r1c0", "r1c1", "r1c2"},
}

func (*ProcessorRemoveCellLeft) Execute

func (a *ProcessorRemoveCellLeft) Execute(records [][]string) ([][]string, error)

func (*ProcessorRemoveCellLeft) GetName

func (a *ProcessorRemoveCellLeft) GetName() string

func (*ProcessorRemoveCellLeft) GetType

func (*ProcessorRemoveCellLeft) SetType

func (a *ProcessorRemoveCellLeft) SetType()

type ProcessorReplaceCell

type ProcessorReplaceCell struct {
	Type  ProcessorType
	Name  string
	Cell  Cell
	Value string
}

Replaces a cell's value with another

Example:

input := [][]string{
	{"r0c0", "r0c1", "r0c2"},
	{"r1c0", "r1c1", "r1c2"},
}
action:= &ProcessorFillRight{
  Cell: Cell{Row: 0, Column: 0},
  Value: "header1",
}
output := [][]string{
	{"header1", "r0c1", "r0c2"},
	{"r1c0", "r1c1", "r1c2"},
}

func (*ProcessorReplaceCell) Execute

func (a *ProcessorReplaceCell) Execute(records [][]string) ([][]string, error)

func (*ProcessorReplaceCell) GetName

func (a *ProcessorReplaceCell) GetName() string

func (*ProcessorReplaceCell) GetType

func (a *ProcessorReplaceCell) GetType() ProcessorType

func (*ProcessorReplaceCell) SetType

func (a *ProcessorReplaceCell) SetType()

type ProcessorTransposeRow

type ProcessorTransposeRow struct {
	Type     ProcessorType
	Name     string
	StartRow int
	EndRow   int
}

Transpose data from horizontal to vertical or vice versa

Example:

	input := [][]string{
		{"r0c0", "r0c1", "r0c2"},
		{"r1c0", "r1c1", "r1c2"},
	}
	action:= &ProcessorTransposeRow{
	  StartRow: 0,
   EndRow: -1
	}
	output := [][]string{
		{"r0c0", "r1c0"},
		{"r0c1", "r1c1"},
		{"r0c2", "r1c2"},
	}

func (*ProcessorTransposeRow) Execute

func (a *ProcessorTransposeRow) Execute(records [][]string) ([][]string, error)

func (*ProcessorTransposeRow) GetName

func (a *ProcessorTransposeRow) GetName() string

func (*ProcessorTransposeRow) GetType

func (a *ProcessorTransposeRow) GetType() ProcessorType

func (*ProcessorTransposeRow) SetType

func (a *ProcessorTransposeRow) SetType()

type ProcessorType

type ProcessorType int
const (
	ProcessorTypeMergeColumns ProcessorType = iota
	ProcessorTypeMergeRows
	ProcessorTypeFillRight
	ProcessorTypeReplaceCell
	ProcessorTypeTransposeRow
	ProcessorTypeRemoveCellLeft

	ProcessorTypeEnd // Used to confirm all types are accounted for
)

type TableLocation

type TableLocation struct {
	Name                string // name of the table. will be used instead of NameLocation if both are provided
	NameLocation        Cell   // if not 0, 0 it will be used to identify the name of the table
	StartCell           Cell
	EndCell             Cell       // if 0, 0 or equal to start cell it will not execute. Negative values will be treated as the end of the row or column
	HeaderNames         []string   // Ignored if TableHasHeader is true
	ColumnDataTypes     []DataType // Ignored if AutoColumnDataTypes is true.
	TableHasHeader      bool       // Whether the first row is the header row or not
	AutoColumnDataTypes bool       // if true will automatically infer column data types from the data
	SkipBlankData       bool       // Skips returning data for a cell if the cell is blank
	ParseAsArray        bool       // If true will parse the fields as an array instead of a JSON list
	ParseSingleRow      bool       // If true will only take the first row (or row beneath header) regardless of number of rows
	ParseSeparated      bool       // If true will segment table into multiple maps
	IgnoreNesting       bool       // If true will not nest the fields under the table name
}

Represents data that is within a table.

func NewTableLocation

func NewTableLocation(
	name string,
	nameLocation Cell,
	startCell Cell,
	endCell Cell,
	tableHasHeader bool,
	headerNames []string,
	autoColumnDataTypes bool,
	columnDataTypes []DataType,
	skipBlankData bool,
) (*TableLocation, error)

func (*TableLocation) Parse

func (t *TableLocation) Parse(records [][]string, keepSpaces bool) (string, any, error)

used to parse a given table based on a table location from csv records

type TimeField

type TimeField struct {
	Cells  []Cell // List of cells. Can be concatenated if the time field spans multiple areas
	Layout string
	Name   string
}

func NewTimeField

func NewTimeField(cells []Cell, Layout string) *TimeField

func (*TimeField) IsBlank

func (t *TimeField) IsBlank() bool

func (*TimeField) Parse

func (t *TimeField) Parse(records [][]string) (time.Time, error)

Jump to

Keyboard shortcuts

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