ltsv

package module
v0.0.0-...-40eb84a Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2015 License: BSD-3-Clause Imports: 10 Imported by: 8

README

ltsv
====

LTSV (Labeled Tab-separated Values) reader/writer for Go language.

About LTSV: http://ltsv.org/

	Labeled Tab-separated Values (LTSV) format is a variant of 
	Tab-separated Values (TSV).Each record in a LTSV file is represented 
	as a single line. Each field is separated by TAB and has a label and
	 a value. The label and the value have been separated by ':'. With 
	the LTSV format, you can parse each line by spliting with TAB (like 
	original TSV format) easily, and extend any fields with unique labels 
	in no particular order.


Example
=======

Reader
------

::

  package main
  
  import (
  	"bytes"
  	"fmt"
  	"github.com/najeira/ltsv"
  )
  
  func main() {
  	data := `
  time:05/Feb/2013:15:34:47 +0000 host:192.168.50.1	req:GET / HTTP/1.1	status:200
  time:05/Feb/2013:15:35:15 +0000 host:192.168.50.1	req:GET /foo HTTP/1.1	status:200
  time:05/Feb/2013:15:35:54 +0000 host:192.168.50.1	req:GET /bar HTTP/1.1	status:404
  `
  	b := bytes.NewBufferString(data)
  	
  	// Read LTSV file into map[string]string
  	reader := ltsv.NewReader(b)
  	records, err := reader.ReadAll()
  	if err != nil {
  		panic(err)
  	}
  	
  	// dump
  	for i, record := range records {
  		fmt.Printf("===== Data %d\n", i)
  		for k, v := range record {
  			fmt.Printf("\t%s --> %s\n", k, v)
  		}
  	}
  }


Writer
------

::

  package main
  	
  import (
  	"fmt"
  	"bytes"
  	"github.com/najeira/ltsv"
  )
  	
  func main() {
  	data := []map[string]string {
  		{"time": "05/Feb/2013:15:34:47 +0000", "host": "192.168.50.1", "req": "GET / HTTP/1.1", "status": "200"},
  		{"time": "05/Feb/2013:15:35:15 +0000", "host": "192.168.50.1", "req": "GET /foo HTTP/1.1", "status": "200"},
  		{"time": "05/Feb/2013:15:35:54 +0000", "host": "192.168.50.1", "req": "GET /bar HTTP/1.1", "status": "404"},
  	}
  	
  	b := &bytes.Buffer{}
  	writer := ltsv.NewWriter(b)
  	err := writer.WriteAll(data)
  	if err != nil {
  		panic(err)
  	}
  	fmt.Printf("%v", b.String())
  }


License
=======

New BSD License.


Links
=====

- http://ltsv.org/
- https://github.com/ymotongpoo/goltsv  LTSV package by ymotongpoo

Documentation

Overview

based on encoding/csv

based on encoding/csv

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUnsupportedType = errors.New("unsupported type")
	ErrLabelInvalid    = errors.New("label is invalid")
	ErrFieldInvalid    = errors.New("field is invalid")
)

Functions

This section is empty.

Types

type Reader

type Reader struct {
	Delimiter rune // Field delimiter (set to '\t' by NewReader)
	Comment   rune // Comment character for start of line
	// contains filtered or unexported fields
}

A Reader reads records from a LTSV-encoded file.

As returned by NewReader, a Reader expects input LTSV-encoded file. The exported fields can be changed to customize the details before the first call to Read or ReadAll.

Delimiter is the field delimiter. It defaults to '\t'.

Comment, if not 0, is the comment character. Lines beginning with the Comment character are ignored.

func NewReader

func NewReader(r io.Reader) *Reader

NewReader returns a new Reader that reads from r.

func (*Reader) Load

func (r *Reader) Load(record interface{}) error

func (*Reader) Read

func (r *Reader) Read() (map[string]string, error)

Read reads one record from r. The record is a slice of strings with each string representing one field.

func (*Reader) ReadAll

func (r *Reader) ReadAll() ([]map[string]string, error)

type Writer

type Writer struct {
	Delimiter rune // Label delimiter (set to to '\t' by NewWriter)
	UseCRLF   bool // True to use \r\n as the line terminator
	// contains filtered or unexported fields
}

A Writer writes records to a LTSV encoded file.

As returned by NewWriter, a Writer writes records terminated by a newline and uses '\t' as the field delimiter. The exported fields can be changed to customize the details before the first call to Write or WriteAll.

Delimiter is the field delimiter.

If UseCRLF is true, the Writer ends each record with \r\n instead of \n.

func NewWriter

func NewWriter(w io.Writer) *Writer

NewWriter returns a new Writer that writes to w.

func (*Writer) Flush

func (w *Writer) Flush()

Flush writes any buffered data to the underlying io.Writer.

func (*Writer) Write

func (w *Writer) Write(record interface{}) error

Writer writes a single CSV record to w along with any necessary quoting. A record is a slice of strings with each string being one field.

func (*Writer) WriteAll

func (w *Writer) WriteAll(records interface{}) error

WriteAll writes multiple LTSV records to w using Write and then calls Flush.

Jump to

Keyboard shortcuts

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