Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

togo-framework/import-export

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

2 Commits

Repository files navigation

togo

togo-framework/import-export

marketplace pkg.go.dev MIT

CSV & Excel data import/export for togo — mapping, validation, resources.

Install

togo install togo-framework/import-export

The togo answer to Laravel Excel / django-import-export: export any rows 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.

Usage

Export

ie, _ := importexport.FromKernel(k)
rows := []map[string]any{{"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"}}
csvBytes, _ := ie.Export(ctx, rows, importexport.ExportOpts{Format: "csv", Columns: []string{"id", "name"}})
xlsxBytes, _ := ie.Export(ctx, rows, importexport.ExportOpts{Format: "xlsx", Sheet: "People"})

Import (mapping + validation)

data := []byte("Identifier,Full Name\n1,Alice\n,Missing\n")
rows, rowErrors, err := ie.Import(ctx, data, importexport.ImportOpts{
 Format: "csv",
 Mapping: map[string]string{"Identifier": "id", "Full Name": "name"},
 Validate: func(row map[string]any) error {
 if row["id"] == "" { return errors.New("id is required") }
 return nil
 },
})
// rows = valid rows (mapped to id/name); rowErrors = []RowError{ {Row:2, Message:"id is required"} }

Registered resources

importexport.Register(importexport.Resource{
 Name: "people",
 Columns: []string{"id", "name"},
 Fetch: func(ctx context.Context) ([]map[string]any, error) { return loadPeople(ctx) },
 Insert: func(ctx context.Context, row map[string]any) error { return savePerson(ctx, row) },
})
// then export/import by name via the Go API or REST.

REST API

Method Path Description
GET /api/export/{resource}?format=csv|xlsx Download a registered resource
POST /api/import/{resource}?format=csv|xlsx Upload + insert; returns {inserted, errors}

Configuration

No required env. XLSX uses excelize; CSV uses the stdlib.


Premium sponsors

ID8 Media · One Studio

Support togo — become a sponsor.

About

togo import/export — CSV & Excel data import/export with mapping & validation

Topics

Resources

License

Stars

Watchers

Forks

Packages

Contributors

Languages

AltStyle によって変換されたページ (->オリジナル) /