Documentation ¶
Overview ¶
Package struct2csv creates slices of strings out of struct fields. Struct2csv can work with either a struct or a slice of structs, The data can be returned as []string, in the case of a single struct, or [][]string, in the case of a slice of structs, by using struct2csv's encoder and `Marshal`.
For writing struct data directly to a CSV file, a writer can be used. `NewWriter` accepts an `io.Writer` and uses it to create a `csv.Writer`, which is used for writing the generated data. The `csv.Writer`'s exported fields are exposed as methods.
Fields of Kind Uintptr, Unsafepointer, Chan, Func, and Interface are not supported.
Unexported fields will be ignored.
Field tags are supported. Struct2csv will look for field tags matching `csv`, unless it's configured to either ignore field tags or use different field tag.
Index ¶
- Variables
- type Encoder
- func (e *Encoder) ColNames() []string
- func (e *Encoder) GetColNames(v interface{}) ([]string, error)
- func (e *Encoder) GetRow(v interface{}) ([]string, error)
- func (e *Encoder) Marshal(v interface{}) ([][]string, error)
- func (e *Encoder) SetBase(i int)
- func (e *Encoder) SetHandlerTag(handlerTag string)
- func (e *Encoder) SetSeparators(beg, end string)
- func (e *Encoder) SetTag(s string)
- func (e *Encoder) SetUseTags(b bool)
- type StructRequiredError
- type StructSliceError
- type Writer
- func (w *Writer) ColNames() []string
- func (w *Writer) Comma() rune
- func (w *Writer) Error() error
- func (w *Writer) Flush()
- func (w *Writer) Rows() int
- func (w *Writer) SetBase(i int)
- func (w *Writer) SetComma(r rune)
- func (w *Writer) SetSeparators(beg, end string)
- func (w *Writer) SetTag(s string)
- func (w *Writer) SetUseCRLF(b bool)
- func (w *Writer) SetUseTags(b bool)
- func (w *Writer) UseCRLF() bool
- func (w *Writer) Write(row []string) error
- func (w *Writer) WriteAll(data [][]string) error
- func (w *Writer) WriteColNames(st interface{}) error
- func (w *Writer) WriteStruct(st interface{}) error
- func (w *Writer) WriteStructs(st interface{}) error
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNilSlice occurs when the slice of structs to encode is nil. ErrNilSlice = errors.New("struct2csv: the slice of structs was nil") // ErrEmptySlice occurs when the slice of structs to encode is empty. ErrEmptySlice = errors.New("struct2csv: the slice of structs was empty") )
Functions ¶
This section is empty.
Types ¶
type Encoder ¶
type Encoder struct {
// contains filtered or unexported fields
}
Encoder handles encoding of a CSV from a struct.
func (*Encoder) ColNames ¶
ColNames returns the encoder's saved column names as a copy. The colNames field must be populated before using this.
func (*Encoder) GetColNames ¶
GetColNames get's the column names from the received struct. If the interface is not a struct, an error will occur.
Field tags are supported. By default, the column names will be the value of the `csv` tag, if any. This can be changed with the SetTag(newTag) func; e.g. `json` to use JSON tags. Use of field tags can be toggled with the the SetUseTag(bool) func. If use of field tags is set to FALSE, the field's name will be used.
func (*Encoder) GetRow ¶
GetRow get's the data from the passed struct. This only operates on single structs. If you wish to transmogrify everything at once, use Encoder.Marshal([]T).
func (*Encoder) Marshal ¶
Marshal takes a slice of structs and returns a [][]byte representing CSV data. Each field in the struct results in a column. Fields that are slices are stored in a single column as a comma separated list of values. Fields that are maps are stored in a single column as a comma separted list of key:value pairs.
If the passed data isn't a slice of structs an error will be returned.
func (*Encoder) SetBase ¶
SetBase sets the base for strings.FormatUint. By default, this is 10. Set the base if another base should be used for formatting uint values.
Base 2 is the minimum value; anything less will be set to two.
func (*Encoder) SetHandlerTag ¶
func (*Encoder) SetSeparators ¶
SetSeparators sets the begin and end separator values for lists. Setting the separators to "", empty strings, results in no separators being added. By default, "(" and ")" are used as the begin and end separators,
func (*Encoder) SetTag ¶
SetTag sets the tag that the Encoder should use for header (column) names. By default, this is set to 'csv'. If the received value is an empty string, nothing will be done
func (*Encoder) SetUseTags ¶
SetUseTags sets whether or not tags should be used for header (column) names.
type StructRequiredError ¶
type StructRequiredError struct {
// contains filtered or unexported fields
}
A StructRequiredError is returned when a non-struct type is received.
func (StructRequiredError) Error ¶
func (e StructRequiredError) Error() string
type StructSliceError ¶
type StructSliceError struct {
// contains filtered or unexported fields
}
A StructSliceError is returned when an interface that isn't a slice of type struct is received.
func (StructSliceError) Error ¶
func (e StructSliceError) Error() string
type Writer ¶
type Writer struct {
// contains filtered or unexported fields
}
A Writer writes structs to a CSV encoded file. This wraps both `csv.Writer` and this package's `Encoder`.
func (*Writer) Flush ¶
func (w *Writer) Flush()
Flush writes any buffered data to the underlying io.Writer.
func (*Writer) SetBase ¶
SetBase set's the base for _uint_ values; mainly used for `strings.FormatUint()`. By default, this is set to 10, for base 10 numbering. Any base value < 2 will be set to 2, binary.
func (*Writer) SetComma ¶
SetComma takes the passed rune and uses it to set the field delimiter for CSV fields.
func (*Writer) SetSeparators ¶
SetSeparators sets the begin and end separator values for lists, which default to `(` and `)`. Empty strings result in no separators being used.
func (*Writer) SetUseCRLF ¶
SetUseCRLF set's the csv'writer's UseCRLF field
func (*Writer) SetUseTags ¶
SetUseTags set's whether or not field tag values should be checked. If field tags are not being checked, the field name will be used for the column name.
func (*Writer) WriteAll ¶
WriteAll writes multiple CSV records, a two-d slice of strings, `[][]string` to w using Write and then calls Flush.
func (*Writer) WriteColNames ¶
WriteColNames writes out the column names of the CSV field.
func (*Writer) WriteStruct ¶
WriteStruct takes a struct, marshals it to CSV and writhes the CSV record to the writer
func (*Writer) WriteStructs ¶
WriteStructs takes a slice of structs and writes them as CSV records. This includes writing out the column names as the first row. When done, Flush is called.