csvmum

package
v0.0.0-...-3d9ac64 Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2024 License: GPL-3.0 Imports: 8 Imported by: 0

README

CSVMUM

CSV Marshal/Unmarshal

CSVMUM is a CSV marshaler/unmarshaler.

Marshal

Example

func marshal() {
	type person struct {
		Name string `csv:"name"`
		Age  int    `csv:"age"`
	}

	csvm, err := csvmum.NewMarshaler[person](os.Stdout)
	if err != nil {
		panic(err)
	}

	csvm.Marshal(person{Name: "Seanny Phoenix", Age: 38})
	csvm.Marshal(person{Name: "Somebody", Age: 27})
	csvm.Flush()
}

Output

name,age
Seanny Phoenix,38
Somebody,27

Unmarshal

Flush must be called for the csv writer to write to the underlying writer.

Example

func unmarshal() {
	type person struct {
		Name string `csv:"name"`
		Age  int    `csv:"age"`
	}

	r := bytes.NewBuffer([]byte("name,age\nNobody,0\nSpot,2\n"))
	csvu, err := csvmum.NewUnmarshaler[person](r)
	if err != nil {
		panic(err)
	}

	pp := []person{}
	for {
		var p person
		err = csvu.Unmarshal(&p)
		if err == io.EOF {
			break
		}
		if err != nil {
			panic(err)
		}
		pp = append(pp, p)
	}

	fmt.Println(pp)
}

Output

[{Nobody 0} {Spot 2}]

Tags

The csv tag can be used in structs to define the column name for a field. As with json.Marshal and json.Unmarshal, a field tagged with a hyphen (-) will be ignored.

Example

func tags() {
	type tagged struct {
		AsIs       string  // marshaled as "AsIs"
		Renamed    float64 `csv:"renamed"` // marshaled as "renamed"
		unexported int     // not marshaled
		Ignored    bool    `csv:"-"` // not marshaled
	}

	taggedData := tagged{
		AsIs:       "as is",
		Renamed:    27.72,
		unexported: 2,
		Ignored:    true,
	}

	csvm, err := csvmum.NewMarshaler[tagged](os.Stdout)
	if err != nil {
		panic(err)
	}

	csvm.Marshal(taggedData)
	csvm.Flush()
}

Output

AsIs,renamed
as is,27.72

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CSVMarshaler

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

func NewCSVMarshaler

func NewCSVMarshaler[T any](w *csv.Writer) (*CSVMarshaler[T], error)

func NewMarshaler

func NewMarshaler[T any](w io.Writer) (*CSVMarshaler[T], error)

func (*CSVMarshaler[T]) Flush

func (m *CSVMarshaler[T]) Flush() error

func (*CSVMarshaler[T]) Marshal

func (m *CSVMarshaler[T]) Marshal(record T) error

type CSVUnmarshaler

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

func NewCSVUnmarshaler

func NewCSVUnmarshaler[T any](r *csv.Reader) (*CSVUnmarshaler[T], error)

func NewUnmarshaler

func NewUnmarshaler[T any](r io.Reader) (*CSVUnmarshaler[T], error)

func (*CSVUnmarshaler[T]) Unmarshal

func (um *CSVUnmarshaler[T]) Unmarshal(record *T) error

Jump to

Keyboard shortcuts

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