gtsv

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

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

Go to latest
Published: Jan 2, 2019 License: MIT Imports: 6 Imported by: 0

README

gtsv - Fast TSV Parser written in Go

Build Status Coverage Status Go Report Card GoDoc

Installation
$ go get -u github.com/yagi5/gtsv
Features
  • get values as specific type
  • get row and column numbers which error caused
Usage

Valid TSV case (column numbers of every row are all the same, and type specify is compatible) :

package main

import (
	"bytes"
	"fmt"

	"github.com/yagi5/gtsv"
)

func main() {
  tsv := "1\t2.1\ta\n" +
    "4\t5.2\tb\n" +
    "7\t8.3\tc\n"

  gt := gtsv.New(bytes.NewBufferString(tsv)) // pass io.Reader

  for gt.Next() {
    fmt.Println(gt.Int())
    fmt.Println(gt.Float64())
    fmt.Println(gt.String())
  }
  fmt.Println(gt.Error())
}

Output is:

1
2.1
a
4
5.2
b
7
8.3
c
<nil>

Invalid TSV Case:

func main() {
  tsvInvalid := "1\t2.1\ta\n" +
    "4\t5.2\tb\n" +
    "a\t8.3\tc\n" // first column is not int

  gt := gtsv.New(bytes.NewBufferString(tsvInvalid))

  for gt.Next() {
    fmt.Println(gt.Int())
    fmt.Println(gt.Float64())
    fmt.Println(gt.String())
  }

  if err := gt.Error(); err != nil {
    er := err.(gtsv.Error)
    fmt.Printf("error row: %d, col: %d", er.Row(), er.Col())
	}
}

Output is:

Output: error row: 3, col: 1

For more detail, see godoc.

Lisence

MIT

Documentation

Overview

Package gtsv is the package to read TSV formatted text.

It is designed to type-specifically use each tsv columns, and easily find row and column number which error happened.

This is the example to treat valid TSV.

package main

import (
  "bytes"
  "fmt"

  "github.com/yagi5/gtsv"
)

func main() {
  tsv := "1\t2.1\ta\n" +
    "4\t5.2\tb\n" +
    "7\t8.3\tc\n"

  gt := gtsv.New(bytes.NewBufferString(tsv))

  for gt.Next() {
    fmt.Println(gt.Int())
    fmt.Println(gt.Float64())
    fmt.Println(gt.String())
  }
  fmt.Println(gt.Error())
}

While gt.Next() returns true, you can load column value type-specific, like `gt.Int()` , `gt.String()` .

Of course, you can use this with struct.

type user struct {
  name string
  age  int
  male bool
}

func main() {
  tsv := "john\t18\ttrue\n" +
    "emily\t16\tfalse\n"

  gt := gtsv.New(bytes.NewBufferString(tsv))

 var users []*user
  for gt.Next() {
    users = append(users, &user{name: gt.String(), age: gt.Int(), male: gt.Bool()})
  }

  fmt.Println(gr.Error())
}

If you change the order to call `gt.String()` and `gt.Int()` , `gt.Error()` will be non-nil because it's not the same as TSV column order.

Sometimes, we want to know the error position of these files. If you need it, you can cast `gt.Error()` (this is just a `error`) into `gtsv.Error` to know error position. like this:

tsvInvalid := "1\t2.1\ta\n" +
  "4\t5.2\tb\n" +
  "a\t8.3\tc\n" // first column is not int

gt := gtsv.New(bytes.NewBufferString(tsvInvalid))

for gt.Next() {
  fmt.Println(gt.Int())
  fmt.Println(gt.Float64())
  fmt.Println(gt.String())
}

if err := gt.Error(); err != nil {
  er := err.(gtsv.Error)
  fmt.Printf("error row: %d, col: %d", er.Row(), er.Col())
}

if cast `err.(gtsv.Error)` succeeded, `Row()` and `Col()` show you error position.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Error

type Error interface {
	Row() int
	Col() int
}

Error is the error interface. If `gt.Error()` returned non-nil, usually it implements this interface. So, Row() and Col() will return error position.

type Reader

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

Reader contains some fields to store tsv-reading-information. It shouldn't be used by client so unexported.

func New

func New(r io.Reader) *Reader

New returnds new TSV reader. This holds passed io.Reader to read it from.

func (*Reader) Bool

func (gr *Reader) Bool() bool

Bool returns next column as bool. If error had happened, it always returns false. Bool() uses `strconv.ParseBool()` inside, so can read 1, t, T, TRUE, true, True, 0, f, F, FALSE, false, False. If any other value, it will be false.

func (*Reader) Bytes

func (gr *Reader) Bytes() []byte

Bytes returns next column as []byte. If error had happened, it always returns nil. Escape sequences will be unescaped.

func (*Reader) Error

func (gr *Reader) Error() error

Error returns TSV reading error. If something is wrong, Error() returns error. It is important to know one thing, that `Error()` doesn't returns io.EOF. If `Next()` returned false && and `Error()` returned nil, reading TSV is correctly finished.

func (*Reader) Float32

func (gr *Reader) Float32() float32

Float32 returns next column as float32. If error had happened, it always returns zero-value.

func (*Reader) Float64

func (gr *Reader) Float64() float64

Float64 returns next column as float64. If error had happened, it always returns zero-value.

func (*Reader) Int

func (gr *Reader) Int() int

Int returns next column as int. If error had happened, it always returns zero-value.

func (*Reader) Int16

func (gr *Reader) Int16() int16

Int16 returns next column as int16. If error had happened, it always returns zero-value.

func (*Reader) Int32

func (gr *Reader) Int32() int32

Int32 returns next column as int32. If error had happened, it always returns zero-value.

func (*Reader) Int64

func (gr *Reader) Int64() int64

Int64 returns next column as int64. If error had happened, it always returns zero-value.

func (*Reader) Int8

func (gr *Reader) Int8() int8

Int8 returns next column as int8. If error had happened, it always returns zero-value.

func (*Reader) Next

func (gr *Reader) Next() bool

Next returns true when next row exists. It's expected to use with `for` . If error had happened, `Next()` returns always false.

func (*Reader) String

func (gr *Reader) String() string

String returns next column as string.

func (*Reader) Uint

func (gr *Reader) Uint() uint

Uint returns next column as uint. If error had happened, it always returns zero-value.

func (*Reader) Uint16

func (gr *Reader) Uint16() uint16

Uint16 returns next column as uint16. If error had happened, it always returns zero-value.

func (*Reader) Uint32

func (gr *Reader) Uint32() uint32

Uint32 returns next column as uint32. If error had happened, it always returns zero-value.

func (*Reader) Uint64

func (gr *Reader) Uint64() uint64

Uint64 returns next column as uint64. If error had happened, it always returns zero-value.

func (*Reader) Uint8

func (gr *Reader) Uint8() uint8

Uint8 returns next column as uint8. If error had happened, it always returns zero-value.

Jump to

Keyboard shortcuts

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