gocustomparser

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

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

Go to latest
Published: Feb 20, 2017 License: MIT Imports: 13 Imported by: 0

README

Small Go package to parse CSV and PRN files with type checking and struct transformation.

Go Get module

go get -u github.com/modeneis/gocustomparser

Tests:

For tests, see prnparser_test.go and csvparser_test.go

USAGE CSV

	type Workbook struct {
		Name        string `csv:"0" json:"name"`
		Address     string `csv:"1" json:"address"`
		Postcode    string `csv:"2" json:"postcode"`
		Phone       string `csv:"3" json:"phone"`
		CreditLimit string `csv:"4" json:"creditlimit"`
		Birthday    string `csv:"5" json:"birthday"`
	}
	csvParser := gocustomparser.CustomParser{
    		File:          "testfiles/Workbook2.csv",
            CustomDecoder: charmap.ISO8859_1.NewDecoder(),
    		Separator:     ',',
    		SkipFirstLine: true,
    }
    var csvParsedItems, err = csvParser.Parse(Workbook{})

USAGE PRN

    type Workbook struct {
		Name        string `prn:"0" json:"name"`
		Address     string `prn:"1" json:"address"`
		Postcode    string `prn:"2" json:"postcode"`
		Phone       string `prn:"3" json:"phone"`
		CreditLimit string `prn:"4" json:"creditlimit"`
		Birthday    string `prn:"5" json:"birthday"`
	}

    prnParser := gocustomparser.CustomParser{
		File:          "testfiles/Workbook2.prn",
        CustomDecoder: charmap.ISO8859_1.NewDecoder(),
		SkipFirstLine: true,
		PRNReader: func(raw string) (line []string, err error) {
			runes := []rune(raw)
			if len(runes) < 74 {
				err = fmt.Errorf("ReadPrnLine detected Wrong data -> %s", raw)
				return
			}
			line = append(line, strings.TrimSpace(string(runes[0:16])))
			line = append(line, strings.TrimSpace(string(runes[16:38])))
			line = append(line, strings.TrimSpace(string(runes[38:47])))
			line = append(line, strings.TrimSpace(string(runes[47:61])))
			line = append(line, strings.TrimSpace(string(runes[61:74])))
			line = append(line, strings.TrimSpace(string(runes[74:])))
			return
		},
	}
	var prnParsedItems, err = prnParser.Parse(Workbook{})

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CustomParser

type CustomParser struct {
	File            string
	Separator       rune
	SkipFirstLine   bool
	SkipEmptyValues bool
	PRNReader       func(raw string) (line []string, err error)
	CustomDecoder   *encoding.Decoder
}

func (CustomParser) GetCSVData

func (parser CustomParser) GetCSVData(f interface{}, decoded []byte, n int) (parsed []interface{}, err error)

Method to get CSV data parsed into struct

func (CustomParser) GetPRNData

func (parser CustomParser) GetPRNData(f interface{}, decoded []byte, n int) (parsed []interface{}, err error)

Method to get PRN data parsed into struct

func (CustomParser) Parse

func (parser CustomParser) Parse(f interface{}) (results []interface{}, error error)

type Parser

type Parser interface {
	Parse(resultType interface{})
}

Jump to

Keyboard shortcuts

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