csv2struct

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2024 License: MIT Imports: 6 Imported by: 0

README

go-csv2struct

go-csv2struct is a Go package that provides functionality to load CSV data into structs, with support for custom column mapping and data type retrieval.

Installation

go get github.com/jimmyclchu/go-csv2struct

Usage

Example CSV File

Create a CSV file named example.csv with the following content:

name,age,email
John Doe,30,johndoe@example.com
Jane Smith,25,janesmith@example.com
Genereate Struct
package main

import (
    "fmt"
    "github.com/username/go-csv2struct"
)

func main() {
    csvPath := "./example.csv"
    c := csv2struct.NewCSV2Struct()
    structSlice, err := c.GenerateStruct(csvPath)
    if err != nil {
        fmt.Println("Error:", err)
        return
    }

    fmt.Printf("%+v\n", structSlice)
}
Custom Column Mapping

You can set custom column mappings if the CSV headers do not match the struct field tags:

func main() {
    csvPath := "./example.csv"
    var people []Person
    c := csv2struct.NewCSV2Struct()
    c.SetCustomMap(map[string]string{"name": "full_name", "age": "years"})
    if err := c.LoadCSV(csvPath, &people); err != nil {
        fmt.Println("Error:", err)
        return
    }
    for _, person := range people {
        fmt.Printf("%+v\n", person)
    }
}
Manually Define Your Struct

If you prefer to define your struct manually, define a struct that matches the CSV columns:

type Person struct {
    Name  string `csv:"name"`
    Age   int    `csv:"age"`
    Email string `csv:"email"`
}

Then load CSV into Struct:

func main() {
    csvPath := "./example.csv"
    var people []Person
    c := csv2struct.NewCSV2Struct()
    if err := c.LoadCSV(csvPath, &people); err != nil {
        fmt.Println("Error:", err)
        return
    }
    for _, person := range people {
        fmt.Printf("%+v\n", person)
    }
}
Get Column Data Types
func main() {
    var people []Person
    c := csv2struct.NewCSV2Struct()
    columnTypes := c.GetColumnType(&people)
    for col, typ := range columnTypes {
        fmt.Printf("Column: %s, Type: %s\n", col, typ)
    }
}

Testing

go test ./...

License

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CSV2Struct

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

CSV2Struct defines the structure for the package

func NewCSV2Struct

func NewCSV2Struct() *CSV2Struct

NewCSV2Struct initializes a new instance of CSV2Struct

func (*CSV2Struct) GenerateStruct

func (c *CSV2Struct) GenerateStruct(filePath string) (interface{}, error)

GenerateStruct automatically generates a struct based on CSV headers

func (*CSV2Struct) GetColumnType

func (c *CSV2Struct) GetColumnType(out interface{}) map[string]string

GetColumnType retrieves the type of each column in the struct

func (*CSV2Struct) LoadCSV

func (c *CSV2Struct) LoadCSV(filePath string, out interface{}) error

LoadCSV loads a CSV file and unmarshals it into the given struct slice

func (*CSV2Struct) SetCustomMap

func (c *CSV2Struct) SetCustomMap(customMap map[string]string)

SetCustomMap sets custom mappings for CSV column headers

Jump to

Keyboard shortcuts

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