csvpb

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2023 License: Apache-2.0 Imports: 7 Imported by: 1

README

CSVPB

PkgGoDev Build Status Go Report Card Discord

CSVPB is a library for writing structpb-typed data to CSV.

Installation

go get github.com/alpstable/csvpb@latest

Usage

The type structpb types supported by this package are

See the gidari library to learn how to write CSV data from a web API.

Contributing

Follow this guide for information on contributing.

Documentation

Overview

csvpb is a package for writing CSV files from a structpb.ListValue.

Index

Examples

Constants

View Source
const Version = "0.2.0"

Version is the version of the csvpb package.

Variables

View Source
var ErrUnkownDecodeType = fmt.Errorf("unknown decode type")

ErrUnkownDecodeType is returned when an unknown decode type is provided.

View Source
var ErrUnsupportedValueType = fmt.Errorf("unsupported value type")

ErrUnsupportedValueType is returned when a value type is not supported.

Functions

func Decode

func Decode(dtype DecodeType, data []byte) (*structpb.ListValue, error)

Decode will a UpsertRequest into a structpb.ListValue for ease-of-use. This method will return an error if the provided "decodeType" is not supported.

Types

type DecodeType

type DecodeType int32

DecodeType is an enum that represents the type of data that is being decoded.

const (
	// DecodeTypeUnknown is the default value for the DecodeType enum.
	DecodeTypeUnknown DecodeType = iota

	// DecodeTypeJSON is used to decode JSON data.
	DecodeTypeJSON
)

type ListWriter

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

ListWriter is used to write a structpb.ListValue to CSV, using a CSV writer.

func NewListWriter

func NewListWriter(writer Writer, opts ...ListWriterOption) *ListWriter

NewListWriter creates a new ListWriter for writing a structpb.ListValue to CSV.

func (*ListWriter) Write

func (w *ListWriter) Write(ctx context.Context, list *structpb.ListValue) error

Write writes the ListValue to CSV.

Example
package main

import (
	"context"
	"encoding/csv"
	"log"
	"os"

	"github.com/alpstable/csvpb"
)

func main() {
	// Create a writer object using the "encoding/csv" package.
	writer := csv.NewWriter(os.Stdout)

	// Create a new list writer.
	listWriter := csvpb.NewListWriter(writer, csvpb.WithAlphabetizeHeaders())

	// Create a structpb.List to write as a CSV to stdout.
	exJSON := []byte(`{"id": 1, "name": "test", "age": null}`)

	exList, err := csvpb.Decode(csvpb.DecodeTypeJSON, exJSON)
	if err != nil {
		log.Fatalf("failed to decode JSON: %v", err)
	}

	// Write a list to the list writer.
	if err := listWriter.Write(context.TODO(), exList); err != nil {
		log.Fatalf("failed to write list: %v", err)
	}

	// Flush the writer.
	writer.Flush()

}
Output:

age,id,name
,1.000000,test

type ListWriterOption

type ListWriterOption func(*ListWriter)

ListWriterOption is used to configure the ListWriter.

func WithAlphabetizeHeaders

func WithAlphabetizeHeaders() ListWriterOption

WithAlphabetizeHeaders configures the ListWriter to alphabetize the headers when writing the CSV.

type Writer

type Writer interface {
	Write(record []string) error
}

Writer is a CSV writer.

Jump to

Keyboard shortcuts

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