Documentation ¶
Index ¶
- Variables
- func CallValidateMethod(v reflect.Value) error
- func FormatTableAsStrings(ctx context.Context, table any, formatter CellFormatter, options ...Option) (rows [][]string, err error)
- func FormatViewAsStrings(ctx context.Context, view View, formatter CellFormatter, options ...Option) (rows [][]string, err error)
- func FprintlnTable(w io.Writer, title string, table any) error
- func FprintlnView(w io.Writer, view View) error
- func HasOption(options []Option, option Option) bool
- func IndexedStructFieldAnyValues(structValue reflect.Value, numVals int, indices []int) []any
- func IndexedStructFieldReflectValues(structValue reflect.Value, numVals int, indices []int) []reflect.Value
- func IsNullLike(val reflect.Value) bool
- func IsStringRowEmpty(row []string) bool
- func MustStructFieldIndex(structPtr, fieldPtr any) int
- func ParseTime(str string) (t time.Time, format string, err error)
- func PrintlnTable(title string, table any) error
- func PrintlnView(view View) error
- func RemoveEmptyStringColumns(rows [][]string) (numCols int)
- func RemoveEmptyStringRows(rows [][]string) [][]string
- func SmartAssign(dst, src reflect.Value, dstScanner Scanner, srcFormatter Formatter) (err error)
- func SpaceGoCase(name string) string
- func SpacePascalCase(name string) string
- func SprintlnTable(w io.Writer, title string, table any) (string, error)
- func SprintlnView(w io.Writer, view View) (string, error)
- func StringColumnWidths(rows [][]string, maxCols int) []int
- func StructFieldAnyValues(structValue reflect.Value) []any
- func StructFieldIndex(structPtr, fieldPtr any) (int, error)
- func StructFieldReflectValues(structValue reflect.Value) []reflect.Value
- func StructFieldTypes(structType reflect.Type) (fields []reflect.StructField)
- func UseTitle(columnTitle string) func(fieldName string) (columnTitle string)
- func ViewToStructSlice[T any](view View, naming *StructFieldNaming, dstScanner Scanner, ...) ([]T, error)
- type AnyValuesView
- type CellFormatter
- type CellFormatterFunc
- type ExtraColsView
- type ExtraRowView
- type FilteredView
- type Formatter
- type FormatterFunc
- type HeaderView
- type LayoutFormatter
- type Option
- type Parser
- type PrintfCellFormatter
- type PrintfRawCellFormatter
- type RawCellString
- type RawStringIfTrue
- type ReflectCellView
- func AsReflectCellView(view View) ReflectCellView
- func DerefView(source View) ReflectCellView
- func ExtraColsAnyValueFuncView(left View, columns []string, anyValue func(row, col int) any) ReflectCellView
- func ExtraColsReflectValueFuncView(left View, columns []string, reflectValue func(row, col int) reflect.Value) ReflectCellView
- type ReflectTypeCellFormatter
- func (f *ReflectTypeCellFormatter) FormatCell(ctx context.Context, view View, row, col int) (str string, raw bool, err error)
- func (f *ReflectTypeCellFormatter) WithDefaultFormatter(fmt CellFormatter) *ReflectTypeCellFormatter
- func (f *ReflectTypeCellFormatter) WithInterfaceTypeFormatter(typ reflect.Type, fmt CellFormatter) *ReflectTypeCellFormatter
- func (f *ReflectTypeCellFormatter) WithKindFormatter(kind reflect.Kind, fmt CellFormatter) *ReflectTypeCellFormatter
- func (f *ReflectTypeCellFormatter) WithTypeFormatter(typ reflect.Type, fmt CellFormatter) *ReflectTypeCellFormatter
- type ReflectValuesView
- type Scanner
- type ScannerFunc
- type SingleReflectValueView
- type SprintFormatter
- type StringIfTrue
- type StringParser
- func (p *StringParser) ParseBool(str string) (bool, error)
- func (p *StringParser) ParseDuration(str string) (time.Duration, error)
- func (p *StringParser) ParseFloat(str string) (float64, error)
- func (p *StringParser) ParseInt(str string) (int64, error)
- func (p *StringParser) ParseTime(str string) (time.Time, error)
- func (p *StringParser) ParseUnt(str string) (uint64, error)
- type StringsView
- type StringsViewer
- type StructFieldNaming
- func (n *StructFieldNaming) ColumnStructFieldValue(structVal reflect.Value, column string) reflect.Value
- func (n *StructFieldNaming) Columns(strct any) []string
- func (n *StructFieldNaming) IsIgnored(column string) bool
- func (n *StructFieldNaming) NewView(title string, table any) (View, error)
- func (n *StructFieldNaming) String() string
- func (n *StructFieldNaming) StructFieldColumn(field reflect.StructField) string
- func (n *StructFieldNaming) WithIgnore(ignore string) *StructFieldNaming
- func (n *StructFieldNaming) WithTag(tag string) *StructFieldNaming
- type StructRowsView
- type StructRowsViewer
- func (v *StructRowsViewer) NewView(title string, table any) (View, error)
- func (v *StructRowsViewer) String() string
- func (v *StructRowsViewer) WithIgnore(ignore string) *StructRowsViewer
- func (v *StructRowsViewer) WithIgnoreField(structPtr, fieldPtr any) *StructRowsViewer
- func (v *StructRowsViewer) WithIgnoreFieldIndex(fieldIndex int) *StructRowsViewer
- func (v *StructRowsViewer) WithIgnoreFieldIndices(fieldIndices ...int) *StructRowsViewer
- func (v *StructRowsViewer) WithMapIndex(fieldIndex, columnIndex int) *StructRowsViewer
- func (v *StructRowsViewer) WithMapIndices(mapIndices map[int]int) *StructRowsViewer
- func (v *StructRowsViewer) WithTag(tag string) *StructRowsViewer
- type UnsupportedCellFormatter
- type UnsupportedFormatter
- type View
- type Viewer
Examples ¶
Constants ¶
This section is empty.
Variables ¶
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 ¶
CallValidateMethod calls the `Validate() error` or `Valid() bool` method on v.Interface() if available and v is not nil.
func FormatTableAsStrings ¶
func FormatViewAsStrings ¶
func IndexedStructFieldAnyValues ¶
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 ¶
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 ¶
IsStringRowEmpty returns true if all cells in the row are empty strings or if the length of the row is zero.
func MustStructFieldIndex ¶
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 PrintlnTable ¶
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 ¶
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 ¶
RemoveEmptyStringColumns removes all columns that only contain empty strings and returns the new number of columns.
func RemoveEmptyStringRows ¶
func SmartAssign ¶
SmartAssign assigns src to dst by converting src to dst type as smart as possible using the passed dstScanner and srcFormatter to convert types from and to strings.
Both dstScanner and srcFormatter can be nil.
func SpaceGoCase ¶
SpaceGoCase inserts spaces before upper case characters within Go case like names. The last upper case character in a sequence of upper case characters is interpreted as the start of a new word and a space is inserted before it. It also replaces underscore '_' characters with spaces. Usable for ReflectColumnTitles.UntaggedTitle
func SpacePascalCase ¶
SpacePascalCase inserts spaces before upper case characters within PascalCase like names. It also replaces underscore '_' characters with spaces. Usable for ReflectColumnTitles.UntaggedTitle
func StringColumnWidths ¶
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 ¶
StructFieldAnyValues returns the values of exported struct fields including the inlined fields of any anonymously embedded structs.
func StructFieldIndex ¶
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 ¶
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 ViewToStructSlice ¶
func ViewToStructSlice[T any](view View, naming *StructFieldNaming, dstScanner Scanner, srcFormatter Formatter, validate func(reflect.Value) error, requiredCols ...string) ([]T, error)
ViewToStructSlice converts a View to a slice of structs mapping the View's columns to the struct fields using the passed StructFieldNaming.
requiredCols must be present in the View and as named struct fields else an error is returned.
SmartAssign is used to assign the View's values to the struct fields using the passed dstScanner and srcFormatter to convert types from and to strings.
After a successful value assignment to a struct field, a non nil validate function is called with the struct field value as argument. If the validate function returns an error, it is returned immediately.
CallValidateMethod can be passed as validate function to call Validate() methods on the struct field values.
The arguments dstScanner, srcFormatter, and validate can be nil.
Types ¶
type AnyValuesView ¶
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) Cell ¶
func (view *AnyValuesView) Cell(row, col int) any
func (*AnyValuesView) Columns ¶
func (view *AnyValuesView) Columns() []string
func (*AnyValuesView) NumRows ¶
func (view *AnyValuesView) NumRows() int
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 ¶
type ExtraColsView ¶
type ExtraColsView []View
func (ExtraColsView) Cell ¶
func (e ExtraColsView) Cell(row, col int) any
func (ExtraColsView) Columns ¶
func (e ExtraColsView) Columns() []string
func (ExtraColsView) NumRows ¶
func (e ExtraColsView) NumRows() int
func (ExtraColsView) Title ¶
func (e ExtraColsView) Title() string
type ExtraRowView ¶
type ExtraRowView []View
func (ExtraRowView) Cell ¶
func (e ExtraRowView) Cell(row, col int) any
func (ExtraRowView) Columns ¶
func (e ExtraRowView) Columns() []string
func (ExtraRowView) NumRows ¶
func (e ExtraRowView) NumRows() int
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) Cell ¶
func (view *FilteredView) Cell(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) Title ¶
func (view *FilteredView) Title() string
type Formatter ¶
func FormatterFromCellFormatter ¶
func FormatterFromCellFormatter(f CellFormatter) Formatter
type FormatterFunc ¶
type HeaderView ¶
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. Whitespace will be trimmed from the column names.
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) Cell ¶
func (view *HeaderView) Cell(row, col int) any
func (*HeaderView) Columns ¶
func (view *HeaderView) Columns() []string
func (*HeaderView) NumRows ¶
func (view *HeaderView) NumRows() int
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 ¶
type PrintfCellFormatter ¶
type PrintfCellFormatter string
PrintfCellFormatter implements CellFormatter by calling fmt.Sprintf with this type's string value as format.
func (PrintfCellFormatter) FormatCell ¶
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 ¶
type RawCellString ¶
type RawCellString string
RawCellString implements CellFormatter by returning the underlying string as raw value.
func (RawCellString) FormatCell ¶
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 ¶
type ReflectCellView ¶
type ReflectCellView interface { View // ReflectCell 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. ReflectCell(row, col int) reflect.Value }
ReflectCellView expands the View interface with a method to return the reflect.Value of the cell at the given row and column.
func AsReflectCellView ¶
func AsReflectCellView(view View) ReflectCellView
AsReflectCellView returns the passed view as a ReflectCellView if it implements the interface, otherwise it wraps the view with a helper type to create a ReflectCellView.
func DerefView ¶
func DerefView(source View) ReflectCellView
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) ReflectCellView
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 (f *ReflectTypeCellFormatter) WithKindFormatter(kind reflect.Kind, fmt CellFormatter) *ReflectTypeCellFormatter
func (*ReflectTypeCellFormatter) WithTypeFormatter ¶
func (f *ReflectTypeCellFormatter) WithTypeFormatter(typ reflect.Type, fmt CellFormatter) *ReflectTypeCellFormatter
type ReflectValuesView ¶
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) Cell ¶
func (view *ReflectValuesView) Cell(row, col int) any
func (*ReflectValuesView) Columns ¶
func (view *ReflectValuesView) Columns() []string
func (*ReflectValuesView) NumRows ¶
func (view *ReflectValuesView) NumRows() int
func (*ReflectValuesView) ReflectCell ¶
func (view *ReflectValuesView) ReflectCell(row, col int) reflect.Value
func (*ReflectValuesView) Title ¶
func (view *ReflectValuesView) Title() string
type ScannerFunc ¶
func (ScannerFunc) ScanString ¶
type SingleReflectValueView ¶
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) Cell ¶
func (view *SingleReflectValueView) Cell(row, col int) any
func (*SingleReflectValueView) Columns ¶
func (view *SingleReflectValueView) Columns() []string
func (*SingleReflectValueView) NumRows ¶
func (view *SingleReflectValueView) NumRows() int
func (*SingleReflectValueView) ReflectCell ¶
func (view *SingleReflectValueView) ReflectCell(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.
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 ¶
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) ParseDuration ¶
func (p *StringParser) ParseDuration(str string) (time.Duration, error)
func (*StringParser) ParseFloat ¶
func (p *StringParser) ParseFloat(str string) (float64, error)
type StringsView ¶
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. Whitespace will be trimmed from the column names.
func (*StringsView) Cell ¶
func (view *StringsView) Cell(row, col int) any
func (*StringsView) Columns ¶
func (view *StringsView) Columns() []string
func (*StringsView) NumRows ¶
func (view *StringsView) NumRows() int
func (*StringsView) Title ¶
func (view *StringsView) Title() string
type StringsViewer ¶
type StringsViewer struct {
Cols []string
}
StringsViewer creates views for tables of type [][]string using Cols as optional column names.
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(structVal reflect.Value, column string) reflect.Value
ColumnStructFieldValue returns the reflect.Value of the struct field that is mapped to the column title.
Valid to call with nil receiver.
func (*StructFieldNaming) Columns ¶
func (n *StructFieldNaming) Columns(strct any) []string
Columns returns the column titles for a struct or a pointer to a struct.
It panics for non struct or struct pointer types.
Valid to call with nil receiver.
func (*StructFieldNaming) IsIgnored ¶
func (n *StructFieldNaming) IsIgnored(column string) bool
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.
Valid to call with nil receiver.
func (*StructFieldNaming) StructFieldColumn ¶
func (n *StructFieldNaming) StructFieldColumn(field reflect.StructField) string
StructFieldColumn returns the column title for a struct field.
Valid to call with nil receiver.
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) Cell ¶
func (view *StructRowsView) Cell(row, col int) any
func (*StructRowsView) Columns ¶
func (view *StructRowsView) Columns() []string
func (*StructRowsView) NumRows ¶
func (view *StructRowsView) NumRows() int
func (*StructRowsView) ReflectCell ¶
func (view *StructRowsView) ReflectCell(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.
type UnsupportedFormatter ¶
type UnsupportedFormatter struct{}
UnsupportedFormatter is a Formatter that always returns errors.ErrUnsupported.
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 // Cell 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. Cell(row, col int) any }
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 NewStructRowsView ¶
func SingleCellView ¶
func SingleColView ¶
func ViewWithTitle ¶
Source Files ¶
- anyvaluesview.go
- assign.go
- cellformatter.go
- config.go
- derefview.go
- extracolsfuncview.go
- extracolsview.go
- extrarowsview.go
- filteredview.go
- formatstrings.go
- formatter.go
- options.go
- parser.go
- reflecttypecellformatter.go
- reflectvaluesview.go
- scanner.go
- singlecolview.go
- stringsview.go
- stringsviewer.go
- structfieldnaming.go
- structrowsview.go
- structrowsviewer.go
- utils.go
- view.go
- viewer.go
- viewtostructslice.go
- viewwithtitle.go