vinyl

package module
v0.0.0-...-3543a59 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2018 License: MIT Imports: 5 Imported by: 0

README

Vinyl

Exchange CSV Records with Structs

Examples
Writer
type Album struct {
    Name string
    Count uint
    Timestamp time.Time
}

func WriteAlbums() {
    buf := bytes.NewBuffer(nil)
    w := csv.NewWriter(buf)
    headers := vinyl.Labels(Album{}, vinyl.SnakeFormat)
    if err := w.Write(headers); err != nil {
        log.Fatal(err)
    }
    for i := 0; i < 10; i++ {
        album := Album {
            Name: fmt.Sprint("Album", i),
            Count: 2 << i,
            Timestamp: time.Now(),
        }
        record := vinyl.New(album)
        row := record.Values()
        if err := w.Write(row); err != nil {
            log.Fatal(err)
        }
    }
}

Reader
top_record_artists.csv
###
name, platinum, multi_platinum, diamond
"Elvis Presley", 67, 27, 1
"The Beatles", 42, 26, 6 
"George Strait", 33, 13, 0
"Garth Brooks", 30, 16, 7
"Barbra Steisand", 30, 12, 0

type Artist {
    Name            string
    Platinum        string
    MultiPlatinum   string
    Diamond         string
}
func ReadArtists() {
    f, err :=  f, err := os.Open("top_record_artists.csv")
    if err != nil {
        log.Fatal(err)
    }
    defer f.Close()
    exhange, err := vinyl.Exchange(Artist{})
    if err != nil {
        log.Fatal(err)
    }
    type awardCount struct {
        Artist string
        Count int
    }
    r := csv.NewReader(bufio.NewReader(f))
    for {
        rec, err := r.Read()
        if err == io.EOF {
            break
        } else if err != nil {
            log.Fatal(err)
        }
        a := exchange.From(rec).(Artist)
        fmt.Println(a)
    }
}
Benchmarks
goos: darwin
goarch: amd64
pkg: github.com/infatuation/vinyl
BenchmarkCSVEncoding-8   	  500000	      2694 ns/op
BenchmarkLabels-8        	 2000000	       616 ns/op
BenchmarkValues-8        	 1000000	      1770 ns/op
BenchmarkDict-8          	 1000000	      2122 ns/op
BenchmarkNewRecord-8     	30000000	        58.7 ns/op
PASS
ok  	github.com/infatuation/vinyl	9.019s

Documentation

Overview

Package vinyl creates Records from structs inferring schema from exported fields via reflect. This package can make working with the stdlib "encoding/csv" more ergonomic by supplying a translation layer between structs and csv encoding that doesn't require extra annotation or codegen. All string marhsaling of labels and values is done via fmt.Sprint

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Exchange

func Exchange(v interface{}) (ex exchange, err error)

Exchange can read records from a row and translate them into the supplied concrete type. Only types with all exported string fields are supported.

type t stuct{A, B, C string}
recs, _ := vinyl.Exchange(t{})
row, _ := csvreader.Read()
v := recs.From(row).(t)

func Labels

func Labels(v interface{}, apply ...Formatter) []string

Labels represent the schema of the record and are inferred from the exported Fields of the supplied type Labels are deterministic in their names and order as defined by the supplied type Formatters will be applied in the order they are supplied

func SnakeFormat

func SnakeFormat(v string) string

SnakeFormat is a formatter to convert labels into snake_case

Types

type Formatter

type Formatter func(string) string

Formatter is a func to rename record labels

type Record

type Record struct {
	// contains filtered or unexported fields
}

Record is a generic type for holding interface types

func New

func New(v interface{}) Record

New will create a new record from the supplied interface

func (Record) Dict

func (rec Record) Dict(apply ...Formatter) map[string]string

Dict returns the records as values with the labels as keys Dict is the slowest as it also reflects labels from the records fields The Formatters will be applied to the keys in the order they are supplied

func (Record) Values

func (rec Record) Values() []string

Values will translate the record into a string slice. The values of the slice correspond to the records labels index.

Jump to

Keyboard shortcuts

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