csv

package module
v0.0.0-...-5f328d6 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2018 License: MIT Imports: 6 Imported by: 1

README

CSV Marshaller UnMarshaller for Go

This is a work in progress. The API should not change, but until this is used in active production I make no promises.

Docs

Documentation

Overview

Package csv provides `Marshal` and `UnMarshal` encoding functions for CSV(Comma Seperated Value) data. This package is built on the the standard library's encoding/csv.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Marshal

func Marshal(i interface{}) ([]byte, error)

Marshal returns the CSV encoding of i, which must be a slice of struct types.

Marshal traverses the slice and encodes the primative values.

The first row of the CSV output is a header row. The column names are based on the field name. If a different name is required a struct tag can be used to define a new name.

Field string `csv:"Column Name"`

To skip encoding a field use the "-" as the tag value.

Field string `csv:"-"`

Boolean fields can use string values to define true or false.

Bool bool `true:"Yes" false:"No"`
Example
type Person struct {
	Name    string `csv:"FullName"`
	Gender  string
	Age     int
	Wallet  float32 `csv:"Bank Account"`
	Happy   bool    `true:"Yes!" false:"Sad"`
	private int     `csv:"-"`
}

people := []Person{
	Person{
		Name:   "Smith, Joe",
		Gender: "M",
		Age:    23,
		Wallet: 19.07,
		Happy:  false,
	},
}

out, _ := Marshal(people)
fmt.Printf("%s", out)
Output:

FullName,Gender,Age,Bank Account,Happy
"Smith, Joe",M,23,19.07,Sad

func Unmarshal

func Unmarshal(doc []byte, v interface{}) error

Unmarshal parses the CSV document and stores the result in the value pointed to by v. Only a slice of a struct is allowed for v.

The first line of the CSV is document is used for column names. These are paired to matching exported fields in v's type. See Marshal on how to use tags to map to different names and additional options.

Supported Types

string, int, float and bool are supported. Any type which implements Unmarshal is also supported.

Example
package main

import (
	"fmt"
)

type Person struct {
	Name    string  `csv:"Full Name"`
	income  string  // unexported fields are not Unmarshalled
	Age     int     `csv:"-"`      // skip this field
	Address Address `csv:"Street"` // skip this field
}

type Address struct {
	City   string
	Street string
}

func (a *Address) UnmarshalCSV(val string, row *Row) error {
	c, _ := row.Named("City")
	s, _ := row.Named("Street")

	a.Street = s
	a.City = c

	return nil
}

func main() {
	people := []Person{}

	sample := []byte(
		`Full Name,income,Age,City,Street
John Doe,"32,000",45,Brooklyn,"7th Street"
`)

	err := Unmarshal(sample, &people)

	if err != nil {
		fmt.Println("Error: ", err)
	}

	fmt.Printf("%+v", people)

}
Output:

[{Name:John Doe income: Age:0 Address:{City:Brooklyn Street:7th Street}}]

Types

type Marshaler

type Marshaler interface {
	MarshalCSV() ([]byte, error)
}

Marshaler is an interface for objects which can Marshal themselves into CSV.

type Row

type Row struct {
	Columns *[]string // The name of the columns, in order
	Data    []string  // the data for the row
}

Row is one row of CSV data, indexed by column name or position.

func (*Row) At

func (r *Row) At(i int) string

At returns the rows data for the column positon i

func (*Row) Named

func (r *Row) Named(n string) (string, error)

Named returns the row's data for the first columne named 'n'

type Unmarshaler

type Unmarshaler interface {

	// UnmarshalCSV receives a string with the column value matching this field
	// and a reference to the the current row.
	// This allows composing a value from multiple columns.
	UnmarshalCSV(string, *Row) error
}

Unmarshaler is the interface implemented by objects which can unmarshall the CSV row itself.

Jump to

Keyboard shortcuts

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