multicsv

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2022 License: MIT Imports: 4 Imported by: 0

README

multicsv

build Go Report Card MIT License

Provides helpers on top of html/template to dynamically parse all templates from the specific directory and provides a unified interface for accessing them. In addition, the package provides the ability to use the Jinja2/Django like {{ extends }} tag.

Install and update

go get -u github.com/zero-pkg/multicsv

How to use

r := multicsv.NewReader(
    multicsv.LazyFileReader("data/users.csv", true),
    multicsv.LazyFileReader("data/users2.csv", true),
)

records, err := r.ReadAll()
if err != nil {
    panic(err)
}

fmt.Println(records)

Extending LazyReader

func main() {
	r := multicsv.NewReader(
		customReader("data/count_10.csv"),
		customReader("data/count_100.csv"),
	)
}

func customReader(file string) *multicsv.LazyReader {
	return &multicsv.LazyReader{
		Init: func() (*csv.Reader, error) {
			f, err := os.Open(file)
			if err != nil {
				return nil, err
			}

			// customize csv.Reader
			r := csv.NewReader(f)
			r.LazyQuotes = true

			return r, nil
		},
	}
}

License

http://www.opensource.org/licenses/mit-license.php

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type InitFunc

type InitFunc func() (*csv.Reader, error)

InitFunc is called during the first time reading from LazyReader

type LazyReader

type LazyReader struct {
	Init InitFunc
	// contains filtered or unexported fields
}

LazyReader allows delayed opening of a resource. It can be used to delay opening a resource until the resource is actually read.

func (*LazyReader) Read

func (r *LazyReader) Read() (record []string, err error)

Read calls Read func from reader that will be returned by InitFunc.

type MultiReader

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

MultiReader is the logical concatenation of the provided input readers. They're read sequentially. Once all inputs have returned EOF, Read will return EOF. If any of the readers return a non-nil, non-EOF error, Read will return that error.

func NewReader

func NewReader(readers ...Reader) *MultiReader

NewReader returns a Reader that's the logical concatenation of the provided input readers. They're read sequentially. Once all inputs have returned EOF, Read will return EOF. If any of the readers return a non-nil, non-EOF error, Read will return that error.

func (*MultiReader) Read

func (mr *MultiReader) Read() (record []string, err error)

Read reads one record (a slice of fields) from the provided input readers. Following code was taken from https://go.dev/src/io/multi.go and adopted to works with csv readers.

func (*MultiReader) ReadAll

func (mr *MultiReader) ReadAll() (records [][]string, err error)

ReadAll reads all the remaining records from the provided input readers.

type Reader

type Reader interface {
	Read() (record []string, err error)
}

func LazyFileReader

func LazyFileReader(filepath string, skipHeader bool) Reader

LazyFileReader returns a LazyReader with a predefined InitFunc, which can be used in most cases. Optionally supports the CSV header skip option.

Jump to

Keyboard shortcuts

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