csvx

package
v0.4.6 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2026 License: MPL-2.0-no-copyleft-exception Imports: 16 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package csvx provides nullable row-oriented data types that bridge CSV, JSON, and SQL. Each type implements MarshalCSV/UnmarshalCSV, MarshalJSON/UnmarshalJSON, and driver.Value/Scan so the same struct can be read from a sheet, written to a file, and scanned from a database.

It also provides generic Parse/Serialize functions for CSV, TSV, JSON, and JSONL, delimiter-specific string and integer slices, plus text normalization helpers (NormalizeEmail, NormalizePhone).

Package csvx provides text normalization and format detection helpers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContentType

func ContentType(f Format) string

ContentType maps canonical format to MIME type.

func DetectFormat

func DetectFormat(filename string) string

DetectFormat returns the format from a file extension.

func Marshalers

func Marshalers() *csvutil.Marshalers

Marshalers returns csvutil.Marshalers for generic types that don't have their own MarshalCSV method.

func NormalizeEmail

func NormalizeEmail(raw string) string

NormalizeEmail trims whitespace and lowercases.

func NormalizePhone

func NormalizePhone(raw string) string

NormalizePhone strips non-digit characters and formats 10-digit US numbers to (xxx) xxx-xxxx. Non-US or non-standard numbers are returned digit-only.

func Parse

func Parse[T any](r io.Reader, format string) ([]T, error)

Parse parses records from an io.Reader in the requested format.

func ParseCSV

func ParseCSV[T any](r csvutil.Reader) ([]T, error)

ParseCSV reads CSV/TSV from a csvutil Reader using csvutil streaming. The reader must already have its delimiter configured. Uses json struct tags and csvx marshalers for type handling.

Blank records — rows where every cell is empty or whitespace-only — are skipped everywhere, including before the header. Human-maintained sources routinely carry stray blank lines (e.g. a Google Sheets export with an empty row above the header); without skipping, the first blank row would be taken as the header and every data row would decode to zero values. Skipping happens in a streaming reader wrapper: records are decoded one at a time and only one record is buffered at a time.

A csv.Reader is a valid csvutil.Reader and can be passed directly.

func ParseJSON

func ParseJSON[T any](r io.Reader) ([]T, error)

ParseJSON decodes a JSON array [{…}, {…}] from r into a slice of T. Returns nil, nil on empty input (io.EOF on first decode).

func ParseJSONL

func ParseJSONL[T any](r io.Reader) ([]T, error)

ParseJSONL reads JSONL (one JSON object per line) from r into a slice of T. Each line is decoded independently; decoding stops at io.EOF.

func ParseJSONWithEnvelope

func ParseJSONWithEnvelope[T any](r io.Reader) (T, error)

ParseJSONWithEnvelope decodes a JSON envelope from r into T. T should be a struct with json tags matching the envelope fields (e.g. {"groups": [...], "kind": "...", "etag": "..."}). Returns zero value, nil on empty input (io.EOF on first decode).

func Serialize

func Serialize[T any](w io.Writer, values []T, format string) error

Serialize writes values in the specified format.

func SerializeCSV

func SerializeCSV[T any](cw *csv.Writer, items []T) error

SerializeCSV writes items as CSV/TSV to a csv.Writer using json struct tags and csvx marshalers. The caller must have configured Comma (or left it at the default comma).

func SerializeJSON

func SerializeJSON[T any](enc *json.Encoder, items []T) error

SerializeJSON writes items as a JSON array [{…}, {…}] using the provided json.Encoder.

func SerializeJSONL

func SerializeJSONL[T any](enc *json.Encoder, items []T) error

SerializeJSONL writes items as JSONL (one JSON object per line) using the provided json.Encoder.

func Unmarshalers

func Unmarshalers() *csvutil.Unmarshalers

Unmarshalers returns csvutil.Unmarshalers for generic types that don't have their own UnmarshalCSV method.

Types

type Bool

type Bool struct {
	Bool  bool
	Valid bool
}

Bool is a nullable boolean that implements CSV, JSON, and driver interfaces.

func (Bool) IsNil

func (b Bool) IsNil() bool

func (Bool) MarshalCSV

func (b Bool) MarshalCSV() ([]byte, error)

func (Bool) MarshalJSON

func (b Bool) MarshalJSON() ([]byte, error)

func (*Bool) Scan

func (b *Bool) Scan(value any) error

func (*Bool) UnmarshalCSV

func (b *Bool) UnmarshalCSV(data []byte) error

func (*Bool) UnmarshalJSON

func (b *Bool) UnmarshalJSON(data []byte) error

func (Bool) Value

func (b Bool) Value() (driver.Value, error)

type BoolishTimestamp

type BoolishTimestamp struct {
	Valid     bool
	Bool      bool
	Timestamp time.Time
	Error     error
	// contains filtered or unexported fields
}

BoolishTimestamp accepts optional true/false-like values and dates or timestamps from human-edited CSV/TSV data. Valid reports that a value was supplied; Bool reports its true-like meaning. Error records an unrecognized non-empty value without making the CSV decoder reject the row. Raw reports the trimmed original value for unrecognized values so callers can apply their own column-specific word policy (e.g. reading "disabled" as true in a Disabled column).

func (BoolishTimestamp) IsZero

func (v BoolishTimestamp) IsZero() bool

IsZero lets encoders treat the zero value as an omitted source value.

func (BoolishTimestamp) MarshalCSV

func (v BoolishTimestamp) MarshalCSV() ([]byte, error)

MarshalCSV writes a normalized usable record. An unrecognized value is conservatively serialized as the current UTC timestamp.

func (BoolishTimestamp) Raw added in v0.4.6

func (v BoolishTimestamp) Raw() string

Raw returns the trimmed original value that was parsed, retained for unrecognized values so callers can apply their own word policy.

func (*BoolishTimestamp) UnmarshalCSV

func (v *BoolishTimestamp) UnmarshalCSV(data []byte) error

UnmarshalCSV parses a human value. Recognized true-like values and dates set Bool; recognized false-like values leave it unset. Unrecognized non-empty values are conservatively false with the error retained so the collection mapper can apply its own policy.

type Comma

type Comma struct{}

Comma separates values with a comma.

func (Comma) Separator

func (Comma) Separator() string

type Date

type Date struct {
	Time  time.Time
	Valid bool
}

Date is a nullable date that implements CSV, JSON, and driver interfaces.

func (Date) IsNil

func (d Date) IsNil() bool

func (Date) MarshalCSV

func (d Date) MarshalCSV() ([]byte, error)

func (Date) MarshalJSON

func (d Date) MarshalJSON() ([]byte, error)

func (*Date) Scan

func (d *Date) Scan(value any) error

func (*Date) UnmarshalCSV

func (d *Date) UnmarshalCSV(data []byte) error

func (*Date) UnmarshalJSON

func (d *Date) UnmarshalJSON(data []byte) error

func (Date) Value

func (d Date) Value() (driver.Value, error)

type Delimiter

type Delimiter interface {
	Separator() string
}

Delimiter selects the separator used by a delimited value's CSV form.

type Float64

type Float64 struct {
	Float64 float64
	Valid   bool
}

Float64 is a nullable float that implements CSV, JSON, and driver interfaces.

func (Float64) MarshalCSV

func (v Float64) MarshalCSV() ([]byte, error)

func (Float64) MarshalJSON

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

func (*Float64) Scan

func (v *Float64) Scan(value any) error

func (*Float64) UnmarshalCSV

func (v *Float64) UnmarshalCSV(data []byte) error

func (*Float64) UnmarshalJSON

func (v *Float64) UnmarshalJSON(data []byte) error

func (Float64) Value

func (v Float64) Value() (driver.Value, error)

type Format

type Format string

Format is a canonical output format.

const (
	// DefaultFormat is csv — project rule: all new routes default to CSV.
	DefaultFormat Format = "csv"
	FormatCSV     Format = "csv"
	FormatTSV     Format = "tab"
	FormatJSON    Format = "json"
	FormatJSONL   Format = "jsonl"
)

func ResolveFormat

func ResolveFormat(r *http.Request) (Format, error)

ResolveFormat returns the output format for a request. ?format= has priority. Otherwise the Accept header's best q-matched media type is used. Per project rule, the default (empty Accept, */*, or no match) is CSV. Returns an error only for an explicit unknown ?format= value.

type Hex

type Hex []byte

Hex marshals and unmarshals bytes to a base16 (hex) string as JSON or CSV.

func (Hex) MarshalCSV

func (h Hex) MarshalCSV() ([]byte, error)

func (Hex) MarshalJSON

func (h Hex) MarshalJSON() ([]byte, error)

func (*Hex) UnmarshalCSV

func (h *Hex) UnmarshalCSV(src []byte) error

func (*Hex) UnmarshalJSON

func (h *Hex) UnmarshalJSON(src []byte) error

type Int64

type Int64 struct {
	Int64 int64
	Valid bool
}

Int64 is a nullable integer that implements CSV, JSON, and driver interfaces.

func (Int64) MarshalCSV

func (v Int64) MarshalCSV() ([]byte, error)

func (Int64) MarshalJSON

func (v Int64) MarshalJSON() ([]byte, error)

func (*Int64) Scan

func (v *Int64) Scan(value any) error

func (*Int64) UnmarshalCSV

func (v *Int64) UnmarshalCSV(data []byte) error

func (*Int64) UnmarshalJSON

func (v *Int64) UnmarshalJSON(data []byte) error

func (Int64) Value

func (v Int64) Value() (driver.Value, error)

type Ints

type Ints[D Delimiter] []int64

Ints is an int64 slice with a delimiter-specific CSV form. Its SQL and JSON forms are JSON arrays.

func (Ints[D]) MarshalCSV

func (s Ints[D]) MarshalCSV() ([]byte, error)

MarshalCSV implements csvutil.Marshaler.

func (*Ints[D]) Scan

func (s *Ints[D]) Scan(value any) error

Scan implements sql.Scanner from a JSON array.

func (*Ints[D]) UnmarshalCSV

func (s *Ints[D]) UnmarshalCSV(data []byte) error

UnmarshalCSV implements csvutil.Unmarshaler.

func (Ints[D]) Value

func (s Ints[D]) Value() (driver.Value, error)

Value implements driver.Valuer as a JSON array.

type RFCBase64

type RFCBase64 []byte

RFCBase64 unmarshals to bytes and marshals to standard base64.

func (RFCBase64) MarshalJSON

func (s RFCBase64) MarshalJSON() ([]byte, error)

MarshalJSON implements JSON marshaling to standard base64.

func (RFCBase64) String

func (s RFCBase64) String() string

func (*RFCBase64) UnmarshalJSON

func (s *RFCBase64) UnmarshalJSON(data []byte) error

UnmarshalJSON implements JSON unmarshaling from standard base64.

type RawJSON

type RawJSON json.RawMessage

RawJSON holds a JSON document as raw bytes. CSV/TSV encode it as JSON text, JSON encoding passes it through unchanged, and jsonb binds it as bytes.

func (RawJSON) MarshalCSV

func (r RawJSON) MarshalCSV() ([]byte, error)

MarshalCSV renders the document as JSON text.

func (RawJSON) MarshalJSON

func (r RawJSON) MarshalJSON() ([]byte, error)

MarshalJSON renders the stored document verbatim.

func (RawJSON) String

func (r RawJSON) String() string

String returns the document as a string.

func (*RawJSON) UnmarshalCSV

func (r *RawJSON) UnmarshalCSV(data []byte) error

UnmarshalCSV stores the field as the JSON document.

func (*RawJSON) UnmarshalJSON

func (r *RawJSON) UnmarshalJSON(data []byte) error

UnmarshalJSON stores the document verbatim after validation.

type Space

type Space struct{}

Space separates values with whitespace.

func (Space) Separator

func (Space) Separator() string

type Strings

type Strings[D Delimiter] []string

Strings is a string slice with a delimiter-specific CSV form. Its SQL and JSON forms are JSON arrays.

func (Strings[D]) MarshalCSV

func (s Strings[D]) MarshalCSV() ([]byte, error)

MarshalCSV implements csvutil.Marshaler.

func (*Strings[D]) Scan

func (s *Strings[D]) Scan(value any) error

Scan implements sql.Scanner from a JSON array.

func (*Strings[D]) UnmarshalCSV

func (s *Strings[D]) UnmarshalCSV(data []byte) error

UnmarshalCSV implements csvutil.Unmarshaler.

func (Strings[D]) Value

func (s Strings[D]) Value() (driver.Value, error)

Value implements driver.Valuer as a JSON array.

type Tab

type Tab struct{}

Tab separates values with a tab.

func (Tab) Separator

func (Tab) Separator() string

type Text

type Text struct {
	String string
	Valid  bool
}

Text is a nullable string that implements CSV, JSON, and driver interfaces.

func (Text) IsNil

func (t Text) IsNil() bool

func (Text) MarshalCSV

func (t Text) MarshalCSV() ([]byte, error)

func (Text) MarshalJSON

func (t Text) MarshalJSON() ([]byte, error)

func (*Text) Scan

func (t *Text) Scan(value any) error

func (*Text) UnmarshalCSV

func (t *Text) UnmarshalCSV(data []byte) error

func (*Text) UnmarshalJSON

func (t *Text) UnmarshalJSON(data []byte) error

func (Text) Value

func (t Text) Value() (driver.Value, error)

type Timestamp

type Timestamp struct {
	Time  time.Time
	Valid bool
}

Timestamp is a nullable timestamp (without timezone) that implements CSV, JSON, and driver interfaces.

func (Timestamp) IsNil

func (t Timestamp) IsNil() bool

func (Timestamp) MarshalCSV

func (t Timestamp) MarshalCSV() ([]byte, error)

func (Timestamp) MarshalJSON

func (t Timestamp) MarshalJSON() ([]byte, error)

func (*Timestamp) Scan

func (t *Timestamp) Scan(value any) error

func (*Timestamp) UnmarshalCSV

func (t *Timestamp) UnmarshalCSV(data []byte) error

func (*Timestamp) UnmarshalJSON

func (t *Timestamp) UnmarshalJSON(data []byte) error

func (Timestamp) Value

func (t Timestamp) Value() (driver.Value, error)

type Timestamptz

type Timestamptz struct {
	Time  time.Time
	Valid bool
}

Timestamptz is a nullable timestamp with timezone that implements CSV, JSON, and driver interfaces. Time is stored in UTC.

func (Timestamptz) IsNil

func (t Timestamptz) IsNil() bool

func (Timestamptz) MarshalCSV

func (t Timestamptz) MarshalCSV() ([]byte, error)

func (Timestamptz) MarshalJSON

func (t Timestamptz) MarshalJSON() ([]byte, error)

func (*Timestamptz) Scan

func (t *Timestamptz) Scan(value any) error

func (*Timestamptz) UnmarshalCSV

func (t *Timestamptz) UnmarshalCSV(data []byte) error

func (*Timestamptz) UnmarshalJSON

func (t *Timestamptz) UnmarshalJSON(data []byte) error

func (Timestamptz) Value

func (t Timestamptz) Value() (driver.Value, error)

type URLBase64

type URLBase64 []byte

URLBase64 unmarshals to bytes and marshals to a raw url base64 string

func (URLBase64) MarshalCSV

func (s URLBase64) MarshalCSV() ([]byte, error)

func (URLBase64) MarshalJSON

func (s URLBase64) MarshalJSON() ([]byte, error)

MarshalJSON implements JSON marshaling to URL-safe base64.

func (URLBase64) String

func (s URLBase64) String() string

func (*URLBase64) UnmarshalCSV

func (s *URLBase64) UnmarshalCSV(src []byte) error

func (*URLBase64) UnmarshalJSON

func (s *URLBase64) UnmarshalJSON(data []byte) error

UnmarshalJSON implements JSON unmarshaling from URL-safe base64.

type UUID

type UUID struct {
	Bytes [16]byte
	Valid bool
}

UUID is a nullable UUID that implements CSV, JSON, and driver interfaces.

func (UUID) IsNil

func (u UUID) IsNil() bool

func (UUID) MarshalCSV

func (u UUID) MarshalCSV() ([]byte, error)

func (UUID) MarshalJSON

func (u UUID) MarshalJSON() ([]byte, error)

func (*UUID) Scan

func (u *UUID) Scan(value any) error

func (UUID) String

func (u UUID) String() string

func (*UUID) UnmarshalCSV

func (u *UUID) UnmarshalCSV(data []byte) error

func (*UUID) UnmarshalJSON

func (u *UUID) UnmarshalJSON(data []byte) error

func (UUID) Value

func (u UUID) Value() (driver.Value, error)

Jump to

Keyboard shortcuts

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