tabular

package
v0.16.2 Latest Latest
Warning

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

Go to latest
Published: Dec 31, 2025 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// TagTabular is the struct tag name for tabular field configuration.
	TagTabular = "tabular"

	// Tag attributes for field configuration.
	AttrDive      = "dive"      // Recursively parse embedded struct
	AttrName      = "name"      // Column name (header)
	AttrWidth     = "width"     // Column width (for display/export hints)
	AttrOrder     = "order"     // Column order
	AttrDefault   = "default"   // Default value for import
	AttrFormat    = "format"    // Format template (date/number format)
	AttrFormatter = "formatter" // Custom formatter name for export
	AttrParser    = "parser"    // Custom parser name for import

	// Special tag value.
	IgnoreField = "-" // Ignore this field
)

Variables

View Source
var ErrUnsupportedType = errors.New("unsupported type")

ErrUnsupportedType indicates the target type is not supported by the parser.

Functions

func NewDefaultFormatter

func NewDefaultFormatter(format string) *defaultFormatter

NewDefaultFormatter creates a default formatter with optional format template.

func NewDefaultParser

func NewDefaultParser(format string) *defaultParser

NewDefaultParser creates a default parser with optional format template.

Types

type Column

type Column struct {
	// Index is the field index path in the struct
	Index []int
	// Name is the column name (header)
	Name string
	// Width is the column width hint (for display/export)
	Width float64
	// Order is the column order (for sorting)
	Order int
	// Default is the default value used during import when cell is empty
	Default string
	// Format is the format template (e.g., date format, number format)
	Format string
	// Formatter is the custom formatter name for export
	Formatter string
	// Parser is the custom parser name for import
	Parser string
}

Column represents metadata for a single column in tabular data.

type ExportError

type ExportError struct {
	// Row is the data row index where the error occurred (0-based)
	Row int
	// Column is the column name where the error occurred (optional)
	Column string
	// Field is the struct field name where the error occurred (optional)
	Field string
	// Err is the underlying error
	Err error
}

ExportError represents an error that occurred during data export.

func (ExportError) Error

func (e ExportError) Error() string

Error implements the error interface.

func (ExportError) Unwrap

func (e ExportError) Unwrap() error

Unwrap returns the underlying error.

type Exporter

type Exporter interface {
	// RegisterFormatter registers a custom formatter with the given name.
	RegisterFormatter(name string, formatter Formatter)
	// ExportToFile exports data to an Excel file.
	ExportToFile(data any, filename string) error
	// Export exports data to a bytes.Buffer.
	Export(data any) (*bytes.Buffer, error)
}

Exporter defines the interface for exporting tabular data. It provides methods to write and format tabular data to various destinations.

type Formatter

type Formatter interface {
	// Format converts a Go value to a cell string
	Format(value any) (string, error)
}

Formatter defines the interface for custom value formatters. Formatters convert Go values to cell strings during export.

type ImportError

type ImportError struct {
	// Row is the row number where the error occurred (1-based, includes header row)
	Row int
	// Column is the column name where the error occurred (optional)
	Column string
	// Field is the struct field name where the error occurred (optional)
	Field string
	// Err is the underlying error
	Err error
}

ImportError represents an error that occurred during data import.

func (ImportError) Error

func (e ImportError) Error() string

Error implements the error interface.

func (ImportError) Unwrap

func (e ImportError) Unwrap() error

Unwrap returns the underlying error.

type Importer

type Importer interface {
	// RegisterParser registers a custom parser with the given name.
	RegisterParser(name string, parser ValueParser)
	// ImportFromFile imports data from an Excel file.
	ImportFromFile(filename string) (any, []ImportError, error)
	// Import imports data from an io.Reader.
	Import(reader io.Reader) (any, []ImportError, error)
}

Importer defines the interface for importing tabular data. It provides methods to read and parse tabular data from various sources.

type Schema

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

Schema contains the pre-parsed tabular metadata for a struct type.

func NewSchema

func NewSchema(typ reflect.Type) *Schema

NewSchema creates a Schema instance by parsing struct fields with tabular tags from the given reflect.Type. Returns an empty Schema if the type is not a struct.

func NewSchemaFor

func NewSchemaFor[T any]() *Schema

NewSchemaFor creates a Schema instance by parsing struct fields with tabular tags from type T. This is a generic convenience function that calls NewSchema with reflect.TypeFor[T]().

func (*Schema) ColumnCount

func (s *Schema) ColumnCount() int

ColumnCount returns the number of columns.

func (*Schema) ColumnNames

func (s *Schema) ColumnNames() []string

ColumnNames returns all column names.

func (*Schema) Columns

func (s *Schema) Columns() []*Column

Columns returns all columns in the schema.

type ValueParser

type ValueParser interface {
	// Parse converts a cell string to a Go value
	Parse(cellValue string, targetType reflect.Type) (any, error)
}

ValueParser defines the interface for custom value parsers. Parsers convert cell strings to Go values during import.

Jump to

Keyboard shortcuts

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