csvpputil

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package csvpputil provides utility functions for converting CSV++ data to other formats such as JSON and YAML.

JSON Streaming Output

For large JSON files, use JSONArrayWriter to stream records directly to the output:

w := csvpputil.NewJSONArrayWriter(out, headers)
for {
    record, err := reader.Read()
    if err == io.EOF {
        break
    }
    if err != nil {
        return err
    }
    if err := w.Write(record); err != nil {
        return err
    }
}
if err := w.Close(); err != nil {
    return err
}

YAML Streaming Output

For YAML output, use YAMLArrayWriter:

w := csvpputil.NewYAMLArrayWriter(out, headers)
for _, record := range records {
    if err := w.Write(record); err != nil {
        return err
    }
}
if err := w.Close(); err != nil {
    return err
}

Convenience Functions

For small to medium datasets, use the Marshal or Write functions:

data, err := csvpputil.MarshalJSON(headers, records)
data, err := csvpputil.MarshalYAML(headers, records)

err := csvpputil.WriteJSON(w, headers, records)
err := csvpputil.WriteYAML(w, headers, records)

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func MarshalJSON

func MarshalJSON(headers []*csvpp.ColumnHeader, records [][]*csvpp.Field, opts ...JSONArrayWriterOption) ([]byte, error)

MarshalJSON converts CSV++ records to JSON bytes. The output is a JSON array of objects, where each object represents a record.

Example
headers := []*csvpp.ColumnHeader{
	{Name: "name", Kind: csvpp.SimpleField},
	{Name: "age", Kind: csvpp.SimpleField},
}

records := [][]*csvpp.Field{
	{{Value: "Alice"}, {Value: "30"}},
	{{Value: "Bob"}, {Value: "25"}},
}

data, err := csvpputil.MarshalJSON(headers, records, csvpputil.WithDeterministic(true))
if err != nil {
	log.Fatal(err)
}
fmt.Println(string(data))
Output:

[{"name":"Alice","age":"30"},{"name":"Bob","age":"25"}]

func MarshalYAML

func MarshalYAML(headers []*csvpp.ColumnHeader, records [][]*csvpp.Field) ([]byte, error)

MarshalYAML converts CSV++ records to YAML bytes. The output is a YAML array where each element is a record.

Example
headers := []*csvpp.ColumnHeader{
	{Name: "name", Kind: csvpp.SimpleField},
	{Name: "age", Kind: csvpp.SimpleField},
}

records := [][]*csvpp.Field{
	{{Value: "Alice"}, {Value: "30"}},
	{{Value: "Bob"}, {Value: "25"}},
}

data, err := csvpputil.MarshalYAML(headers, records)
if err != nil {
	log.Fatal(err)
}
fmt.Print(string(data))
Output:

- name: Alice
  age: "30"
- name: Bob
  age: "25"

func WriteJSON added in v0.0.4

func WriteJSON(w io.Writer, headers []*csvpp.ColumnHeader, records [][]*csvpp.Field, opts ...JSONArrayWriterOption) error

WriteJSON writes CSV++ records as a JSON array to the provided writer. The output is a JSON array of objects, where each object represents a record.

Example
headers := []*csvpp.ColumnHeader{
	{Name: "name", Kind: csvpp.SimpleField},
	{Name: "score", Kind: csvpp.SimpleField},
}

records := [][]*csvpp.Field{
	{{Value: "Alice"}, {Value: "100"}},
	{{Value: "Bob"}, {Value: "85"}},
}

if err := csvpputil.WriteJSON(os.Stdout, headers, records, csvpputil.WithDeterministic(true)); err != nil {
	log.Fatal(err)
}
Output:

[{"name":"Alice","score":"100"},{"name":"Bob","score":"85"}]

func WriteYAML added in v0.0.4

func WriteYAML(w io.Writer, headers []*csvpp.ColumnHeader, records [][]*csvpp.Field) error

WriteYAML writes CSV++ records as a YAML array to the provided writer. The output is a YAML array where each element is a record.

Example
headers := []*csvpp.ColumnHeader{
	{Name: "name", Kind: csvpp.SimpleField},
	{Name: "score", Kind: csvpp.SimpleField},
}

records := [][]*csvpp.Field{
	{{Value: "Alice"}, {Value: "100"}},
	{{Value: "Bob"}, {Value: "85"}},
}

if err := csvpputil.WriteYAML(os.Stdout, headers, records); err != nil {
	log.Fatal(err)
}
Output:

- name: Alice
  score: "100"
- name: Bob
  score: "85"

Types

type JSONArrayWriter added in v0.0.4

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

JSONArrayWriter writes CSV++ records as a JSON array using streaming output. It uses jsontext.Encoder internally for efficient token-level writing.

Example
headers := []*csvpp.ColumnHeader{
	{Name: "name", Kind: csvpp.SimpleField},
	{Name: "score", Kind: csvpp.SimpleField},
}

w := csvpputil.NewJSONArrayWriter(os.Stdout, headers, csvpputil.WithDeterministic(true))

if err := w.Write([]*csvpp.Field{{Value: "Alice"}, {Value: "100"}}); err != nil {
	log.Fatal(err)
}
if err := w.Write([]*csvpp.Field{{Value: "Bob"}, {Value: "85"}}); err != nil {
	log.Fatal(err)
}
if err := w.Close(); err != nil {
	log.Fatal(err)
}
Output:

[{"name":"Alice","score":"100"},{"name":"Bob","score":"85"}]

func NewJSONArrayWriter added in v0.0.4

func NewJSONArrayWriter(w io.Writer, headers []*csvpp.ColumnHeader, opts ...JSONArrayWriterOption) *JSONArrayWriter

NewJSONArrayWriter creates a new JSONArrayWriter that writes to w.

func (*JSONArrayWriter) Close added in v0.0.4

func (w *JSONArrayWriter) Close() error

Close finishes the JSON array by writing ']'. jsontext.Encoder auto-flushes, so no explicit Flush needed.

func (*JSONArrayWriter) Write added in v0.0.4

func (w *JSONArrayWriter) Write(record []*csvpp.Field) error

Write writes a single record as a JSON object in the array. The first call writes '[', subsequent calls add ',' before each object.

type JSONArrayWriterOption added in v0.0.4

type JSONArrayWriterOption func(*JSONArrayWriter)

JSONArrayWriterOption is a functional option for JSONArrayWriter.

func WithDeterministic deprecated

func WithDeterministic(_ bool) JSONArrayWriterOption

WithDeterministic is kept for API compatibility but has no effect. Output order is always determined by the headers order.

Deprecated: This option is no longer needed as output order follows headers order.

type YAMLArrayWriter added in v0.0.4

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

YAMLArrayWriter writes CSV++ records as a YAML array. Due to YAML's structure (go-yaml doesn't support streaming array elements), records are buffered until Close.

Example
headers := []*csvpp.ColumnHeader{
	{Name: "name", Kind: csvpp.SimpleField},
	{Name: "score", Kind: csvpp.SimpleField},
}

w := csvpputil.NewYAMLArrayWriter(os.Stdout, headers)

if err := w.Write([]*csvpp.Field{{Value: "Alice"}, {Value: "100"}}); err != nil {
	log.Fatal(err)
}
if err := w.Write([]*csvpp.Field{{Value: "Bob"}, {Value: "85"}}); err != nil {
	log.Fatal(err)
}
if err := w.Close(); err != nil {
	log.Fatal(err)
}
Output:

- name: Alice
  score: "100"
- name: Bob
  score: "85"

func NewYAMLArrayWriter added in v0.0.4

func NewYAMLArrayWriter(w io.Writer, headers []*csvpp.ColumnHeader) *YAMLArrayWriter

NewYAMLArrayWriter creates a new YAMLArrayWriter that writes to w.

func (*YAMLArrayWriter) Close added in v0.0.4

func (w *YAMLArrayWriter) Close() error

Close writes all buffered records as a YAML array and closes the writer. go-yaml requires the complete array for proper YAML array output.

func (*YAMLArrayWriter) Write added in v0.0.4

func (w *YAMLArrayWriter) Write(record []*csvpp.Field) error

Write adds a single record to the buffer. The actual writing happens on Close.

Jump to

Keyboard shortcuts

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