parse

package
v0.1.1-0...-b1a8328 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package parse turns the messy textual fields of a bank CSV (amounts, dates, raw rows) into the typed values the rest of ledgerly works with.

Index

Constants

This section is empty.

Variables

View Source
var DefaultDateLayouts = []string{
	"2006-01-02",
	"02.01.2006",
	"2.1.2006",
	"02/01/2006",
	"2/1/2006",
	"2006/01/02",
	"02-01-2006",
}

DefaultDateLayouts are tried, in order, when a profile does not pin down its own date format. ISO comes first because it is unambiguous; the German day-first formats follow. Padded and non-padded variants are both listed so "2.1.2026" and "02.01.2026" both parse.

Functions

func ParseAmount

func ParseAmount(s string, dec DecimalSeparator) (model.Money, error)

ParseAmount parses a monetary string into model.Money (minor units). It copes with grouping separators, either decimal convention, surrounding whitespace, currency symbols and a leading sign.

func ParseDate

func ParseDate(s string, layouts []string) (model.Date, error)

ParseDate parses a date string using the supplied layouts, falling back to DefaultDateLayouts when none are given. The first layout that matches wins.

Types

type DecimalSeparator

type DecimalSeparator int

DecimalSeparator tells the amount parser how to interpret '.' and ',' in a number. Auto guesses from the shape of the string, which is good enough for most exports; a bank profile can pin it down when the data is ambiguous.

const (
	// DecimalAuto guesses the decimal separator from the input.
	DecimalAuto DecimalSeparator = iota
	// DecimalComma treats ',' as the decimal separator (1.234,56).
	DecimalComma
	// DecimalDot treats '.' as the decimal separator (1,234.56).
	DecimalDot
)

type ReadOptions

type ReadOptions struct {
	// Delimiter is the field separator; 0 means auto-detect.
	Delimiter rune
	// SkipRows drops this many decoded lines before the header is read, for
	// banks that print a preamble above the actual table.
	SkipRows int
}

ReadOptions tunes how a CSV is read. The zero value auto-detects the delimiter and skips nothing, which is what ReadCSV uses.

type Table

type Table struct {
	Header    []string
	Rows      [][]string
	Delimiter rune
}

Table is a decoded CSV file: a header row plus the data rows, with the delimiter that was used so callers can report it.

func ReadCSV

func ReadCSV(r io.Reader) (*Table, error)

ReadCSV reads a CSV from r, auto-detecting both the byte encoding and the field delimiter. German bank exports are frequently semicolon-separated and encoded as Windows-1252 with a UTF-8 BOM, so we cannot assume the Go defaults.

func ReadCSVOpts

func ReadCSVOpts(r io.Reader, opts ReadOptions) (*Table, error)

ReadCSVOpts reads a CSV honouring the given options.

func ReadCSVWithDelimiter

func ReadCSVWithDelimiter(r io.Reader, delim rune) (*Table, error)

ReadCSVWithDelimiter is like ReadCSV but uses the supplied delimiter instead of guessing it.

Jump to

Keyboard shortcuts

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