fwencoder

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2022 License: MIT Imports: 13 Imported by: 3

README

Fixed width file parser (encoder/decoder) for GO (golang)

License GoDoc Build Status Coverage Status Go Report Card

This library is using to parse fixed-width table data like:

Name            Address               Postcode Phone          Credit Limit Birthday
Evan Whitehouse V4560 Camel Back Road 3122     (918) 605-5383    1000000.5 19870101
Chuck Norris    P.O. Box 872          77868    (713) 868-6003     10909300 19651203

Install

To install the library use the following command:

$ go get -u github.com/o1egl/fwencoder

Decoding example

Parsing data from io.Reader:

type Person struct {
	Name        string
	Address     string
	Postcode    int
	Phone       string
	CreditLimit float64   `json:"Credit Limit"`
	Bday        time.Time `column:"Birthday" format:"20060102"`
}

f, _ := os.Open("/path/to/file")
defer f.Close

var people []Person
err := fwencoder.UnmarshalReader(f, &people)

You can also parse data from byte array:

b, _ := ioutil.ReadFile("/path/to/file")
var people []Person
err := fwencoder.Unmarshal(b, &people)

Encoding example

people := []Person{
	Name: "John",
	Address: "P.O. Box 872",
	Phone: "(713) 868-6003", 
	CreditLimit: 10909300,
	Bday: "19651203"
}

b, err := Marshal(&people)
fmt.Println(string(b))

or you can directly write to io.Writer

people := []Person{
	Name: "John",
	Address: "P.O. Box 872",
	Phone: "(713) 868-6003", 
	CreditLimit: 10909300,
	Bday: "19651203"
}

err := MarshalWriter(os.Stdout, &people)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrIncorrectInputValue represents wrong input param
	ErrIncorrectInputValue = errors.New("value is not a pointer to slice of structs")
)

Functions

func Marshal

func Marshal(v interface{}) ([]byte, error)

Marshal returns the fixed width table data encoding of v If v is nil or not a pointer to slice of structs, Unmarshal returns an ErrIncorrectInputValue.

By default, Marshal converts struct's field names to column names. This behavior could be overridden by `column` or `json` tags.

To unmarshal raw data into a struct, Unmarshal tries to convert every column's data from string to Marshal converts base go types into their string representation (int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64, string, bool, time.Time) It also supports slices and custom types by converting them to JSON.

By default, time.RFC3339 is used to parse time.Time data. To override this behavior use `format` tag. For example:

type Person struct {
    Name     string
    BDate    time.Time `column:"Birthday" format:"2006/01/02"`
    Postcode int       `json:"Zip"`
}

func MarshalWriter

func MarshalWriter(writer io.Writer, v interface{}) (err error)

MarshalWriter behaves the same as Marshal, but write data into io.Writer

func Unmarshal

func Unmarshal(data []byte, v interface{}) error

Unmarshal parses the fixed width table data and stores the result in the value pointed to by v. If v is nil or not a pointer to slice of structs, Unmarshal returns an ErrIncorrectInputValue.

To unmarshal raw data into a struct, Unmarshal tries to convert every column's data from string to supported types (int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64, string, bool, time.Time). It also supports slices and custom types by reading them as JSON.

By default, Unmarshal tries to match column names to struct's field names. This behavior could be overridden by `column` or `json` tags.

By default, time.RFC3339 is used to parse time.Time data. To override this behavior use `format` tag. For example:

type Person struct {
    Name     string
    BDate    time.Time `column:"Birthday" format:"2006/01/02"`
    Postcode int       `json:"Zip"`
}

func UnmarshalReader

func UnmarshalReader(reader io.Reader, v interface{}) (err error)

UnmarshalReader behaves the same as Unmarshal, but reads data from io.Reader

Types

This section is empty.

Jump to

Keyboard shortcuts

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