mapper

package module
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Aug 1, 2026 License: MIT Imports: 10 Imported by: 0

README

mapper

mapper is a Go library for importing CSV and XLSX files and mapping the data to Go structs.

Installation

go get github.com/audunhov/mapper

Example

package main

import (
	"fmt"
	"strings"

	"github.com/audunhov/mapper"
)

type User struct {
	FirstName string `map:"First Name"`
	LastName  string
	Age       int
	IsActive  bool   `map:"active"`
}

func main() {
	csvData := `First Name,last_name,Age,active
Alice,Smith,30,true
Bob,Jones,25,false`

	importer := mapper.NewCSVImporter(strings.NewReader(csvData))
	imported, err := importer.Import()
	if err != nil {
		panic(err)
	}

	users, err := mapper.Convert[User](imported, nil)
	if err != nil {
		panic(err)
	}

	for _, u := range users {
		fmt.Printf("%s %s (Age: %d, Active: %t)\n", u.FirstName, u.LastName, u.Age, u.IsActive)
	}
}

Documentation

See examples.md and web_example.md for more details.

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Convert

func Convert[T any](imported *Imported, mapping ImportMap, opts ...ConvertOption) ([]T, error)

Convert maps rows in Imported into a slice of type T (struct or pointer to struct) using the provided runtime ImportMap. If mapping is nil or empty, fields are mapped automatically using struct tags (`map`) and matching field names. It accepts optional ConvertOption parameters to customize error handling.

Types

type CSVImporter

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

func NewCSVImporter

func NewCSVImporter(reader io.Reader, opts ...CSVOption) *CSVImporter

func (*CSVImporter) Import

func (ci *CSVImporter) Import() (*Imported, error)

func (*CSVImporter) ReadHeaders

func (ci *CSVImporter) ReadHeaders() ([]string, error)

type CSVOption added in v0.1.2

type CSVOption func(*csv.Reader)

func WithComma added in v0.1.2

func WithComma(comma rune) CSVOption

WithComma sets the CSV field delimiter.

func WithComment added in v0.1.2

func WithComment(comment rune) CSVOption

WithComment sets the comment character.

func WithFieldsPerRecord added in v0.1.2

func WithFieldsPerRecord(num int) CSVOption

WithFieldsPerRecord sets the expected number of fields per record.

func WithLazyQuotes added in v0.1.2

func WithLazyQuotes(lazy bool) CSVOption

WithLazyQuotes configures the reader to allow lazy quotes.

func WithTrimLeadingSpace added in v0.1.2

func WithTrimLeadingSpace(trim bool) CSVOption

WithTrimLeadingSpace configures the reader to trim leading whitespace.

type ConvertOption

type ConvertOption func(*convertConfig)

ConvertOption allows customizing the behavior of Convert.

func WithErrorHandler

func WithErrorHandler(handler func(err error, rowIndex int, row map[string]string) error) ConvertOption

WithErrorHandler provides a custom callback to handle conversion errors on a per-row basis. If the callback returns nil, the row is skipped and conversion continues with the next row. If the callback returns a non-nil error, conversion is aborted immediately and that error is returned.

func WithSkipOnError

func WithSkipOnError() ConvertOption

WithSkipOnError is a convenience option that tells Convert to silently skip any rows that fail conversion.

type ImportMap

type ImportMap map[string]string

ImportMap defines a runtime mapping from input source keys (e.g. CSV header names) to exact struct field names.

type Imported

type Imported struct {
	Headers []string
	Rows    []map[string]string
}

Imported holds the headers and rows parsed from a source.

type Importer

type Importer interface {
	Import() (*Imported, error)
	ReadHeaders() ([]string, error)
}

Importer defines the interface for reading different file formats into Imported data.

type XLSXImporter

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

func NewXLSXImporter

func NewXLSXImporter(reader io.Reader, sheet string) *XLSXImporter

func (*XLSXImporter) Close added in v0.1.2

func (xi *XLSXImporter) Close() error

Close closes the underlying excelize File if it was opened.

func (*XLSXImporter) GetSheets added in v0.1.2

func (xi *XLSXImporter) GetSheets() ([]string, error)

GetSheets returns a list of sheet names in the XLSX file.

func (*XLSXImporter) Import

func (xi *XLSXImporter) Import() (*Imported, error)

func (*XLSXImporter) ReadHeaders

func (xi *XLSXImporter) ReadHeaders() ([]string, error)

func (*XLSXImporter) SetSheet added in v0.1.2

func (xi *XLSXImporter) SetSheet(sheet string)

SetSheet changes the target sheet to be imported and clears any cached import data.

Jump to

Keyboard shortcuts

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