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 ¶
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 ¶
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.
Click to show internal directories.
Click to hide internal directories.