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 ¶
- func ContentType(f Format) string
- func DetectFormat(filename string) string
- func Marshalers() *csvutil.Marshalers
- func NormalizeEmail(raw string) string
- func NormalizePhone(raw string) string
- func Parse[T any](r io.Reader, format string) ([]T, error)
- func ParseCSV[T any](r csvutil.Reader) ([]T, error)
- func ParseJSON[T any](r io.Reader) ([]T, error)
- func ParseJSONL[T any](r io.Reader) ([]T, error)
- func ParseJSONWithEnvelope[T any](r io.Reader) (T, error)
- func Serialize[T any](w io.Writer, values []T, format string) error
- func SerializeCSV[T any](cw *csv.Writer, items []T) error
- func SerializeJSON[T any](enc *json.Encoder, items []T) error
- func SerializeJSONL[T any](enc *json.Encoder, items []T) error
- func Unmarshalers() *csvutil.Unmarshalers
- type Bool
- type BoolishTimestamp
- type Comma
- type Date
- type Delimiter
- type Float64
- type Format
- type Hex
- type Int64
- type Ints
- type RFCBase64
- type RawJSON
- type Space
- type Strings
- type Tab
- type Text
- type Timestamp
- func (t Timestamp) IsNil() bool
- func (t Timestamp) MarshalCSV() ([]byte, error)
- func (t Timestamp) MarshalJSON() ([]byte, error)
- func (t *Timestamp) Scan(value any) error
- func (t *Timestamp) UnmarshalCSV(data []byte) error
- func (t *Timestamp) UnmarshalJSON(data []byte) error
- func (t Timestamp) Value() (driver.Value, error)
- type Timestamptz
- func (t Timestamptz) IsNil() bool
- func (t Timestamptz) MarshalCSV() ([]byte, error)
- func (t Timestamptz) MarshalJSON() ([]byte, error)
- func (t *Timestamptz) Scan(value any) error
- func (t *Timestamptz) UnmarshalCSV(data []byte) error
- func (t *Timestamptz) UnmarshalJSON(data []byte) error
- func (t Timestamptz) Value() (driver.Value, error)
- type URLBase64
- type UUID
- func (u UUID) IsNil() bool
- func (u UUID) MarshalCSV() ([]byte, error)
- func (u UUID) MarshalJSON() ([]byte, error)
- func (u *UUID) Scan(value any) error
- func (u UUID) String() string
- func (u *UUID) UnmarshalCSV(data []byte) error
- func (u *UUID) UnmarshalJSON(data []byte) error
- func (u UUID) Value() (driver.Value, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ContentType ¶
ContentType maps canonical format to MIME type.
func DetectFormat ¶
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 ¶
NormalizeEmail trims whitespace and lowercases.
func NormalizePhone ¶
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 ParseCSV ¶
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 ¶
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 ¶
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 ¶
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 SerializeCSV ¶
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 ¶
SerializeJSON writes items as a JSON array [{…}, {…}] using the provided json.Encoder.
func SerializeJSONL ¶
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 ¶
Bool is a nullable boolean that implements CSV, JSON, and driver interfaces.
func (Bool) MarshalCSV ¶
func (Bool) MarshalJSON ¶
func (*Bool) UnmarshalCSV ¶
func (*Bool) UnmarshalJSON ¶
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 Date ¶
Date is a nullable date that implements CSV, JSON, and driver interfaces.
func (Date) MarshalCSV ¶
func (Date) MarshalJSON ¶
func (*Date) UnmarshalCSV ¶
func (*Date) UnmarshalJSON ¶
type Delimiter ¶
type Delimiter interface {
Separator() string
}
Delimiter selects the separator used by a delimited value's CSV form.
type Float64 ¶
Float64 is a nullable float that implements CSV, JSON, and driver interfaces.
func (Float64) MarshalCSV ¶
func (Float64) MarshalJSON ¶
func (*Float64) UnmarshalCSV ¶
func (*Float64) UnmarshalJSON ¶
type Format ¶
type Format string
Format is a canonical output format.
func ResolveFormat ¶
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 (Hex) MarshalJSON ¶
func (*Hex) UnmarshalCSV ¶
func (*Hex) UnmarshalJSON ¶
type Int64 ¶
Int64 is a nullable integer that implements CSV, JSON, and driver interfaces.
func (Int64) MarshalCSV ¶
func (Int64) MarshalJSON ¶
func (*Int64) UnmarshalCSV ¶
func (*Int64) UnmarshalJSON ¶
type Ints ¶
Ints is an int64 slice with a delimiter-specific CSV form. Its SQL and JSON forms are JSON arrays.
func (Ints[D]) MarshalCSV ¶
MarshalCSV implements csvutil.Marshaler.
func (*Ints[D]) UnmarshalCSV ¶
UnmarshalCSV implements csvutil.Unmarshaler.
type RFCBase64 ¶
type RFCBase64 []byte
RFCBase64 unmarshals to bytes and marshals to standard base64.
func (RFCBase64) MarshalJSON ¶
MarshalJSON implements JSON marshaling to standard base64.
func (*RFCBase64) UnmarshalJSON ¶
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 ¶
MarshalCSV renders the document as JSON text.
func (RawJSON) MarshalJSON ¶
MarshalJSON renders the stored document verbatim.
func (*RawJSON) UnmarshalCSV ¶
UnmarshalCSV stores the field as the JSON document.
func (*RawJSON) UnmarshalJSON ¶
UnmarshalJSON stores the document verbatim after validation.
type Strings ¶
Strings is a string slice with a delimiter-specific CSV form. Its SQL and JSON forms are JSON arrays.
func (Strings[D]) MarshalCSV ¶
MarshalCSV implements csvutil.Marshaler.
func (*Strings[D]) UnmarshalCSV ¶
UnmarshalCSV implements csvutil.Unmarshaler.
type Text ¶
Text is a nullable string that implements CSV, JSON, and driver interfaces.
func (Text) MarshalCSV ¶
func (Text) MarshalJSON ¶
func (*Text) UnmarshalCSV ¶
func (*Text) UnmarshalJSON ¶
type Timestamp ¶
Timestamp is a nullable timestamp (without timezone) that implements CSV, JSON, and driver interfaces.
func (Timestamp) MarshalCSV ¶
func (Timestamp) MarshalJSON ¶
func (*Timestamp) UnmarshalCSV ¶
func (*Timestamp) UnmarshalJSON ¶
type Timestamptz ¶
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
type URLBase64 ¶
type URLBase64 []byte
URLBase64 unmarshals to bytes and marshals to a raw url base64 string
func (URLBase64) MarshalCSV ¶
func (URLBase64) MarshalJSON ¶
MarshalJSON implements JSON marshaling to URL-safe base64.
func (*URLBase64) UnmarshalCSV ¶
func (*URLBase64) UnmarshalJSON ¶
UnmarshalJSON implements JSON unmarshaling from URL-safe base64.
type UUID ¶
UUID is a nullable UUID that implements CSV, JSON, and driver interfaces.