reader

package
v1.7.0 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2025 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Parse a whitespace separated list of values

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrFieldCount       = errors.New("wrong number of fields")
	ErrLineFeedTerm     = errors.New("line feed terminated before the line end end")
	ErrInvalidNull      = errors.New("null `-` specifier cannot be included without white space surrounding, unless it is the last value in the line. To record a literal `-` please wrap the value in double quotes")
	ErrBareQuote        = errors.New("bare \" in non-quoted-field")
	ErrReaderEnded      = errors.New("reader ended, nothing left to read")
	ErrCommentPlacement = errors.New("comments should be the last elements in a row, if immediate preceding lines are null, they cannot be omitted and must be explicitly declared")
)
View Source
var (
	ErrFieldNotFound = errors.New("field does not exist")
	ErrEndOfLine     = errors.New("no more fields left in this line")
)

Functions

func Unmarshal added in v1.5.0

func Unmarshal(d []byte, v any) error

Unmarshal a slice of bytes into a struct `v`.

Will use the struct tag `wsv` to unmarshal the input.

Types

type Line added in v1.1.0

type Line interface {
	// Returns the field value at the 0-index or `ErrFieldNotFound` if out of bounds
	Field(fi int) (*internal.Field, error)
	// Get the value of comment for the line
	Comment() string
	// Get the line number
	LineNumber() int
	// A count of the number of data fields in the line
	FieldCount() int
	// Get the next field value, or error if at the end of the line for data
	NextField() (*internal.Field, error)
	// Returns true if the line is a slice of headers
	IsHeaderLine() bool
	// Returns serialized values of all fields on a line
	FieldsValues() []string
	// All the fields in the line
	Fields() []internal.Field
}

func Parse

func Parse(wsvFile string) ([]Line, error)

Takes a file path and attempts to read the document as a WSV document

- Will attempt to parse using the default `NewReader()` and return slice of lines it was able to reader

- Can return a *PathError or *ParseError

type Reader

type Reader struct {
	IncludesHeader bool
	IsTabular      bool

	NullTrailingColumns bool

	AllowPartialError bool
	// contains filtered or unexported fields
}

A WSV Document Reader

func NewReader

func NewReader(r io.Reader) *Reader

Creates a new WSV NewReader

- By default the first non-empty and non-comment line is considered the header

- By default it expects a tabular [each record has the same number of fields] document

- By default omitted trailing fields for a record are allowed

func (*Reader) ColumnNameOf

func (r *Reader) ColumnNameOf(i int) (*string, error)

Return the column name at the index i, will return "" if not found

func (*Reader) CurrentRow

func (r *Reader) CurrentRow() int

Returns the line number of the reader

func (*Reader) Headers

func (r *Reader) Headers() []string

Returns a slice of headers for a WSV

func (*Reader) IndexedAt

func (r *Reader) IndexedAt(n string) []int

Return the index of a column name

func (*Reader) Read

func (r *Reader) Read() (Line, error)

Reads the current line of a reader and returns a *Line.

- If `r.IsTabular == true` and the current line being parsed has more fields than amount of headers `r.Read()` returns a the *Line along with the error *ParseError.

- If the record contains a field that cannot be parsed, `r.Read()` returns a *Line with as many records as it could before encountering an error. The partial record contains all fields read before the error.

- If there is no data left to be read, `r.Read()` returns a *Line with an empty slice Fields and io.EOF.

- Subsequent calls to `r.Read()` after io.EOF returns a nil and ErrReaderEnded

func (*Reader) ReadAll

func (r *Reader) ReadAll() (records []Line, err error)

Will read all lines of a reader until it reaches the end of a file or *ParseError

If `err == nil`, it has read the entire document successfully

func (*Reader) ToDocument

func (r *Reader) ToDocument() (*doc.Document, error)

Takes a reader an turns that into a document

type UnmarshalWSV added in v1.5.0

type UnmarshalWSV interface {
	UnmarshalWSV(value string, format string) error
}

Jump to

Keyboard shortcuts

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