writer

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: May 28, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package writer provides small streaming helpers for exporting Spanner rows using spanvalue formatters.

CSVWriter and JSONLWriter preserve explicit duplicate column names. Their UnnamedFieldNamer only fills empty column names, and generated names avoid collisions with existing names. CSVWriter buffers through encoding/csv, so callers must call Flush after the final write.

CSVWriter, JSONLWriter, and SQLInsertWriter implement Flusher. If an adapter exposes a Close method, that Close method should call Flush; Flush does not close the underlying io.Writer.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrEmptyTableName reports that SQLInsertWriter.Table is empty.
	ErrEmptyTableName = errors.New("empty table name")
	// ErrEmptyColumnName reports that a SQL writer received an empty column name.
	ErrEmptyColumnName = errors.New("empty column name")
	// ErrNilOutputWriter reports that a writer was constructed without an output.
	ErrNilOutputWriter = errors.New("nil output writer")
	// ErrNilRow reports that WriteRow was called with a nil row.
	ErrNilRow = spanvalue.ErrNilRow
	// ErrMissingColumnNames reports that writing values requires initialized column names.
	ErrMissingColumnNames = errors.New("missing column names")
	// ErrColumnNamesMismatch reports that provided column names differ from initialized schema.
	ErrColumnNamesMismatch = errors.New("column names mismatch")
	// ErrHeaderAfterData reports that CSVWriter.WriteHeader was called after data rows were emitted.
	ErrHeaderAfterData = errors.New("header after data")
	// ErrInvalidDelimiter reports that CSVWriter.Comma is not a valid delimiter.
	ErrInvalidDelimiter = errors.New("invalid delimiter")
	// ErrDelimiterAfterWrite reports that CSVWriter.Comma changed after the underlying CSV writer was initialized.
	ErrDelimiterAfterWrite = errors.New("delimiter changed after writer initialization")
)

Functions

This section is empty.

Types

type CSVWriter

type CSVWriter struct {
	Formatter *spanvalue.FormatConfig
	Header    bool
	// Comma is the field delimiter. The zero value selects ','. Set it before
	// the first write; use '\t' for TSV output. Any non-zero delimiter must be
	// a valid encoding/csv delimiter: a valid rune other than '"', '\r', '\n',
	// or utf8.RuneError.
	Comma rune
	// Set before the first write. Once names have been resolved for the current
	// schema, later changes do not retroactively rewrite cached header names.
	UnnamedFieldNamer spanvalue.UnnamedFieldNamer
	// contains filtered or unexported fields
}

CSVWriter writes rows as CSV-style delimited text. Call Flush after the final write.

func NewCSVWriter

func NewCSVWriter(out io.Writer, metadata ...*sppb.ResultSetMetadata) *CSVWriter

NewCSVWriter returns a CSV writer optionally initialized from result-set metadata.

func NewDelimitedWriter added in v0.3.1

func NewDelimitedWriter(out io.Writer, delimiter rune, metadata ...*sppb.ResultSetMetadata) *CSVWriter

NewDelimitedWriter returns a CSV-style writer using delimiter as the field delimiter. Pass '\t' for TSV output.

func (*CSVWriter) Flush

func (w *CSVWriter) Flush() error

Flush flushes buffered CSV or TSV data to the underlying writer. It does not close the underlying writer.

func (*CSVWriter) WriteGCVs

func (w *CSVWriter) WriteGCVs(values []spanner.GenericColumnValue) error

func (*CSVWriter) WriteHeader

func (w *CSVWriter) WriteHeader() error

WriteHeader writes the CSV header once using the initialized column names.

func (*CSVWriter) WriteRow

func (w *CSVWriter) WriteRow(row *spanner.Row) error

WriteRow writes one CSV row, initializing the schema from row metadata if needed.

func (*CSVWriter) WriteValues

func (w *CSVWriter) WriteValues(columnNames []string, values []spanner.GenericColumnValue) error

type Flusher added in v0.3.1

type Flusher interface {
	Flush() error
}

Flusher finalizes any buffered output. Flush does not close the underlying io.Writer. CSVWriter uses Flush to forward buffered CSV or TSV data; JSONLWriter and SQLInsertWriter implement it as a no-op so adapters can use one finalize path for all writer implementations.

type JSONLWriter

type JSONLWriter struct {
	Formatter *spanvalue.FormatConfig
	// Set before the first write. Once names have been resolved for the current
	// schema, later changes do not retroactively rewrite cached object keys.
	UnnamedFieldNamer spanvalue.UnnamedFieldNamer
	// contains filtered or unexported fields
}

JSONLWriter writes one JSON object per line.

func NewJSONLWriter

func NewJSONLWriter(out io.Writer, metadata ...*sppb.ResultSetMetadata) *JSONLWriter

NewJSONLWriter returns a JSONL writer optionally initialized from result-set metadata.

func (*JSONLWriter) Flush added in v0.3.1

func (w *JSONLWriter) Flush() error

Flush finalizes JSONL output. JSONLWriter is unbuffered, so this is a no-op.

func (*JSONLWriter) WriteGCVs

func (w *JSONLWriter) WriteGCVs(values []spanner.GenericColumnValue) error

func (*JSONLWriter) WriteRow

func (w *JSONLWriter) WriteRow(row *spanner.Row) error

func (*JSONLWriter) WriteValues

func (w *JSONLWriter) WriteValues(columnNames []string, values []spanner.GenericColumnValue) error

type SQLInsertWriter

type SQLInsertWriter struct {
	Table     string
	Formatter *spanvalue.FormatConfig
	// contains filtered or unexported fields
}

SQLInsertWriter writes rows as GoogleSQL INSERT statements.

func NewSQLInsertWriter

func NewSQLInsertWriter(out io.Writer, table string, metadata ...*sppb.ResultSetMetadata) *SQLInsertWriter

NewSQLInsertWriter returns a SQL INSERT writer optionally initialized from result-set metadata.

func (*SQLInsertWriter) Flush added in v0.3.1

func (w *SQLInsertWriter) Flush() error

Flush finalizes SQL INSERT output. SQLInsertWriter is unbuffered, so this is a no-op.

func (*SQLInsertWriter) WriteGCVs

func (w *SQLInsertWriter) WriteGCVs(values []spanner.GenericColumnValue) error

func (*SQLInsertWriter) WriteRow

func (w *SQLInsertWriter) WriteRow(row *spanner.Row) error

func (*SQLInsertWriter) WriteValues

func (w *SQLInsertWriter) WriteValues(columnNames []string, values []spanner.GenericColumnValue) error

type Writer

type Writer interface {
	WriteRow(row *spanner.Row) error
}

Writer writes Spanner rows to an output stream.

Writer intentionally models row streaming only. Some concrete writers also implement Flusher; callers that own the full write lifecycle must call Flush after the final row when it is available. Factories that may return a buffered writer should return a concrete type or a local composite interface such as interface { Writer; Flusher }, not Writer alone.

Jump to

Keyboard shortcuts

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