csv

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2025 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

This package implements a csv parser that extends the one supplied by the std lib to include marshalling and un-marshaling to struct values. Many options are provided beyond what the std lib csv package presents. This package relies on the iterator package to create streams of structs and string slices which are used to represent un-marshalling and marshalling respectively.

Within a csv file, string literals can be specified that follow the same rules as the std lib csv parser. (A quote literal can escape newlines and commas, a double double quote "" escapes a quote within a quote literal, etc...)

Index

Constants

This section is empty.

Variables

View Source
var (
	MalformedCSVFile   = errors.New("The supplied CSV file is not valid.")
	MalformedCSVStruct = errors.New("The supplied struct was not valid.")
	DuplicateColName   = errors.New("A column name was duplicated either in the struct definition or the file itself.")
	InvalidHeader      = errors.New("A header specified in the CSV file or options could not be mapped to the supplied struct using the supplied options. Make sure the specified field is exported.")
	InvalidStructField = errors.New("An error occurred while trying to set the struct field. Make sure the field is settable.")
	InvalidHeaders     = errors.New("The supplied list of headers is not valid with the supplied struct.")
)
View Source
var (
	InvalidOptionsFlag               = errors.New("Invalid optionsFlag")
	OPTIONS_FLAG       []optionsFlag = []optionsFlag{
		hasHeaders,
		ignoreHeaders,
		useStructTags,
		writeHeaders,
		headersSupplied,
		writeZeroValues,
		unknownOptionsFlag,
	}
)

Functions

func Flatten

func Flatten(elems iter.Iter[[]string], opts *options) iter.Iter[string]

Maps a stream of string slices to a stream of strings using a csv format, which can then be written directly to a file. The options argument controls the behavior of the mapping process, see NewOptions for more information. If an error is encountered while mapping the stream of strings slices then iteration will stop and that error will be returned.

func FromStructs

func FromStructs[R any](src iter.Iter[R], opts *options) iter.Iter[[]string]

Takes an iterator stream of structs with type specified by the generic R type, treats each struct as a row of a row of a csv file, and maps that stream of struts to a stream of string slices. The options argument controls the behavior of the mapping process, see NewOptions for more information. If an error is encountered while processing the stream of structs then iteration will stop and that error will be returned.

The data that is placed in the csv file based either on the struct field names, the struct field tags, or a combination of both depending on the options passed as well as what is available from the struct definition. The ordering of the columns is either determined by the ordering of the struct fields or by the options passed.

When writing the data to the file, the following data types are supported:

  • All integer types (int,int8,int16,int32,int64)
  • All unsigned integer types (uint,uint8,uint16,uint32,uint64)
  • All float types (float32,float64)
  • Strings
  • Booleans
  • Date time formats (format specified by options)

The above data types represent the basic types present within the language, and this mirrors the limitations of a CSV file. For more expressive representations of arrays and objects consider using JSON instead.

func NewOptions

func NewOptions() *options

Returns a new options struct initialized with the default values.

func NewOptionsFlag

func NewOptionsFlag() optionsFlag

func Parse

func Parse(src string, opts *options) iter.Iter[[]string]

Parses the supplied csv file to produce a stream of string slices, which can then be passed to the ToStructs function to unmarshall the data into structs. The options argument controls the behavior of the parsing process, see NewOptions for more information. If an error is encountered while processing the stream of structs then iteration will stop and that error will be returned.

func ToStructs

func ToStructs[R any](src iter.Iter[[]string], opts *options) iter.Iter[R]

Takes an iterator stream of string slices, treats each slice as a row of a csv file, and maps that stream of slices to a stream of structs with the type specified by the generic R type. The options argument controls the behavior of the mapping process, see NewOptions for more information. If an error is encountered while processing the stream of slices then iteration will stop and that error will be returned.

Data is placed in struct fields based on either the CSV headers or the ordering of the fields in the struct, depending on the options passed to the function. Any fields of the struct that are not set given the header mapping will be left zero-initilized. Any blank values in the CSV file will also be zero initilized.

When parsing data the following data types are supported:

  • All integer types (int,int8,int16,int32,int64)
  • All unsigned integer types (uint,uint8,uint16,uint32,uint64)
  • All float types (float32,float64)
  • Strings
  • Booleans
  • Date time formats (format specified by options)

The above data types represent the basic types present within the language, and this mirrors the limitations of a CSV file. For more expressive representations of arrays and objects consider using JSON instead.

Types

This section is empty.

Jump to

Keyboard shortcuts

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