Documentation
¶
Overview ¶
Package importexport adds CSV & Excel (XLSX) data import/export to togo — the togo answer to Laravel Excel / django-import-export.
Export any rows ([]map[string]any) or a registered Resource to CSV/XLSX, and import from CSV/XLSX with column mapping, header detection, and per-row validation that reports row/column errors instead of failing the whole file.
ie, _ := importexport.FromKernel(k)
csv, _ := ie.Export(ctx, rows, importexport.ExportOpts{Format: "csv", Columns: []string{"id","name"}})
out, errs, _ := ie.Import(ctx, data, importexport.ImportOpts{Format: "xlsx"})
Index ¶
- func Register(r Resource)
- type ExportOpts
- type ImportOpts
- type Resource
- type RowError
- type Service
- func (s *Service) Export(ctx context.Context, rows []map[string]any, opts ExportOpts) ([]byte, error)
- func (s *Service) ExportResource(ctx context.Context, name string, opts ExportOpts) ([]byte, error)
- func (s *Service) Import(ctx context.Context, data []byte, opts ImportOpts) ([]map[string]any, []RowError, error)
- func (s *Service) ImportResource(ctx context.Context, name string, data []byte, opts ImportOpts) (int, []RowError, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type ExportOpts ¶
type ExportOpts struct {
Format string // "csv" (default) or "xlsx"
Columns []string // column order; inferred (sorted) from the rows when empty
Sheet string // XLSX sheet name (default "Sheet1")
}
ExportOpts controls an export.
type ImportOpts ¶
type ImportOpts struct {
Format string // "csv" (default) or "xlsx"
Sheet string // XLSX sheet to read (default first)
Mapping map[string]string // source header -> target field; identity when empty
Validate func(row map[string]any) error
}
ImportOpts controls an import.
type Resource ¶
type Resource struct {
Name string
Columns []string
Fetch func(ctx context.Context) ([]map[string]any, error) // for export
Insert func(ctx context.Context, row map[string]any) error // for import
}
Resource is a named, importable/exportable dataset.
type RowError ¶
type RowError struct {
Row int `json:"row"` // 1-based data row (excludes header)
Column string `json:"column,omitempty"`
Message string `json:"message"`
}
RowError reports a problem with one imported row.
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service is the import/export runtime stored on the kernel.
func FromKernel ¶
FromKernel returns the import-export Service.
func (*Service) Export ¶
func (s *Service) Export(ctx context.Context, rows []map[string]any, opts ExportOpts) ([]byte, error)
Export renders rows to CSV or XLSX bytes.
func (*Service) ExportResource ¶
ExportResource fetches a registered resource and exports it.