structtable

package module
v0.0.0-...-ebdd804 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2024 License: MIT Imports: 11 Imported by: 0

README

go-structtable

Read and write data-table formats as slices of Go structs

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultReflectColumnTitles = &ReflectColumnTitles{
	Tag:                "col",
	IgnoreTitle:        "-",
	UntaggedFieldTitle: SpacePascalCase,
}

DefaultReflectColumnTitles provides the default ReflectColumnTitles using "col" as Tag and the SpacePascalCase function for UntaggedFieldTitle. Implements ColumnMapper.

Functions

func Read

func Read(reader Reader, structSlicePtr interface{}, numHeaderRows int) (headerRows [][]string, err error)

func Render

func Render(renderer Renderer, structSlice interface{}, renderTitleRow bool, columnMapper ColumnMapper) error

func RenderBytes

func RenderBytes(renderer Renderer, structSlice interface{}, renderTitleRow bool, columnMapper ColumnMapper) ([]byte, error)

func RenderFile

func RenderFile(file fs.File, renderer Renderer, structSlice interface{}, renderTitleRow bool, columnMapper ColumnMapper) error

func RenderTo

func RenderTo(writer io.Writer, renderer Renderer, structSlice interface{}, renderTitleRow bool, columnMapper ColumnMapper) error

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.UntaggedFieldTitle

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 StructFieldValues

func StructFieldValues(structValue reflect.Value) (values []reflect.Value)

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

Types

type ColumnMapper

type ColumnMapper interface {
	// ColumnTitlesAndRowReflector returns the column titles and indices for structFields.
	// The length of the titles and indices slices must be identical to the length of structFields.
	// The indices start at zero, the special index -1 filters removes the column
	// for the corresponding struct field.
	ColumnTitlesAndRowReflector(structType reflect.Type) (titles []string, rowReflector RowReflector)
}

ColumnMapper is used to map struct type fields to column names

func NoColumnTitles

func NoColumnTitles() ColumnMapper

NoColumnTitles returns a ColumnMapper that returns nil as column titles and the StructFieldValues function of this package as RowReflector.

type ColumnMapperFunc

type ColumnMapperFunc func(structType reflect.Type) (titles []string, rowReflector RowReflector)

ColumnMapperFunc implements the ColumnMapper interface with a function

func (ColumnMapperFunc) ColumnTitlesAndRowReflector

func (f ColumnMapperFunc) ColumnTitlesAndRowReflector(structType reflect.Type) (titles []string, rowReflector RowReflector)

type ColumnTitles

type ColumnTitles []string

ColumnTitles implements ColumnMapper by returning the underlying string slice as column titles and the StructFieldValues function of this package as RowReflector. It does not check if the number of column titles and the reflected row values are identical and re-mapping or ignoring of columns is not possible.

func (ColumnTitles) ColumnTitlesAndRowReflector

func (t ColumnTitles) ColumnTitlesAndRowReflector(structType reflect.Type) (titles []string, rowReflector RowReflector)

type HTMLFormatRenderer

type HTMLFormatRenderer interface {
	// RenderBeforeTable is useful when you want to add custom styles or render anything before the table element.
	RenderBeforeTable(writer io.Writer) error
}

HTMLFormatRenderer is the renderer for the HTML format.

type HTMLRenderer

type HTMLRenderer struct {
	TableConfig *HTMLTableConfig
	// contains filtered or unexported fields
}

HTMLRenderer implements Renderer by using a HTMLFormatRenderer for a specific text based table format.

func NewHTMLRenderer

func NewHTMLRenderer(format HTMLFormatRenderer, TableConfig *HTMLTableConfig, config *strfmt.FormatConfig) *HTMLRenderer

func (*HTMLRenderer) MIMEType

func (*HTMLRenderer) MIMEType() string

func (*HTMLRenderer) RenderHeaderRow

func (htm *HTMLRenderer) RenderHeaderRow(columnTitles []string) error

func (*HTMLRenderer) RenderRow

func (htm *HTMLRenderer) RenderRow(columnValues []reflect.Value) error

func (*HTMLRenderer) Result

func (htm *HTMLRenderer) Result() ([]byte, error)

func (*HTMLRenderer) WriteResultFile

func (htm *HTMLRenderer) WriteResultFile(file fs.File, perm ...fs.Permissions) error

func (*HTMLRenderer) WriteResultTo

func (htm *HTMLRenderer) WriteResultTo(writer io.Writer) error

type HTMLTableConfig

type HTMLTableConfig struct {
	Caption         string
	TableClass      string
	CaptionClass    string
	RowClass        string
	CellClass       string
	HeaderRowClass  string
	HeaderCellClass string
	DataRowClass    string
	DataCellClass   string
}

HTMLTableConfig is the config for the actual, visual, resulting HTML table.

type Reader

type Reader interface {
	NumRows() int
	ReadRowStrings(index int) ([]string, error)
	ReadRow(index int, destStruct reflect.Value) error
}

type ReflectColumnTitles

type ReflectColumnTitles struct {
	// Tag is the struct field tag to be used as column name
	Tag string
	// IgnoreTitle will result in a column index of -1
	IgnoreTitle string
	// UntaggedFieldTitle will be called with the struct field name to
	// return a column name in case the struct field has no tag named Tag.
	// If UntaggedFieldTitle is nil, then the struct field name with be used unchanged.
	UntaggedFieldTitle func(fieldName string) (columnTitle string)
	// MapIndices is a map from the index of a field in struct
	// to the column index returned by ColumnTitlesAndRowReflector.
	// If MapIndices is nil, then no mapping will be performed.
	// Map to the index -1 to not create a column for a struct field.
	MapIndices map[int]int
}

ReflectColumnTitles implements ColumnMapper with a struct field Tag to be used for naming and a UntaggedFieldTitle in case the Tag is not set.

func (*ReflectColumnTitles) ColumnTitlesAndRowReflector

func (n *ReflectColumnTitles) ColumnTitlesAndRowReflector(structType reflect.Type) (titles []string, rowReflector RowReflector)

func (*ReflectColumnTitles) String

func (n *ReflectColumnTitles) String() string

func (*ReflectColumnTitles) WithIgnoreIndex

func (n *ReflectColumnTitles) WithIgnoreIndex(fieldIndex int) *ReflectColumnTitles

func (*ReflectColumnTitles) WithIgnoreTitle

func (n *ReflectColumnTitles) WithIgnoreTitle(ignoreTitle string) *ReflectColumnTitles

func (*ReflectColumnTitles) WithMapIndex

func (n *ReflectColumnTitles) WithMapIndex(fieldIndex, columnIndex int) *ReflectColumnTitles

func (*ReflectColumnTitles) WithMapIndices

func (n *ReflectColumnTitles) WithMapIndices(mapIndices map[int]int) *ReflectColumnTitles

func (*ReflectColumnTitles) WithTag

type Renderer

type Renderer interface {
	RenderHeaderRow(columnTitles []string) error
	RenderRow(columnValues []reflect.Value) error

	Result() ([]byte, error)
	WriteResultTo(w io.Writer) error
	WriteResultFile(file fs.File, perm ...fs.Permissions) error

	// MIMEType returns the MIME-Type of the rendered content
	MIMEType() string
}

type RowReflector

type RowReflector interface {
	// ReflectRow returns reflection values for struct fields
	// of structValue representing a table row.
	ReflectRow(structValue reflect.Value) (columnValues []reflect.Value)
}

RowReflector is used to reflect column values from the fields of a struct representing a table row.

type RowReflectorFunc

type RowReflectorFunc func(structValue reflect.Value) (columnValues []reflect.Value)

RowReflectorFunc implements RowReflector with a function

func (RowReflectorFunc) ReflectRow

func (f RowReflectorFunc) ReflectRow(structValue reflect.Value) (columnValues []reflect.Value)

type TextFormatRenderer

type TextFormatRenderer interface {
	RenderBeginTableText(writer io.Writer) error
	RenderHeaderRowText(writer io.Writer, columnTitles []string) error
	RenderRowText(writer io.Writer, fields []string) error
	RenderEndTableText(writer io.Writer) error
}

TextFormatRenderer has to be formatemented for a format to be used by TextRenderer.

type TextReader

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

func NewTextReader

func NewTextReader(rows [][]string, columnMapping map[int]string, columnTitleTag string, scanConfig ...*strfmt.ScanConfig) *TextReader

func (*TextReader) NumRows

func (tr *TextReader) NumRows() int

func (*TextReader) ReadRow

func (tr *TextReader) ReadRow(index int, destStruct reflect.Value) error

type TextRenderer

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

TextRenderer implements Renderer by using a TextFormatRenderer for a specific text based table format.

func NewTextRenderer

func NewTextRenderer(format TextFormatRenderer, config *strfmt.FormatConfig) *TextRenderer

func (*TextRenderer) RenderHeaderRow

func (txt *TextRenderer) RenderHeaderRow(columnTitles []string) error

func (*TextRenderer) RenderRow

func (txt *TextRenderer) RenderRow(columnValues []reflect.Value) error

func (*TextRenderer) Result

func (txt *TextRenderer) Result() ([]byte, error)

func (*TextRenderer) WriteResultFile

func (txt *TextRenderer) WriteResultFile(file fs.File, perm ...fs.Permissions) error

func (*TextRenderer) WriteResultTo

func (txt *TextRenderer) WriteResultTo(writer io.Writer) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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