exporter

package module
v0.0.0-...-8a86631 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2025 License: MIT Imports: 4 Imported by: 0

README

Go Data Exporter

Go Data Exporter is a lightweight and extensible Go library for exporting tabular data from various sources (such as SQL databases, in-memory slices, etc.) into multiple formats, including:

  • CSV
  • JSON (standard or newline-delimited)
  • XML
  • HTML

Features

  • Pluggable scanner interface to support different data sources (database/sql, Hive, slices, etc.)
  • Custom export codecs with configurable behavior
  • Row preprocessing support
  • NULL value customization
  • Support for column metadata
  • Easy extension with your own codecs or scanners

Installation

go get github.com/go-data-exporter/exporter

Examples

Simple export in-memory data
package main

import (
    "os"
    "time"

    "github.com/go-data-exporter/exporter"
    "github.com/go-data-exporter/exporter/codec"
    "github.com/go-data-exporter/exporter/scanner"
)

func main() {
    // Define in-memory tabular data as a slice of rows.
    // Each inner slice represents a row with mixed types.
    data := [][]any{
        {1, 2, time.Now(), 5, "text", 3.14},
        {4, 5, time.Now(), 5, "text", 3.14},
        {7, 8, time.Now(), 5, "text", 3.14},
    }

    // Create a scanner from the in-memory data.
    // It provides the data through the generic Rows interface.
    s := scanner.FromData(data)

    // Create a CSV codec for exporting the data.
    // Other formats like JSON and HTML are also available.
    c := codec.CSV()

    // Export the scanned data to standard output.
    if err := exporter.New(s, c).Write(os.Stdout); err != nil {
        log.Fatalln(err)
    }
}
Simple export sql.Rows.
package main

import (
    "database/sql"
    "log"
    "os"
    "time"

    "github.com/go-data-exporter/exporter"
    "github.com/go-data-exporter/exporter/codec"
    jsoncodec "github.com/go-data-exporter/exporter/codec/json"
    "github.com/go-data-exporter/exporter/scanner"
)

func main() {
    // Open a connection to the database using the specified driver and DSN.
    db, err := sql.Open("driver", "dsn")
    if err != nil {
        log.Fatalln(err)
    }
    defer db.Close()

    // Execute a SQL query and obtain the result set.
    rows, err := db.Query("SELECT * FROM table")
    if err != nil {
        log.Fatalln(err)
    }
    defer rows.Close()

    // Wrap the sql.Rows in a scanner that implements the exporter.Rows interface.
    s := scanner.FromSQL(rows, "driver")

    // Create a JSON codec.
    c := codec.JSON()

    // Export the data to standard output using the configured scanner and codec.
    if err := exporter.New(s, c).Write(os.Stdout); err != nil {
        log.Fatalln(err)
    }
}
Customization
package main

import (
    "database/sql"
    "log"
    "os"
    "time"

    "github.com/go-data-exporter/exporter"
    "github.com/go-data-exporter/exporter/codec"
    jsoncodec "github.com/go-data-exporter/exporter/codec/json"
    "github.com/go-data-exporter/exporter/scanner"
)

func main() {
    // Open a connection to the database using the specified driver and DSN.
    db, err := sql.Open("driver", "dsn")
    if err != nil {
        log.Fatalln(err)
    }
    defer db.Close()

    // Execute a SQL query and obtain the result set.
    rows, err := db.Query("SELECT * FROM table")
    if err != nil {
        log.Fatalln(err)
    }
    defer rows.Close()

    // Wrap the sql.Rows in a scanner that implements the exporter.Rows interface.
    s := scanner.FromSQL(rows, "driver")

    // Create a JSON codec with custom export behavior.
    c := codec.JSON(
        // Limit the output to the first 100 rows.
        jsoncodec.WithLimit(100),

        // Use newline-delimited JSON format (one JSON object per line).
        jsoncodec.WithNewlineDelimited(true),

        // Customize how time.Time values are serialized.
        jsoncodec.WithCustomType(func(v time.Time, metadata scanner.Metadata) any {
            // If the timestamp has no time component, output only the date part.
            if v.Equal(v.Truncate(24 * time.Hour)) {
                return v.Format(time.DateOnly)
            }
            return v
        }),

        // Filter out specific rows before exporting.
        // For example, exclude users with the username "admin".
        jsoncodec.WithPreProcessorFunc(func(rowID int, row map[string]any) (map[string]any, bool) {
            if row["username"] == "admin" {
                return nil, false
            }
            return row, true
        }),
    )

    // Export the data to standard output using the configured scanner and codec.
    if err := exporter.New(s, c).Write(os.Stdout); err != nil {
        log.Fatalln(err)
    }
}

Supported Formats

Out of the box, the library provides codecs for exporting data to:

  • CSV — standard comma-separated values with customizable options.
  • JSON — standard or newline-delimited (JSON Lines).
  • XML — XML.
  • HTML — styled HTML tables with optional headers and cell formatting.

✅ Currently, only CSV, JSON, XML and HTML are officially supported.

Custom Codecs

You can implement your own codec by satisfying the following interface:

type Codec interface {
    Write(rows scanner.Rows, writer io.Writer) error
}

This allows you to export data to any other format — such as XML, Excel, Markdown, YAML, or even custom binary formats — by plugging in your own encoder logic.

For example, to add support for a new format:

type MyCodec struct{}

func (c *MyCodec) Write(rows scanner.Rows, w io.Writer) error {
    // implement your export logic here
    return nil
}

Then use it with:

exporter.New(scanner, &MyCodec{}).Write(os.Stdout)

License

MIT

Author

https://github.com/armantarkhanian

Documentation

Overview

Package exporter provides a unified interface for exporting tabular data using pluggable codecs (e.g., CSV, JSON, HTML). It wraps a data source and a codec implementation to perform the export.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Exporter

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

Exporter is the main struct that coordinates exporting data. It uses a scanner.Rows as the data source and a codec.Codec to determine the output format.

func New

func New(rows scanner.Rows, codec codec.Codec) *Exporter

New creates a new Exporter instance using the given data source and codec.

func (*Exporter) Write

func (cs *Exporter) Write(writer io.Writer) error

Write writes the exported data to the given io.Writer using the codec.

func (*Exporter) WriteFile

func (cs *Exporter) WriteFile(filename string) error

WriteFile writes the exported data directly to a file specified by filename.

Directories

Path Synopsis
Package codec defines the Codec interface and provides factory functions to create different output format encoders such as CSV, JSON, and HTML.
Package codec defines the Codec interface and provides factory functions to create different output format encoders such as CSV, JSON, and HTML.
csv
Package csvcodec provides an implementation of the Codec interface for writing data in CSV (Comma-Separated Values) format.
Package csvcodec provides an implementation of the Codec interface for writing data in CSV (Comma-Separated Values) format.
html
Package htmlcodec provides an HTML implementation of the Codec interface, generating well-formatted HTML tables with optional headers, NULL styling, row preprocessing, and type-specific string conversion.
Package htmlcodec provides an HTML implementation of the Codec interface, generating well-formatted HTML tables with optional headers, NULL styling, row preprocessing, and type-specific string conversion.
json
Package jsoncodec provides a JSON implementation of the Codec interface, allowing tabular data to be exported in either standard JSON array format or newline-delimited JSON (JSON Lines).
Package jsoncodec provides a JSON implementation of the Codec interface, allowing tabular data to be exported in either standard JSON array format or newline-delimited JSON (JSON Lines).
xml
Package xmlcodec provides an XML implementation of the Codec interface, generating well-formatted XML tables with optional headers, NULL styling, row preprocessing, and type-specific string conversion.
Package xmlcodec provides an XML implementation of the Codec interface, generating well-formatted XML tables with optional headers, NULL styling, row preprocessing, and type-specific string conversion.
Package scanner defines interfaces and types for column and row metadata.
Package scanner defines interfaces and types for column and row metadata.
Package tostring provides functionality to convert arbitrary Go values into their string representation, while also detecting NULL or zero-equivalent values.
Package tostring provides functionality to convert arbitrary Go values into their string representation, while also detecting NULL or zero-equivalent values.

Jump to

Keyboard shortcuts

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