retable

package module
v0.0.0-...-508fc3c Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2024 License: MIT Imports: 14 Imported by: 1

README

go-retable

Reflection based tables

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// DefaultStructFieldNaming provides the default StructFieldNaming
	// using "col" as title tag, ignores "-" titled fields,
	// and uses SpacePascalCase for untagged fields.
	// Implements the Viewer interface.
	DefaultStructFieldNaming = StructFieldNaming{
		Tag:      "col",
		Ignore:   "-",
		Untagged: SpacePascalCase,
	}

	// DefaultStructFieldNamingIgnoreUntagged provides the default StructFieldNaming
	// using "col" as title tag, ignores "-" titled as well as untitled fields.
	// Implements the Viewer interface.
	DefaultStructFieldNamingIgnoreUntagged = StructFieldNaming{
		Tag:      "col",
		Ignore:   "-",
		Untagged: UseTitle("-"),
	}

	// SelectViewer selects the best matching Viewer implementation
	// for the passed table type.
	// By default it returns a StringsViewer for a [][]string table
	// and the DefaultStructRowsViewer for all other cases.
	SelectViewer = func(table any) (Viewer, error) {
		if _, ok := table.([][]string); ok {
			return new(StringsViewer), nil
		}
		return &DefaultStructFieldNaming, nil
	}
)

Functions

func CallValidateMethod

func CallValidateMethod(v reflect.Value) error

CallValidateMethod calls the Validate() method on v.Interface() and returns its error if v is not nil and if it implements interface{ Validate() error }

func FormatTableAsStrings

func FormatTableAsStrings(ctx context.Context, table any, formatter CellFormatter, options ...Option) (rows [][]string, err error)

func FormatViewAsStrings

func FormatViewAsStrings(ctx context.Context, view View, formatter CellFormatter, options ...Option) (rows [][]string, err error)

func FprintlnTable

func FprintlnTable(w io.Writer, title string, table any) error

func FprintlnView

func FprintlnView(w io.Writer, view View) error

func HasOption

func HasOption(options []Option, option Option) bool

func IndexedStructFieldAnyValues

func IndexedStructFieldAnyValues(structValue reflect.Value, numVals int, indices []int) []any

IndexedStructFieldAnyValues returns the values of exported struct fields including the inlined fields of any anonymously embedded structs.

func IndexedStructFieldReflectValues

func IndexedStructFieldReflectValues(structValue reflect.Value, numVals int, indices []int) []reflect.Value

IndexedStructFieldReflectValues returns the reflect.Value of exported struct fields including the inlined fields of any anonymously embedded structs.

func IsNullLike

func IsNullLike(val reflect.Value) bool

IsNullLike return true if passed reflect.Value fulfills any of the following conditions:

  • is not valid
  • nil (of a type that can be nil),
  • is of type struct{},
  • implements the IsNull() bool method which returns true,
  • implements the IsZero() bool method which returns true,
  • implements the driver.Valuer interface which returns nil, nil.

func IsStringRowEmpty

func IsStringRowEmpty(row []string) bool

IsStringRowEmpty returns true if all cells in the row are empty strings or if the length of the row is zero.

func MustStructFieldIndex

func MustStructFieldIndex(structPtr, fieldPtr any) int

MustStructFieldIndex returns the index of of the struct field pointed to by fieldPtr within the struct pointed to by structPtr. The returned index counts exported struct fields including the inlined fields of any anonymously embedded structs.

func ParseTime

func ParseTime(str string) (t time.Time, format string, err error)

func PrintlnTable

func PrintlnTable(title string, table any) error
Example
type Row struct {
	A string
	B int
	C *time.Time
}
t := time.Date(2024, 1, 2, 3, 4, 5, 0, time.UTC)

PrintlnTable("ExamplePrintlnTable", []Row{
	{A: "1", B: -1, C: &t},
	{A: "", B: 2222222222, C: nil},
	{A: "Last row", B: 0, C: nil},
})
Output:

ExamplePrintlnTable:
| A        | B          | C                             |
| 1        | -1         | 2024-01-02 03:04:05 +0000 UTC |
|          | 2222222222 |                               |
| Last row | 0          |                               |

func PrintlnView

func PrintlnView(view View) error
Example
PrintlnView(&StringsView{
	Tit:  "ExamplePrintlnView",
	Cols: []string{"A", "B", "C"},
	Rows: [][]string{
		{"1", "2222222222", "3"},
		{"", "", "3333"},
		{"Last row"},
	},
})
Output:

ExamplePrintlnView:
| A        | B          | C    |
| 1        | 2222222222 | 3    |
|          |            | 3333 |
| Last row |            |      |

func RemoveEmptyStringColumns

func RemoveEmptyStringColumns(rows [][]string) (numCols int)

RemoveEmptyStringColumns removes all columns that only contain empty strings and returns the new number of columns.

func RemoveEmptyStringRows

func RemoveEmptyStringRows(rows [][]string) [][]string

func SmartAssign

func SmartAssign(dst, src reflect.Value, dstScanner Scanner, srcFormatter Formatter) (err error)

SmartAssign assigns src to dst by converting src to dst type as smart as possible using the passed dstScanner and srcFormatter. dstScanner can be nil srcFormatter can be nil

func SpacePascalCase

func SpacePascalCase(name string) string

SpacePascalCase inserts spaces before upper case characters within PascalCase like names. It also replaces underscore '_' characters with spaces. Usable for ReflectColumnTitles.UntaggedTitle

func SprintlnTable

func SprintlnTable(w io.Writer, title string, table any) (string, error)

func SprintlnView

func SprintlnView(w io.Writer, view View) (string, error)

func StringColumnWidths

func StringColumnWidths(rows [][]string, maxCols int) []int

StringColumnWidths returns the column widths of the passed table as count of UTF-8 runes. maxCols limits the number of columns to consider, if maxCols is -1, then all columns are considered.

func StructFieldAnyValues

func StructFieldAnyValues(structValue reflect.Value) []any

StructFieldAnyValues returns the values of exported struct fields including the inlined fields of any anonymously embedded structs.

func StructFieldIndex

func StructFieldIndex(structPtr, fieldPtr any) (int, error)

StructFieldIndex returns the index of of the struct field pointed to by fieldPtr within the struct pointed to by structPtr. The returned index counts exported struct fields including the inlined fields of any anonymously embedded structs.

func StructFieldReflectValues

func StructFieldReflectValues(structValue reflect.Value) []reflect.Value

StructFieldReflectValues returns the reflect.Value of exported struct fields including the inlined fields of any anonymously embedded structs.

func StructFieldTypes

func StructFieldTypes(structType reflect.Type) (fields []reflect.StructField)

StructFieldTypes returns the exported fields of a struct type including the inlined fields of any anonymously embedded structs.

func UseTitle

func UseTitle(columnTitle string) func(fieldName string) (columnTitle string)

UseTitle returns a function that always returns the passed columnTitle.

func ViewToStructSlice

func ViewToStructSlice[T any](view View, naming *StructFieldNaming, dstScanner Scanner, srcFormatter Formatter, validate func(reflect.Value) error) ([]T, error)

ViewToStructSlice converts a View to a slice of structs mapping the View's columns to the struct fields using the passed StructFieldNaming.

SmartAssign is used to assign the View's values to the struct fields.

After a successful value assignment to a struct field, validate is called with the struct field value and its error is returned if not nil.

The arguments dstScanner, srcFormatter, and validate can be nil.

Types

type AnyValuesView

type AnyValuesView struct {
	Tit  string
	Cols []string
	Rows [][]any
}

AnyValuesView is a View implementation that holds its rows as slices of value with any type.

func NewAnyValuesViewFrom

func NewAnyValuesViewFrom(source View) *AnyValuesView

NewAnyValuesViewFrom reads and caches all cells from the source View as ValuesView.

func (*AnyValuesView) AnyValue

func (view *AnyValuesView) AnyValue(row, col int) any

func (*AnyValuesView) Columns

func (view *AnyValuesView) Columns() []string

func (*AnyValuesView) NumRows

func (view *AnyValuesView) NumRows() int

func (*AnyValuesView) ReflectValue

func (view *AnyValuesView) ReflectValue(row, col int) reflect.Value

func (*AnyValuesView) Title

func (view *AnyValuesView) Title() string

type CellFormatter

type CellFormatter interface {
	// FormatCell formats the view cell at a row/col position as string
	// or returns a wrapped errors.ErrUnsupported error if
	// it doesn't support formatting the value of the cell.
	// The raw result indicates if the returned string
	// is in the raw format of the table format and can be
	// used as is or if it has to be sanitized in some way.
	FormatCell(ctx context.Context, view View, row, col int) (str string, raw bool, err error)
}

CellFormatter is an interface for formatting view cells as strings.

func CellFormatterFromFormatter

func CellFormatterFromFormatter(f Formatter, rawResult bool) CellFormatter

func SprintCellFormatter

func SprintCellFormatter(rawResult bool) CellFormatter

SprintCellFormatter returns a CellFormatter that formats a cell's value using fmt.Sprint and returns the result together with the rawResult argument.

func TryFormattersOrSprint

func TryFormattersOrSprint(formatters ...CellFormatter) CellFormatter

SprintRawCellFormatter returns a CellFormatter that tries the passed formatters in order until they return no error or a non errors.ErrUnsupported error. If all formatters return errors.ErrUnsupported then fmt.Sprint is used as fallback or an empty string returned for nil. In case of the fallback the raw bool is always false. nil formatters are ignored.

type CellFormatterFunc

type CellFormatterFunc func(ctx context.Context, view View, row, col int) (str string, raw bool, err error)

CellFormatterFunc implements CellFormatter for a function.

func ReflectCellFormatterFunc

func ReflectCellFormatterFunc(function any, rawResult bool) (formatter CellFormatterFunc, valType reflect.Type, err error)

ReflectCellFormatterFunc uses reflection to convert the passed function into a CellFormatterFunc. The function can have zero to two arguments and one or two results. In case of two arguments the first argument must be of type context.Context. The first result must be of type string and the optional second result of type error. The returned CellFormatterFunc will return the passed rawResult argument as raw result value.

func (CellFormatterFunc) FormatCell

func (f CellFormatterFunc) FormatCell(ctx context.Context, view View, row, col int) (str string, raw bool, err error)

type ExtraColsView

type ExtraColsView []View

func (ExtraColsView) AnyValue

func (e ExtraColsView) AnyValue(row, col int) any

func (ExtraColsView) Columns

func (e ExtraColsView) Columns() []string

func (ExtraColsView) NumRows

func (e ExtraColsView) NumRows() int

func (ExtraColsView) ReflectValue

func (e ExtraColsView) ReflectValue(row, col int) reflect.Value

func (ExtraColsView) Title

func (e ExtraColsView) Title() string

type ExtraRowView

type ExtraRowView []View

func (ExtraRowView) AnyValue

func (e ExtraRowView) AnyValue(row, col int) any

func (ExtraRowView) Columns

func (e ExtraRowView) Columns() []string

func (ExtraRowView) NumRows

func (e ExtraRowView) NumRows() int

func (ExtraRowView) ReflectValue

func (e ExtraRowView) ReflectValue(row, col int) reflect.Value

func (ExtraRowView) Title

func (e ExtraRowView) Title() string

type FilteredView

type FilteredView struct {
	Source View
	// Offset index of the first row from Source, must be positive.
	RowOffset int
	// Limits the number of rows, only used if > 0.
	RowLimit int
	// If not nil then the view has as many
	// columns as ColumnMapping has elements and
	// every element is a column index into the Source view.
	// If nil then the view has as many columns as the Source view.
	ColumnMapping []int
}

func (*FilteredView) AnyValue

func (view *FilteredView) AnyValue(row, col int) any

func (*FilteredView) Columns

func (view *FilteredView) Columns() []string

func (*FilteredView) NumCols

func (view *FilteredView) NumCols() int

func (*FilteredView) NumRows

func (view *FilteredView) NumRows() int

func (*FilteredView) ReflectValue

func (view *FilteredView) ReflectValue(row, col int) reflect.Value

func (*FilteredView) Title

func (view *FilteredView) Title() string

type Formatter

type Formatter interface {
	Format(reflect.Value) (string, error)
}

func FormatterFromCellFormatter

func FormatterFromCellFormatter(f CellFormatter) Formatter

type FormatterFunc

type FormatterFunc func(reflect.Value) (string, error)

func (FormatterFunc) Format

func (f FormatterFunc) Format(v reflect.Value) (string, error)

type HeaderView

type HeaderView struct {
	Tit  string
	Cols []string
}

HeaderView is a View that uses the Cols field as column names and also as first row.

func NewHeaderView

func NewHeaderView(cols ...string) *HeaderView

NewHeaderView returns a View using the passed cols as column names and also as first row.

func NewHeaderViewFrom

func NewHeaderViewFrom(source View) *HeaderView

NewHeaderViewFrom returns a View using the column names from the source View also as first row.

func (*HeaderView) AnyValue

func (view *HeaderView) AnyValue(row, col int) any

func (*HeaderView) Columns

func (view *HeaderView) Columns() []string

func (*HeaderView) NumRows

func (view *HeaderView) NumRows() int

func (*HeaderView) ReflectValue

func (view *HeaderView) ReflectValue(row, col int) reflect.Value

func (*HeaderView) Title

func (view *HeaderView) Title() string

type LayoutFormatter

type LayoutFormatter string

LayoutFormatter formats any type that implements interface{ Format(string) string } like time.Time by calling the Format method with the string value of LayoutFormatter.

func (LayoutFormatter) FormatCell

func (f LayoutFormatter) FormatCell(ctx context.Context, view View, row, col int) (str string, raw bool, err error)

type Option

type Option int
const (
	OptionAddHeaderRow Option = 1 << iota
)

func (Option) Has

func (o Option) Has(option Option) bool

func (Option) String

func (o Option) String() string

type Parser

type Parser interface {
	ParseInt(string) (int64, error)
	ParseUnt(string) (uint64, error)
	ParseFloat(string) (float64, error)
	ParseBool(string) (bool, error)
	ParseTime(string) (time.Time, error)
	ParseDuration(string) (time.Duration, error)
}

type PrintfCellFormatter

type PrintfCellFormatter string

PrintfCellFormatter implements CellFormatter by calling fmt.Sprintf with this type's string value as format.

func (PrintfCellFormatter) FormatCell

func (format PrintfCellFormatter) FormatCell(ctx context.Context, view View, row, col int) (str string, raw bool, err error)

type PrintfRawCellFormatter

type PrintfRawCellFormatter string

PrintfRawCellFormatter implements CellFormatter by calling fmt.Sprintf with this type's string value as format. The result will be indicated to be a raw value.

func (PrintfRawCellFormatter) FormatCell

func (format PrintfRawCellFormatter) FormatCell(ctx context.Context, view View, row, col int) (str string, raw bool, err error)

type RawCellString

type RawCellString string

RawCellString implements CellFormatter by returning the underlying string as raw value.

func (RawCellString) FormatCell

func (rawStr RawCellString) FormatCell(ctx context.Context, view View, row, col int) (str string, raw bool, err error)

type RawStringIfTrue

type RawStringIfTrue string

RawStringIfTrue formats bool cells by returning the underlying string as raw value for true and an empty string as raw value for false.

func (RawStringIfTrue) FormatCell

func (f RawStringIfTrue) FormatCell(ctx context.Context, view View, row, col int) (str string, raw bool, err error)

type ReflectTypeCellFormatter

type ReflectTypeCellFormatter struct {
	Types          map[reflect.Type]CellFormatter
	InterfaceTypes map[reflect.Type]CellFormatter
	Kinds          map[reflect.Kind]CellFormatter
	Default        CellFormatter
}

func NewReflectTypeCellFormatter

func NewReflectTypeCellFormatter() *ReflectTypeCellFormatter

func (*ReflectTypeCellFormatter) FormatCell

func (f *ReflectTypeCellFormatter) FormatCell(ctx context.Context, view View, row, col int) (str string, raw bool, err error)

FormatCell implements CellFormatter

func (*ReflectTypeCellFormatter) WithDefaultFormatter

func (f *ReflectTypeCellFormatter) WithDefaultFormatter(fmt CellFormatter) *ReflectTypeCellFormatter

func (*ReflectTypeCellFormatter) WithInterfaceTypeFormatter

func (f *ReflectTypeCellFormatter) WithInterfaceTypeFormatter(typ reflect.Type, fmt CellFormatter) *ReflectTypeCellFormatter

func (*ReflectTypeCellFormatter) WithKindFormatter

func (*ReflectTypeCellFormatter) WithTypeFormatter

type ReflectValuesView

type ReflectValuesView struct {
	Tit  string
	Cols []string
	Rows [][]reflect.Value
}

ReflectValuesView is a View implementation that holds its rows as slices of reflect.Value.

func NewReflectValuesViewFrom

func NewReflectValuesViewFrom(source View) (*ReflectValuesView, error)

NewReflectValuesViewFrom reads and caches all cells as reflect.Value from the source View as ReflectValuesView.

func (*ReflectValuesView) AnyValue

func (view *ReflectValuesView) AnyValue(row, col int) any

func (*ReflectValuesView) Columns

func (view *ReflectValuesView) Columns() []string

func (*ReflectValuesView) NumRows

func (view *ReflectValuesView) NumRows() int

func (*ReflectValuesView) ReflectValue

func (view *ReflectValuesView) ReflectValue(row, col int) reflect.Value

func (*ReflectValuesView) Title

func (view *ReflectValuesView) Title() string

type Scanner

type Scanner interface {
	ScanString(dest reflect.Value, str string, parser Parser) error
}

type ScannerFunc

type ScannerFunc func(dest reflect.Value, str string, parser Parser) error

func (ScannerFunc) ScanString

func (f ScannerFunc) ScanString(dest reflect.Value, str string, parser Parser) error

type SingleReflectValueView

type SingleReflectValueView struct {
	Tit string
	Col string
	Val reflect.Value
}

SingleReflectValueView is a View implementation that holds its rows as slices of reflect.Value.

func NewSingleReflectValueView

func NewSingleReflectValueView(source View, row, col int) *SingleReflectValueView

NewSingleReflectValueView reads the cell at row/col from the source View and wraps it as SingleReflectValueView.

func (*SingleReflectValueView) AnyValue

func (view *SingleReflectValueView) AnyValue(row, col int) any

func (*SingleReflectValueView) Columns

func (view *SingleReflectValueView) Columns() []string

func (*SingleReflectValueView) NumRows

func (view *SingleReflectValueView) NumRows() int

func (*SingleReflectValueView) ReflectValue

func (view *SingleReflectValueView) ReflectValue(row, col int) reflect.Value

func (*SingleReflectValueView) Title

func (view *SingleReflectValueView) Title() string

type SprintFormatter

type SprintFormatter struct{}

SprintFormatter is a Formatter that uses fmt.Sprint to format any value.

func (SprintFormatter) Format

func (SprintFormatter) Format(v reflect.Value) (string, error)

type StringIfTrue

type StringIfTrue string

StringIfTrue formats bool cells by returning the underlying string as non-raw value for true and an empty string as non-raw value for false.

func (StringIfTrue) FormatCell

func (f StringIfTrue) FormatCell(ctx context.Context, view View, row, col int) (str string, raw bool, err error)

type StringParser

type StringParser struct {
	TrueStrings  []string `json:"trueStrings"`
	FalseStrings []string `json:"falseStrings"`
	NilStrings   []string `json:"nilStrings"`
	TimeFormats  []string `json:"timeFormats"`
}

func NewStringParser

func NewStringParser() *StringParser

func (*StringParser) ParseBool

func (p *StringParser) ParseBool(str string) (bool, error)

func (*StringParser) ParseDuration

func (p *StringParser) ParseDuration(str string) (time.Duration, error)

func (*StringParser) ParseFloat

func (p *StringParser) ParseFloat(str string) (float64, error)

func (*StringParser) ParseInt

func (p *StringParser) ParseInt(str string) (int64, error)

func (*StringParser) ParseTime

func (p *StringParser) ParseTime(str string) (time.Time, error)

func (*StringParser) ParseUnt

func (p *StringParser) ParseUnt(str string) (uint64, error)

type StringsView

type StringsView struct {
	Tit  string
	Cols []string
	Rows [][]string
}

StringsView is a View that uses strings as values. Cols defines the column names and number of columns.

StringsView is a sparse table int the sense that a row within Rows can have fewer slice elements than Cols in which case empty strings are used as value.

func NewStringsView

func NewStringsView(title string, rows [][]string, cols ...string) *StringsView

NewStringsView returns a StringsView using either the optional cols arguments as column names or the first row if no cols have been passed.

func (*StringsView) AnyValue

func (view *StringsView) AnyValue(row, col int) any

func (*StringsView) Columns

func (view *StringsView) Columns() []string

func (*StringsView) NumRows

func (view *StringsView) NumRows() int

func (*StringsView) ReflectValue

func (view *StringsView) ReflectValue(row, col int) reflect.Value

func (*StringsView) Title

func (view *StringsView) Title() string

type StringsViewer

type StringsViewer struct {
	Cols []string
}

func (StringsViewer) NewView

func (v StringsViewer) NewView(title string, table any) (View, error)

type StructFieldNaming

type StructFieldNaming struct {
	// Tag is the struct field tag to be used as column title.
	// If Tag is empty, then every struct field will be treated as untagged.
	Tag string
	// Ignore will result in a column index of -1
	// for columns with that title
	Ignore string
	// Untagged will be called with the struct field name to
	// return a title in case the struct field has no tag named Tag.
	// If Untagged is nil, then the struct field name will be used.
	Untagged func(fieldName string) (column string)
}

StructFieldNaming defines how struct fields are mapped to column titles as used by View.

nil is a valid value for *StructFieldNaming and is equal to the zero value which will use all exported struct fields with their field name as column title.

StructFieldNaming implements the Viewer interface.

func (*StructFieldNaming) ColumnStructFieldValue

func (n *StructFieldNaming) ColumnStructFieldValue(strct reflect.Value, column string) reflect.Value

func (*StructFieldNaming) NewView

func (n *StructFieldNaming) NewView(title string, table any) (View, error)

NewView returns a View for a table made up of a slice or array of structs. NewView implements the Viewer interface for StructFieldNaming.

func (*StructFieldNaming) String

func (n *StructFieldNaming) String() string

String implements the fmt.Stringer interface for StructFieldNaming.

func (*StructFieldNaming) StructFieldColumn

func (n *StructFieldNaming) StructFieldColumn(structField reflect.StructField) string

StructFieldColumn returns the column title for a struct field.

func (*StructFieldNaming) WithIgnore

func (n *StructFieldNaming) WithIgnore(ignore string) *StructFieldNaming

func (*StructFieldNaming) WithTag

func (n *StructFieldNaming) WithTag(tag string) *StructFieldNaming

type StructRowsView

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

func (*StructRowsView) AnyValue

func (view *StructRowsView) AnyValue(row, col int) any

func (*StructRowsView) Columns

func (view *StructRowsView) Columns() []string

func (*StructRowsView) NumRows

func (view *StructRowsView) NumRows() int

func (*StructRowsView) ReflectValue

func (view *StructRowsView) ReflectValue(row, col int) reflect.Value

func (*StructRowsView) Title

func (view *StructRowsView) Title() string

type StructRowsViewer

type StructRowsViewer struct {
	StructFieldNaming

	// MapIndices is a map from the index of a field in struct
	// to the column index returned by the function StructFieldTypes.
	// If MapIndices is nil, then no mapping will be performed.
	// Mapping a struct field index to -1 will ignore this field
	// and not create a column for it..
	MapIndices map[int]int
}

StructRowsViewer implements Viewer for tables represented by a slice or array of struct rows.

func DefaultStructRowsViewer

func DefaultStructRowsViewer() *StructRowsViewer

DefaultStructRowsViewer returns a StructRowsViewer that uses DefaultStructFieldNaming and no MapIndices.

func NoTagsStructRowsViewer

func NoTagsStructRowsViewer() *StructRowsViewer

NoTagsStructRowsViewer returns a StructRowsViewer that uses the struct field names as column titles without considering struct field tags.

func (*StructRowsViewer) NewView

func (v *StructRowsViewer) NewView(title string, table any) (View, error)

NewView returns a View for a table made up of a slice or array of structs. NewView implements the Viewer interface for StructRowsViewer.

func (*StructRowsViewer) String

func (v *StructRowsViewer) String() string

String implements the fmt.Stringer interface for StructRowsViewer.

func (*StructRowsViewer) WithIgnore

func (v *StructRowsViewer) WithIgnore(ignore string) *StructRowsViewer

func (*StructRowsViewer) WithIgnoreField

func (v *StructRowsViewer) WithIgnoreField(structPtr, fieldPtr any) *StructRowsViewer

func (*StructRowsViewer) WithIgnoreFieldIndex

func (v *StructRowsViewer) WithIgnoreFieldIndex(fieldIndex int) *StructRowsViewer

func (*StructRowsViewer) WithIgnoreFieldIndices

func (v *StructRowsViewer) WithIgnoreFieldIndices(fieldIndices ...int) *StructRowsViewer

func (*StructRowsViewer) WithMapIndex

func (v *StructRowsViewer) WithMapIndex(fieldIndex, columnIndex int) *StructRowsViewer

func (*StructRowsViewer) WithMapIndices

func (v *StructRowsViewer) WithMapIndices(mapIndices map[int]int) *StructRowsViewer

func (*StructRowsViewer) WithTag

func (v *StructRowsViewer) WithTag(tag string) *StructRowsViewer

type UnsupportedCellFormatter

type UnsupportedCellFormatter struct{}

UnsupportedCellFormatter is a CellFormatter that always returns errors.ErrUnsupported.

func (UnsupportedCellFormatter) Format

func (UnsupportedCellFormatter) Format(ctx context.Context, view View, row, col int) (str string, raw bool, err error)

type UnsupportedFormatter

type UnsupportedFormatter struct{}

UnsupportedFormatter is a Formatter that always returns errors.ErrUnsupported.

func (UnsupportedFormatter) Format

type View

type View interface {
	// Title of the View
	Title() string
	// Columns returns the column names
	// which can be empty strings.
	Columns() []string
	// Numrows returns the number of rows
	NumRows() int
	// AnyValue returns the empty interface value of the cell at the given row and column.
	// If row and col are out of bounds then nil is returned.
	AnyValue(row, col int) any
	// Value returns the reflect.Value of the cell at the given row and column.
	// If row and col are out of bounds then the zero value is returned.
	ReflectValue(row, col int) reflect.Value
}

View is an interface implemented by types with table like data to enable reading (viewing) the data in a uniform table like way.

The design of this package assumes that the contents of a View are first read into memory and then wrapped as View, so the View methods don't need a context parameter and error result.

func DerefView

func DerefView(source View) View

DerefView returns a View that dereferences every value returned by the source View by calling reflect.Value.Elem() wich might panic if the contained value does not support calling the Elem method.

func ExtraColsAnyValueFuncView

func ExtraColsAnyValueFuncView(left View, columns []string, anyValue func(row, col int) any) View

func ExtraColsReflectValueFuncView

func ExtraColsReflectValueFuncView(left View, columns []string, reflectValue func(row, col int) reflect.Value) View

func NewStructRowsView

func NewStructRowsView(title string, columns []string, indices []int, rows reflect.Value) View

func SingleCellView

func SingleCellView[T any](title, column string, value T) View

func SingleColView

func SingleColView[T any](column string, rows []T) View

func ViewWithTitle

func ViewWithTitle(source View, title string) View

type Viewer

type Viewer interface {
	// NewView creates a View with the passed title
	// for the passed table.
	NewView(title string, table any) (View, error)
}

Viewer implementations have a NewView method to create a View for a table.

Directories

Path Synopsis
exceltable module

Jump to

Keyboard shortcuts

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