csvx

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2021 License: Apache-2.0 Imports: 8 Imported by: 1

README

go-csvx

unit-tests

go-csvx implements the csv encoder and extends it with various functionalities. This library supports a typed csv format. The second column defines the data type.

Getting started

Since this repository serves as a library for typed and untyped csv parsing, you need to define it as a Go dependency to work with it. To declare it as a dependency in your project, use the go get command from below to attach it to your project:

go get github.com/programmfabrik/go-csvx

Defining a csv

Typed:

foo,bar,counter,names
string,string,int,"string,array"
test1,test2,10,"hello,world,how,is,it,going"

Untyped:

foo,bar,counter,names
hello,world,10,"hello,world,how,is,it,going"

Supported data types

This library supports the following list of data types:

  • string
  • int64
  • int
  • float64
  • bool
  • "string,array"
  • "int64,array"
  • "float64,array"
  • "bool,array"
  • json
  • *string
  • *int64
  • *int
  • *float64
  • *bool
  • *"string,array"
  • *"int64,array"
  • *"float64,array"
  • *"bool,array"
  • *json

Examples

Untyped example:

package main

import (
    "fmt"

    "github.com/programmfabrik/go-csvx"
)

func main() {
    csv := csvx.CSVParser{
        Comma:            ',',
        Comment:          '#',
        TrimLeadingSpace: true,
        SkipEmptyColumns: true,
    }

    data, _ := csv.Untyped([]byte(
        `foo,bar,counter,names
        hello,world,10,"hello,world,how,is,it,going"`))

    fmt.Printf("untyped data:\n\t%+#v\n", data)
}

Result:

untyped data:
        []map[string]interface {}{map[string]interface {}{"bar":"world", "counter":"10", "foo":"hello", "names":"hello,world,how,is,it,going"}}

Typed example:

package main

import (
    "fmt"

    "github.com/programmfabrik/go-csvx"
)

func main() {
    csv := csvx.CSVParser{
        Comma:            ',',
        Comment:          '#',
        TrimLeadingSpace: true,
        SkipEmptyColumns: true,
    }

    data, _ := csv.Typed([]byte(
        `foo,bar,counter,names
        string,string,int,"string,array"
        hello,world,10,"hello,world,how,is,it,going"`))

    fmt.Printf("untyped data:\n\t%+#v\n", data)
}

Result:

typed data:
        []map[string]interface {}{map[string]interface {}{"bar":"world", "counter":10, "foo":"hello", "names":[]string{"hello", "world", "how", "is", "it", "going"}}}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrDataIsNil                          = errors.New("data is nil")
	ErrOnlyOneRowIsAllowedForStringArray  = errors.New("only one row is allowed for type 'string,array'")
	ErrOnlyOneRowIsAllowedForInt64Array   = errors.New("only one row is allowed for type 'int64,array'")
	ErrOnlyOneRowIsAllowedForFloat64Array = errors.New("only one row is allowed for type 'float64,array'")
	ErrOnlyOneRowIsAllowedForBoolArray    = errors.New("only one row is allowed for type 'bool,array'")
	ErrInEmbeddedJSON                     = errors.New("unable to parse json in csv")
	ErrUnsupportedType                    = errors.New("unsupported type format type")
)

Functions

This section is empty.

Types

type CSVParser added in v1.1.0

type CSVParser struct {
	// Comma defines the rune with which the entries in the csv file are separated from each other.
	Comma rune
	// Comment defines the rune used to mark comment strings within the CSV.
	// If the line starts with this rune, the whole line is ignored.
	Comment rune
	// TrimLeadingSpace specifies whether leading spaces should be trimmed or not.
	TrimLeadingSpace bool
	// SkipEmptyColumns defines whether empty rows should be ignored or not.
	SkipEmptyColumns bool
	// contains filtered or unexported fields
}

func (*CSVParser) Typed added in v1.1.0

func (c *CSVParser) Typed(data []byte) ([]map[string]interface{}, error)

Typed unmarshals the typed data into a slice of map[string]interface{}

In this case, the second column of the csv must contain the field types, otherwise it will throw an error

func (*CSVParser) Untyped added in v1.1.0

func (c *CSVParser) Untyped(data []byte) ([]map[string]interface{}, error)

Untyped unmarshals the data into a slice of map[string]interface{}

Jump to

Keyboard shortcuts

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