xsv

package module
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2023 License: MIT Imports: 12 Imported by: 0

README

XSV

csv handling package written in go

Licence Code Size Release Github Stars

Most of the programs related to csv generation and reading are created from code in this repository.↓

Copyright (c) 2014 Jonathan Picques https://github.com/gocarina/gocsv

※xsv does not include gocsv.

🚀Getting Started

go get github.com/shigetaichi/xsv

🔨Usage

package main

import (
	"os"
	"xsv"
)

type Client struct { // Our example struct, you can use "-" to ignore a field
	ID            string `csv:"client_id"`
	Name          string `csv:"client_name"`
	Age           string `csv:"client_age"`
	NotUsedString string `csv:"-"`
}

func main() {
	clients := []*Client{
		{ID: "12", Name: "John", Age: "21"},
		{ID: "13", Name: "Fred"},
		{ID: "14", Name: "James", Age: "32"},
		{ID: "15", Name: "Danny"},
	}

	// Create an empty clients file
	clientsFile, err := os.OpenFile("clients.csv", os.O_RDWR|os.O_CREATE|os.O_TRUNC, os.ModePerm)
	if err != nil {
		panic(err)
	}
	defer clientsFile.Close()

	// instancing xsvWrite struct
	xsvWrite := xsv.NewXsvWrite[*Client]()
	// change some preferences
	xsvWrite.OmitHeaders = true
	// set writer and write!
	err = xsvWrite.SetFileWriter(clientsFile).Write(clients)
	if err != nil {
		return
	}

	// instancing xsvRead struct
	xsvRead := xsv.NewXsvRead[*Client]()
	// change some preferences
	xsvRead.TagName = "xsv"
	// set reader and read!
	var clientOutput []*Client
	err = xsvRead.SetFileReader(clientsFile).ReadTo(&clientOutput)
	if err != nil {
		return
	}
}

🛠️Details

XsvWrite
  • TagName: string
    • Key in the struct field's tag to scan
  • TagSeparator: string
    • Separator string for multiple CSV tags in struct fields
  • OmitHeaders: bool
    • Whether to output headers to CSV or not
  • SelectedColumns: []string
    • Slice of field names (which is set in "TagName" tag) to output
  • SortOrder: []uint
    • Column sort order
  • HeaderModifier: map[string]string
    • Map to dynamically change headers
  • OnRecord func(T) T
    • Callback function to be called on each record
XsvRead
  • TagName: string
    • Key in the struct field's tag to scan
  • TagSeparator: string
    • Separator string for multiple CSV tags in struct fields
  • FailIfUnmatchedStructTags: bool
    • Indicates whether it is considered an error when there is an unmatched struct tag.
  • FailIfDoubleHeaderNames: bool
    • Indicates whether it is considered an error when a header name is repeated in the CSV header.
  • ShouldAlignDuplicateHeadersWithStructFieldOrder: bool
    • Indicates whether duplicate CSV headers should be aligned per their order in the struct definition.
  • OnRecord func(T) T
    • Callback function to be called on each record
  • NameNormalizer: Normalizer(func(string) string)
    • Normalizer is a function that takes and returns a string. It is applied to struct and header field values before they are compared. It can be used to alter names for comparison. For instance, you could allow case-insensitive matching or convert '-' to '_'.
  • ErrorHandler: ErrorHandler(func(*csv.ParseError) bool)
    • ErrorHandler is a function that takes an error and handles it. It can be used to log errors or to panic.

Documentation

Overview

Package xsv aims to provide easy CSV serialization and deserialization to the golang programming language

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrEmptyCSVFile = errors.New("empty csv file given")
	ErrNoStructTags = errors.New("no csv struct tags found")
)
View Source
var (
	ErrChannelIsClosed = errors.New("channel is closed")
)

Functions

func CSVToMap

func CSVToMap(in io.Reader) (map[string]string, error)

CSVToMap creates a simple map from a CSV of 2 columns.

func UnmarshalCSVToMap

func UnmarshalCSVToMap(r *csv.Reader, out interface{}) error

UnmarshalCSVToMap parses a CSV of 2 columns into a map.

Types

type ErrorHandler

type ErrorHandler func(*csv.ParseError) bool

type NoMarshalFuncError

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

NoMarshalFuncError is the custom error type to be raised in case there is no marshal function defined on type

func (NoMarshalFuncError) Error

func (e NoMarshalFuncError) Error() string

type NoUnmarshalFuncError

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

NoUnmarshalFuncError is the custom error type to be raised in case there is no unmarshal function defined on type

func (NoUnmarshalFuncError) Error

func (e NoUnmarshalFuncError) Error() string

type Normalizer

type Normalizer func(string) string

Normalizer is a function that takes and returns a string. It is applied to struct and header field values before they are compared. It can be used to alter names for comparison. For instance, you could allow case insensitive matching or convert '-' to '_'.

type TypeMarshaller

type TypeMarshaller interface {
	MarshalCSV() (string, error)
}

TypeMarshaller is implemented by any value that has a MarshalCSV method This converter is used to convert the value to it string representation

type TypeUnmarshalCSVWithFields

type TypeUnmarshalCSVWithFields interface {
	UnmarshalCSVWithFields(key, value string) error
}

TypeUnmarshalCSVWithFields can be implemented on whole structs to allow for whole structures to customized internal vs one off fields

type TypeUnmarshaller

type TypeUnmarshaller interface {
	UnmarshalCSV(string) error
}

TypeUnmarshaller is implemented by any value that has an UnmarshalCSV method This converter is used to convert a string to your value representation of that string

type XsvRead

type XsvRead[T any] struct {
	TagName                                         string    //key in the struct field's tag to scan
	TagSeparator                                    string    //separator string for multiple csv tags in struct fields
	FailIfUnmatchedStructTags                       bool      // indicates whether it is considered an error when there is an unmatched struct tag.
	FailIfDoubleHeaderNames                         bool      // indicates whether it is considered an error when a header name is repeated in the csv header.
	ShouldAlignDuplicateHeadersWithStructFieldOrder bool      // indicates whether we should align duplicate CSV headers per their alignment in the struct definition.
	OnRecord                                        func(T) T // callback function to be called on each record
	NameNormalizer                                  Normalizer
	ErrorHandler                                    ErrorHandler
}

XsvRead manages configuration values related to the csv read process.

func NewXsvRead

func NewXsvRead[T any]() *XsvRead[T]

NewXsvRead creates a new XsvRead struct with default configuration values

func (*XsvRead[T]) SetByteReader

func (x *XsvRead[T]) SetByteReader(byte []byte) (xr *XsvReader[T])

func (*XsvRead[T]) SetFileReader

func (x *XsvRead[T]) SetFileReader(file *os.File) (xr *XsvReader[T])

func (*XsvRead[T]) SetReader

func (x *XsvRead[T]) SetReader(r *csv.Reader) (xr *XsvReader[T])

func (*XsvRead[T]) SetStringReader

func (x *XsvRead[T]) SetStringReader(string string) (xr *XsvReader[T])

type XsvReader

type XsvReader[T any] struct {
	XsvRead[T]
	// contains filtered or unexported fields
}

func NewXsvReader

func NewXsvReader[T any](xsvRead XsvRead[T]) *XsvReader[T]

func (*XsvReader[T]) Lazy

func (r *XsvReader[T]) Lazy() *XsvReader[T]

func (*XsvReader[T]) ReadEach

func (r *XsvReader[T]) ReadEach(c chan T) error

func (*XsvReader[T]) ReadEachWithoutHeaders

func (r *XsvReader[T]) ReadEachWithoutHeaders(c chan T) error

func (*XsvReader[T]) ReadTo

func (r *XsvReader[T]) ReadTo(out *[]T) error

func (*XsvReader[T]) ReadToCallback

func (r *XsvReader[T]) ReadToCallback(f func(s T) error) error

func (*XsvReader[T]) ReadToWithoutHeaders

func (r *XsvReader[T]) ReadToWithoutHeaders(out *[]T) error

func (*XsvReader[T]) ToChanMaps

func (r *XsvReader[T]) ToChanMaps(c chan<- map[string]string) error

func (*XsvReader[T]) ToMap

func (r *XsvReader[T]) ToMap() ([]map[string]string, error)

type XsvWrite

type XsvWrite[T any] struct {
	TagName         string //key in the struct field's tag to scan
	TagSeparator    string //separator string for multiple csv tags in struct fields
	OmitHeaders     bool
	SelectedColumns []string          // slice of field names to output
	SortOrder       []int             // column sort order
	HeaderModifier  map[string]string // map to dynamically change headers
	OnRecord        func(T) T         // callback function to be called on each record
	// contains filtered or unexported fields
}

XsvWrite manages configuration values related to the csv write process.

func NewXsvWrite

func NewXsvWrite[T any]() XsvWrite[T]

NewXsvWrite creates a new XsvWrite struct with default configuration values

func (*XsvWrite[T]) SetBufferWriter

func (x *XsvWrite[T]) SetBufferWriter(buffer *bytes.Buffer) (xw *XsvWriter[T])

func (*XsvWrite[T]) SetFileWriter

func (x *XsvWrite[T]) SetFileWriter(file *os.File) (xw *XsvWriter[T])

func (*XsvWrite[T]) SetWriter

func (x *XsvWrite[T]) SetWriter(writer *csv.Writer) (xw *XsvWriter[T])

type XsvWriter

type XsvWriter[T any] struct {
	XsvWrite[T]
	// contains filtered or unexported fields
}

func NewXsvWriter

func NewXsvWriter[T any](xsvWrite XsvWrite[T]) *XsvWriter[T]

func (*XsvWriter[T]) Comma

func (xw *XsvWriter[T]) Comma(comma rune) *XsvWriter[T]

func (*XsvWriter[T]) UseCRLF

func (xw *XsvWriter[T]) UseCRLF(useCRLF bool) *XsvWriter[T]

func (*XsvWriter[T]) Write

func (xw *XsvWriter[T]) Write(data []T) error

func (*XsvWriter[T]) WriteFromChan

func (xw *XsvWriter[T]) WriteFromChan(dataChan chan T) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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