Help us shape the future of Go! Take the 2026 Go Developer Survey

importer

package
v0.5.3 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2026 License: MIT Imports: 22 Imported by: 0

Documentation

Overview

Package importer streams CSV and XLSX uploads into typed rows. A heading row maps declared columns onto a row struct through scalar codecs; each row is then checked by ordinary validation rules. Valid rows reach the application handler in bounded chunks while invalid rows are reported with their row number and issues. Input, row, cell, decompressed XML and shared-string sizes are all bounded; no complete file or result set is held in memory.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Column

type Column[R any] struct {
	// contains filtered or unexported fields
}

Column maps one file heading onto a field of row type R. Construct it with Field or NullableField. Headings match after trimming spaces, ignoring case.

func Field

func Field[R, V any](heading string, codec foundryhttp.QueryCodec[V], assign func(*R, V)) Column[R]

Field declares a required heading and a non-empty cell parsed by codec. An empty cell is a required issue. assign stores the parsed value in the row.

func NullableField

func NullableField[R, V any](heading string, codec foundryhttp.QueryCodec[V], assign func(*R, value.Nullable[V])) Column[R]

NullableField declares an optional heading. An empty cell, or a file without the heading, assigns value.Null; other cells are parsed by codec.

type Definition

type Definition[R any] struct {
	// contains filtered or unexported fields
}

Definition is an immutable, validated import declaration.

func Define

func Define[R any](spec Spec[R]) Definition[R]

Define copies column declarations and initializes an import definition, retaining validation errors for Validate.

func (Definition[R]) Run

func (d Definition[R]) Run(ctx context.Context, source Source, handle Handler[R]) (Report, error)

Run reads the source, maps and validates rows, and calls handle with chunks of valid rows. Invalid rows are reported and skipped; blank rows are ignored. Chunks already handled are not undone when a later chunk, a limit or the input fails: run the handler inside a caller-owned transaction and inspect Report.Failed to make an import all-or-nothing. Codec, rule and handler callbacks run synchronously in file order; panic/Goexit are faults.

func (Definition[R]) Validate

func (d Definition[R]) Validate() error

Validate returns the definition's initialization error, or an invalid fault for an undefined import.

type Failure

type Failure struct {
	Row    int              `json:"row"`
	Issues []contract.Issue `json:"issues"`
}

Failure reports the issues of one rejected row. Paths name the column heading (for parsing issues) or the row's validation field.

type Format

type Format string

Format selects the container of an uploaded table.

const (
	CSV  Format = "csv"
	XLSX Format = "xlsx"
)

type Handler

type Handler[R any] func(context.Context, []Row[R]) error

Handler receives each chunk of valid rows in file order. The slice is owned by the handler. Returning an error stops the import.

type HeadingError

type HeadingError struct {
	// contains filtered or unexported fields
}

HeadingError reports a heading row that cannot be mapped: missing required headings or repeated declared headings. Nothing was passed to the handler.

func (*HeadingError) Error

func (*HeadingError) Error() string

Error returns the heading mismatch message.

func (*HeadingError) Is

func (*HeadingError) Is(target error) bool

Is reports whether the target is fault.Invalid.

func (*HeadingError) Issues

func (e *HeadingError) Issues() []contract.Issue

Issues returns a copy of the heading issues.

type Limits

type Limits struct {
	// MaxRows bounds data rows after the heading; exceeding it fails the import.
	MaxRows int
	// MaxColumns bounds the columns of any row.
	MaxColumns int
	// MaxCellBytes bounds one cell; a longer cell is a row issue.
	MaxCellBytes int
	// MaxRowBytes bounds one CSV record, including quoted line breaks.
	// Longer records fail the import: they cannot be parsed incrementally.
	MaxRowBytes int
	// MaxInputBytes bounds CSV bytes read or the XLSX archive size.
	MaxInputBytes int64
	// MaxXMLBytes bounds all decompressed XLSX parts that are read. No single
	// XML token may exceed eight times MaxCellBytes plus 4 KiB, whatever this
	// total allows. Raise it together with MaxRows for very wide sheets.
	MaxXMLBytes int64
	// MaxSharedStrings and MaxSharedStringBytes bound the XLSX string table,
	// the only part that must be held in memory for random access.
	MaxSharedStrings     int
	MaxSharedStringBytes int64
	// MaxFailures bounds retained row failures; later failures are counted.
	MaxFailures int
	// ChunkSize is the number of valid rows passed to each handler call.
	ChunkSize  int
	Validation validation.Limits
}

Limits bound one import. DefaultLimits is the source of the defaults.

func DefaultLimits

func DefaultLimits() Limits

DefaultLimits returns default import input, row, validation, and reporting budgets.

func (Limits) Validate

func (l Limits) Validate() error

Validate checks import budgets and nested validation limits.

type Report

type Report struct {
	Rows     int       `json:"rows"`
	Imported int       `json:"imported"`
	Failed   int       `json:"failed"`
	Failures []Failure `json:"failures,omitempty"`
}

Report summarizes an import. Rows counts non-blank data rows read; Imported counts rows accepted by the handler; Failed counts rejected rows, of which at most Limits.MaxFailures are retained in Failures.

type Row

type Row[R any] struct {
	Number int
	Value  R
}

Row is one valid parsed row and its 1-based row number: the worksheet row for XLSX, or the line on which the record starts for CSV.

type Source

type Source struct {
	// contains filtered or unexported fields
}

Source is one uploaded table. The caller owns the underlying reader and closes it after Run returns. Construct it with CSVSource or XLSXSource.

func CSVSource

func CSVSource(reader io.Reader) Source

CSVSource reads comma-separated UTF-8 text (RFC 4180 quoting). A leading byte-order mark is ignored.

func XLSXSource

func XLSXSource(reader io.ReaderAt, size int64) Source

XLSXSource reads the first worksheet of an Office Open XML workbook. The archive is random access, so the caller supplies its exact size.

func (Source) WithComma

func (s Source) WithComma(comma rune) Source

WithComma selects another CSV field delimiter, such as ';' or '\t'.

func (Source) WithSheet

func (s Source) WithSheet(name string) Source

WithSheet selects a worksheet by its exact name instead of the first one.

type Spec

type Spec[R any] struct {
	Columns    []Column[R]
	Rules      []validation.Rule[R]
	HeadingRow int
	TimeZone   temporal.ZoneName
	Limits     Limits
}

Spec declares one import. Rules run on each completely parsed row. An empty TimeZone means UTC; it converts spreadsheet date-time cells, which have no zone, into instants. HeadingRow is 1-based; zero means the first row. Zero Limits means DefaultLimits.

Jump to

Keyboard shortcuts

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