Documentation
¶
Overview ¶
Package csv is a pure-Go (no cgo) reimplementation of Ruby's CSV standard library — the parser and generator behind MRI 4.0.5's csv gem (3.3.5). It reproduces Ruby's dialect options (col_sep / row_sep / quote_char), RFC-style quoting with embedded quotes, separators and newlines inside quoted fields, the headers model (CSV::Row / CSV::Table), the built-in field and header converters, and CSV::MalformedCSVError with MRI-exact messages and line numbers — all without any Ruby runtime.
It is the CSV backend for go-embedded-ruby, but is a standalone, reusable module: a sibling of go-ruby-regexp, go-ruby-erb and go-ruby-yaml.
Value model ¶
A parsed field is one of: nil (an empty, unquoted field — Ruby's nil), a string, or — when field converters run — an int, float64, a Date or a DateTime. Headers turn a parsed document into a Table of [Row]s offering access by header name or by index, mirroring CSV::Table / CSV::Row.
Index ¶
- func Generate(rows [][]any, opts Options) (string, error)
- func GenerateLine(row []any, opts Options) (string, error)
- func Parse(data string, opts Options) (any, error)
- func ParseLine(line string, opts Options) ([]any, error)
- func ParseRows(data string, opts Options) ([][]any, error)
- type Date
- type DateTime
- type MalformedCSVError
- type Options
- type Row
- type Symbol
- type Table
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Generate ¶
Generate renders many rows, mirroring Ruby's CSV.generate. When WriteHeaders is set with a []string Headers, the header row is emitted first.
func GenerateLine ¶
GenerateLine renders one row to a CSV record (with the row separator), mirroring Ruby's CSV.generate_line. A nil field becomes an empty (unquoted) string; other fields are stringified with Ruby's to_s rules for the common scalar types (the binding pre-stringifies anything exotic).
func Parse ¶
Parse parses an entire CSV document, mirroring Ruby's CSV.parse. Without headers it returns [][]any. With headers set it returns a *Table; callers that want the rows can use Table.Rows or the convenience Parse result type switch. The returned value is `any` because Ruby's CSV.parse returns either an Array of Arrays or a CSV::Table depending on :headers.
func ParseLine ¶
ParseLine parses a single CSV record, mirroring Ruby's CSV.parse_line. It returns the row's fields ([]any) or nil when the line is empty (Ruby returns nil for an empty string). A record may legitimately span several physical lines when a field is quoted and contains the row separator; ParseLine reads exactly one logical record and ignores any trailing records.
Types ¶
type Date ¶
type Date struct {
Year, Month, Day int
// Raw is the original field text Date.parse consumed (for the binding to
// re-parse faithfully if it needs the exact Ruby object).
Raw string
}
Date mirrors a Ruby Date produced by the :date converter. It carries the calendar date; the binding maps it to a Ruby Date object.
type DateTime ¶
DateTime mirrors a Ruby DateTime / Time produced by the :date_time / :time converters. Time reports whether it came from the :time converter (Ruby Time) rather than :date_time (Ruby DateTime).
type MalformedCSVError ¶
type MalformedCSVError struct {
// Reason is the human-readable cause, without the trailing " in line N.".
Reason string
// Line is the 1-based line number MRI reports for the error.
Line int
}
MalformedCSVError mirrors Ruby's CSV::MalformedCSVError. Its message and the 1-based line number match MRI byte-for-byte (e.g. "Unclosed quoted field in line 2.").
func (*MalformedCSVError) Error ¶
func (e *MalformedCSVError) Error() string
Error renders the message exactly as MRI's CSV::MalformedCSVError#message.
type Options ¶
type Options struct {
// ColSep is Ruby's :col_sep (the field separator). Empty means ",".
ColSep string
// RowSep is Ruby's :row_sep (the record separator). Empty means :auto,
// which detects "\r\n", "\r" or "\n" from the data when parsing and uses
// "\n" when generating.
RowSep string
// QuoteChar is Ruby's :quote_char. Empty means '"'. Set NoQuote to disable
// quoting entirely (Ruby's quote_char: nil).
QuoteChar string
// NoQuote disables quote processing (Ruby quote_char: nil): quotes are then
// ordinary characters on both parse and generate.
NoQuote bool
// Headers selects header handling, mirroring Ruby's :headers:
// nil / false → no headers (plain rows)
// true → first row is the header row
// "first_row" → same as true
// []string{...} → explicit header names
Headers any
// ReturnHeaders mirrors :return_headers — when true and Headers is set, the
// header row is itself emitted as a Row (with HeaderRow true).
ReturnHeaders bool
// WriteHeaders mirrors :write_headers — when generating with Headers set,
// emit the header row first.
WriteHeaders bool
// Converters lists built-in field converter names (e.g. "integer",
// "float", "numeric", "date", "date_time", "time", "all") applied to each
// parsed field. Unknown names are ignored, matching nothing.
Converters []string
// HeaderConverters lists built-in header converter names ("downcase",
// "symbol", "symbol_raw") applied to each header.
HeaderConverters []string
// SkipBlanks mirrors :skip_blanks — drop wholly empty rows when parsing.
SkipBlanks bool
// SkipLines mirrors :skip_lines — a Go regexp (RE2) source; lines whose raw
// text matches are skipped. Empty disables it.
SkipLines string
// LiberalParsing mirrors :liberal_parsing — tolerate quotes that would
// otherwise be malformed, keeping the raw text instead of erroring.
LiberalParsing bool
// Strip mirrors :strip. StripSpace strips leading/trailing whitespace from
// unquoted fields; StripChars (when non-empty) strips those specific
// characters instead (Ruby strip: "x").
StripSpace bool
StripChars string
// ForceQuotes mirrors :force_quotes — quote every generated field.
ForceQuotes bool
// QuoteEmpty mirrors :quote_empty (default true) — quote empty (but
// non-nil) string fields when generating. Set QuoteEmptySet to make a false
// value meaningful (otherwise the zero value is treated as the default true).
QuoteEmpty bool
QuoteEmptySet bool
// NilValue mirrors :nil_value — value substituted for an empty unquoted
// field on parse (default nil). NilValueSet distinguishes "set to nil"
// from "unset".
NilValue any
NilValueSet bool
// EmptyValue mirrors :empty_value — value substituted for an empty *quoted*
// field on parse (default ""). EmptyValueSet distinguishes "set to nil".
EmptyValue any
EmptyValueSet bool
}
Options holds the subset of Ruby CSV options this library understands. The zero value is *not* MRI's default; use DefaultOptions (or leave the relevant fields blank and let [normalize] fill them) to get Ruby's defaults (col_sep ",", row_sep :auto, quote_char '"').
func DefaultOptions ¶
DefaultOptions returns a copy of opts with Ruby's defaults filled in for any blank dialect field, so callers can reason about effective separators.
type Row ¶
type Row struct {
// Headers are the header values (string or Symbol after header conversion),
// shared with the owning Table.
Headers []any
// Fields are this row's values, positionally aligned with Headers.
Fields []any
// HeaderRow reports whether this Row is the header row itself (emitted only
// when return_headers is set), mirroring CSV::Row#header_row?.
HeaderRow bool
}
Row mirrors CSV::Row: an ordered list of fields paired with the table's headers, offering access by header name or by index. It is the per-record type the binding maps to a CSV::Row.
func (*Row) At ¶
At returns the value at index i, mirroring Row#[] with an integer argument. A negative index counts from the end (Ruby semantics). ok is false when out of range.
type Symbol ¶
type Symbol string
Symbol mirrors a Ruby Symbol produced by the :symbol / :symbol_raw header converters.
type Table ¶
type Table struct {
// Headers are the (possibly converted) header values.
Headers []any
// Rows are the data rows. When ReturnHeaders was set, the first element is
// the header Row (HeaderRow true).
Rows []*Row
}
Table mirrors CSV::Table: a header row plus its data Rows.
