csv

package
v0.0.0-...-7f81a18 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2022 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package csv provides CsvReader and CsvWriter to process csv format file in the struct declaration style.

Package csv provides CsvReader and CsvWriter to process csv format file in the struct declaration style.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ColumnInfo

type ColumnInfo struct {
	// Name in the csv header.
	HeaderName string
	// Field to lookup values in struct.
	LookupField string
	// Number of cells for this column. Applied when IsSlice = true
	NumSpan int
	// Max length of content in this column
	Limit      int
	IsSlice    bool
	IsJsonNull bool
}

func GenerateColumnInfos

func GenerateColumnInfos(typ reflect.Type) []*ColumnInfo

type CsvReader

type CsvReader struct {
	*csv.Reader
	// contains filtered or unexported fields
}
Example
type TestStruct struct {
	Int        int
	String     string
	TagInt     int `csv:"tag_i"`
	anonyInt   int
	HiddenInt  int    `csv:"-"`
	NextString string `csv:"nextstring"`
}
input := `Int,String,tag_i,nextstring
1,2,3,6`
t := &TestStruct{}

buf := bytes.NewBufferString(input)
r := NewCsvReader(buf)
r.ReadStruct(t)
fmt.Printf("%+v", t)
Output:

&{Int:1 String:2 TagInt:3 anonyInt:0 HiddenInt:0 NextString:6}

func NewCsvReader

func NewCsvReader(r io.Reader, opts ...ReaderOption) *CsvReader

func (*CsvReader) ReadAllStructs

func (r *CsvReader) ReadAllStructs(i interface{}) error

func (*CsvReader) ReadStruct

func (r *CsvReader) ReadStruct(i interface{}) error

type CsvWriter

type CsvWriter struct {
	*csv.Writer
	// contains filtered or unexported fields
}

CsvWriter extends the encoding/csv writer, supporting writting struct, and shortcut to write to a file.

Example
type TestStruct struct {
	Int        int
	String     string
	TagInt     int `csv:"tag_i"`
	anonyInt   int
	HiddenInt  int    `csv:"-"`
	NextString string `csv:"nextstring"`
}
t := TestStruct{
	1, "2", 3, 4, 5, "6",
}

buf := &bytes.Buffer{}
w := NewCsvWriter(buf, WithAppendBOM(false))
w.WriteStruct(t)
w.Flush()
fmt.Println(buf.String())
Output:

Int,String,tag_i,nextstring
1,2,3,6

func NewCsvWriter

func NewCsvWriter(w io.Writer, opts ...WriterOption) *CsvWriter

func (*CsvWriter) Close

func (w *CsvWriter) Close() error

func (*CsvWriter) WriteStruct

func (w *CsvWriter) WriteStruct(i interface{}) error

type FileCsvReader

type FileCsvReader struct {
	*CsvReader
	File *os.File
}

func NewFileCsvReader

func NewFileCsvReader(filename string) *FileCsvReader

func (*FileCsvReader) Close

func (r *FileCsvReader) Close() error

type FileCsvWriter

type FileCsvWriter struct {
	*CsvWriter
	File *os.File
}

func NewFileCsvWriter

func NewFileCsvWriter(filename string) *FileCsvWriter

NewFileCsvWriter is combination of creating file and passing it to a writer.

func (*FileCsvWriter) Close

func (fcw *FileCsvWriter) Close() error

type ReaderOption

type ReaderOption func(*CsvReader)

func WithReaderColumnInfos

func WithReaderColumnInfos(infos []*ColumnInfo) ReaderOption

func WithReaderSkipJSONNull

func WithReaderSkipJSONNull(skip bool) ReaderOption

func WithReaderSliceDelimiter

func WithReaderSliceDelimiter(d string) ReaderOption

type WriterOption

type WriterOption func(*CsvWriter)

func WithAppendBOM

func WithAppendBOM(enabled bool) WriterOption

func WithColumnInfos

func WithColumnInfos(infos []*ColumnInfo) WriterOption

func WithSkipJSONNull

func WithSkipJSONNull(skip bool) WriterOption

func WithSliceDelimiter

func WithSliceDelimiter(d string) WriterOption

Jump to

Keyboard shortcuts

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