tsv

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: May 21, 2020 License: MIT Imports: 7 Imported by: 0

README

Advanced TSV parser for Go

Based on github.com/dogenzaka/tsv but has some improvements

  • Support for []string
  • Support for float64
  • Support for float32
  • Support for nil values in columns
  • More resilient to empty spaces
  • Supports go modules

Note it is not a complete drop in replacement as some of the public API needed to be changed in order to support more functionality

tsv advanced is tab-separated values parser for GO. It will parse lines and insert data into any type of struct. tsv supports both simple structs and structs with tagging.

go get github.com/dllz/tsv-advanced

Quickstart

tsv inserts data into struct by fields order.


import (
    "fmt"
    "os"
    "testing"
    )

type TestRow struct {
  Name   string // 0
  Age    int    // 1
  Gender string // 2
  Active bool   // 3
}

func main() {

  file, _ := os.Open("example.tsv")
  defer file.Close()
  arrayDeliminator := ","
  nilSign := "\\N"
  data := TestRow{}
  parser, err := NewParser(file, &data, arrayDeliminator, nilSign)

  for {
    eof, err := parser.Next()
    if eof {
      return
    }
    if err != nil {
      panic(err)
    }
    fmt.Println(data)
  }

}

You can define tags to struct fields to map values.

type TestRow struct {
	Name        string   `tsv:"name"`
	Age         int      `tsv:"age"`
	Active      bool     `tsv:"active"`
	Gender      string   `tsv:"gender"`
	MiddleNames []string `tsv:"middleNames"`
	BigNumber   float64  `tsv:"bigNumber"`
	SmallNumber float32  `tsv:"smallNumber"`
}

Supported field types

Currently, this library supports limited fields but more can easily be added on request or feel free to open PRs with the added functionality

  • int
  • string
  • bool
  • float32
  • float64
  • []string

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Parser

type Parser struct {
	Headers []string
	Reader  *csv.Reader
	Data    interface{}
	// contains filtered or unexported fields
}

Parser has information for parser

func NewParser

func NewParser(reader io.Reader, data interface{}, arrayDeliminator string, nilSign string) (*Parser, error)

NewStructModeParser creates new TSV parser with given io.Reader as struct mode

func NewParserWithoutHeader

func NewParserWithoutHeader(reader io.Reader, data interface{}, arrayDeliminator string, nilSign string) *Parser

NewParserWithoutHeader creates new TSV parser with given io.Reader

func (*Parser) Next

func (p *Parser) Next() (eof bool, err error)

Next puts reader forward by a line

Jump to

Keyboard shortcuts

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