cartogopher

package module
v0.0.0-...-f5fe2b6 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2016 License: MIT Imports: 3 Imported by: 0

README

Cartogopher Build Status Go Report Card

A super simple library to read CSVs as maps instead of arrays in golang

Usage

Cartogopher can be used as an augmentor of Go's built in CSV reader.

The 'Hello World' for cartogopher looks something like this:

package main

import (
    "encoding/csv"
    "github.com/literallyelvis/cartogopher"
    "os"
)

func main(){
    file, err := os.Open("whatever.csv")
    if err != nil{
        // handle error as you please
    }

    reader, err := csv.NewReader(file)
    if err != nil{
        // handle error as you please
    }

    myCSV, err := cartogopher.NewReader(reader)
    if err != nil{
        // handle error as you please
    }

    theRestOfTheFile, err := myCSV.Reader.ReadAll()
    if err != nil{
        // handle error as you please
    }
    // do things with theRestOfTheFile
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MapReader

type MapReader struct {
	Headers        []string
	HeaderIndexMap map[string]int
	Reader         *csv.Reader
}

MapReader contains all our necessary data for the various methods to function

func NewReader

func NewReader(file io.Reader) (*MapReader, error)

NewReader returns a new MapReader struct. It can be created the same way a regular CSV file is created, by providing it with a reference to a file reader, ideally one that points to a CSV file. I'm using an interface here so that, should the need arise, you can provide your CSV to the package in a variety of non-file based ways. Note that here we read the first row of the file without setting any non-standard values for the CSV package's Reader struct. If it becomes apparent that the ability to change these parameters is vital, then I'm more than happy to figure out an idiomatic way to accomplish that task.

func (*MapReader) CreateHeaderIndexMap

func (m *MapReader) CreateHeaderIndexMap()

CreateHeaderIndexMap creates a map of header strings to their indices in the array generated by encoding/csv's reader. For instance, if your CSV file looks something like this:

---------------------
| one | two | three |
---------------------
|  A  |  B  |   C   |
---------------------

Go's generated array for the header row will be [ "one", "two", "three" ]. Cartogopher's generated map for the header row will be { "one": 1, "two": 2, "three": 3 }

func (MapReader) CreateRowMap

func (m MapReader) CreateRowMap(csvRow []string) map[string]string

CreateRowMap takes a given CSV array and returns a map of column names to the values contained therein. For instance, if your CSV file looks something like this:

---------------------
| one | two | three |
---------------------
|  A  |  B  |   C   |
---------------------

The return result will be:

{
	"one": "A",
	"two": "B",
	"three": "C",
}

Note that this requires the HeaderIndexMap to be created and not a null value.

func (MapReader) Read

func (m MapReader) Read() (map[string]string, error)

Read mimics the built-in CSV reader Read method, returning one row of the CSV. The only difference here being that obviously we return a map instead of a slice.

func (MapReader) ReadAll

func (m MapReader) ReadAll() ([]map[string]string, error)

ReadAll mimics the built-in CSV reader ReadAll method

type MapWriter

type MapWriter struct {
	InputHeaders    []string
	OutputHeaderMap map[string]int
	Writer          *csv.Writer
}

MapWriter mimics the writer struct.

func NewWriter

func NewWriter(w io.Writer, headers []string) *MapWriter

NewWriter creates a new writer

func (*MapWriter) Flush

func (w *MapWriter) Flush()

Flush simply calls the built-in CSV writer's flush method

func (MapWriter) Write

func (w MapWriter) Write(row map[string]string) error

Write recreates the built-in CSV writer's Write method

func (MapWriter) WriteAll

func (w MapWriter) WriteAll(rows []map[string]string) error

WriteAll rereates the built-in CSV writer's WriteAll method

Jump to

Keyboard shortcuts

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