tabular

package
v0.19.0 Latest Latest
Warning

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

Go to latest
Published: Jan 27, 2026 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

This section is empty.

Types

type Column

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

Column represents metadata for a single column in tabular data.

type ExportError

type ExportError struct {
	Row    int
	Column string
	Field  string
	Err    error
}

ExportError represents an error that occurred during data export. Row is 0-based data row index.

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.

func NewDefaultFormatter

func NewDefaultFormatter(format string) Formatter

NewDefaultFormatter creates a default formatter with optional format template.

type ImportError

type ImportError struct {
	Row    int
	Column string
	Field  string
	Err    error
}

ImportError represents an error that occurred during data import. Row is 1-based and includes the header row.

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.

func NewSchemaFor

func NewSchemaFor[T any]() *Schema

NewSchemaFor creates a Schema instance from type 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.

func NewDefaultParser

func NewDefaultParser(format string) ValueParser

NewDefaultParser creates a default parser with optional format template.

Jump to

Keyboard shortcuts

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