csv2structs

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2025 License: MIT Imports: 10 Imported by: 0

README

CSV2Structs

GoDoc Go Report Card

Package csv2structs parses CSV data into a slice of structs.

Example Usage

package main

import (
	"fmt"
	"strings"

	"github.com/digitalocean-labs/csv2structs"
)

func main() {
	csvData := `first_name,age
Alice,30
Bob,25
Charlie,35`

	type Person struct {
		FirstName string
		Age       int
	}

	r := strings.NewReader(csvData)
	people, err := csv2structs.Parse[Person](r)
	if err != nil {
		fmt.Println("error:", err)
		return
	}

	for _, p := range people {
		fmt.Printf("%+v\n", p)
	}
}

The above is available as a runnable example.

Headers

All exported fields in the struct passed must match the headers in the CSV data.

By default, the headers in the CSV data are transformed from snake_case to TitleCase.

If you want to disable the header transformation, you can use the WithHeaderType option with the HeaderTypeNone value:

people, err := csv2structs.Parse[Person](r, csv2structs.WithHeaderType(csv2structs.HeaderTypeNone))

If your CSV data has headers in a different format, you can implement your own header transformation function and pass it to the WithHeaderTransform option:

func customHeaderTransform(header string) string {
    // your custom header transformation logic
}

people, err := csv2structs.Parse[Person](r, csv2structs.WithHeaderTransform(customHeaderTransform))

Or, if your CSV data has headers in snake_case format and you want to be explicit, you can use the WithHeaderType option with the HeaderTypeSnake value:

people, err := csv2structs.Parse[Person](r, csv2structs.WithHeaderType(csv2structs.HeaderTypeSnake))

License

This project is licensed under the MIT License - see the LICENSE file for details.

Documentation

Overview

Package csv2structs parses CSV data into a slice of structs.

Example Usage

package main

import (
	"fmt"
	"strings"

	"github.com/digitalocean-labs/csv2structs"
)

func main() {
	csvData := `first_name,age
Alice,30
Bob,25
Charlie,35`

	type Person struct {
		FirstName string
		Age       int
	}

	r := strings.NewReader(csvData)
	people, err := csv2structs.Parse[Person](r)
	if err != nil {
		fmt.Println("error:", err)
		return
	}

	for _, p := range people {
		fmt.Printf("%+v\n", p)
	}
}

Headers

All exported fields in the struct passed must match the headers in the CSV data.

By default, the headers in the CSV data are transformed from snake_case to TitleCase.

If you want to disable the header transformation, you can use the WithHeaderType option with the HeaderTypeNone value:

people, err := csv2structs.Parse[Person](r, csv2structs.WithHeaderType(csv2structs.HeaderTypeNone))

If your CSV data has headers in a different format, you can implement your own header transformation function and pass it to the WithHeaderTransform option:

func customHeaderTransform(header string) string {
	// your custom header transformation logic
}

people, err := csv2structs.Parse[Person](r, csv2structs.WithHeaderTransform(customHeaderTransform))

Or, if your CSV data has headers in snake_case format and you want to be explicit, you can use the WithHeaderType option with the HeaderTypeSnake value:

people, err := csv2structs.Parse[Person](r, csv2structs.WithHeaderType(csv2structs.HeaderTypeSnake))

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Parse

func Parse[T any](reader io.Reader, opts ...Option) ([]*T, error)

Parse parses a CSV and returns a slice of structs

Types

type HeaderType

type HeaderType int32
const (
	HeaderTypeNone  HeaderType = iota // do not munge csv headers
	HeaderTypeSnake                   // snake_case csv headers
)

type Option

type Option func(*options)

func WithHeaderTransform

func WithHeaderTransform(fn func(string) string) Option

WithHeaderTransform returns an Option to set a custom header transformation function

func WithHeaderType

func WithHeaderType(ht HeaderType) Option

WithHeaderType returns an Option to set the header type depending on your CSV data

type Parser

type Parser[T any] interface {
	Read() (*T, error)
	ReadAll() ([]*T, error)
}

Parser can be used to read a single or all structs from an io.Reader containing CSV data

func NewParser

func NewParser[T any](reader io.Reader, opts ...Option) (Parser[T], error)

NewParser returns a new Parser implementation for the given io.Reader containing CSV data

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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