Documentation
¶
Overview ¶
Package formatter provides various implementations to convert arbitrary values to different string representations such as JSON.
Index ¶
- Variables
- func FormatTab(tw *tabwriter.Writer, f Formatter, i interface{}) (n int, err error)
- func Noop(_ interface{}) (s string, err error)
- type CompFormatter
- type Formatter
- func AsTab(f Formatter) Formatter
- func FromMap(sep, delim string) Formatter
- func FromMapKeys(sep, delim string, ks ...reflect.Value) Formatter
- func FromMapSlice(sep, delim string) Formatter
- func FromMapSliceKeys(sep, delim string, ks ...reflect.Value) Formatter
- func FromStruct(sep, delim string, typ reflect.Type) Formatter
- func FromStructSlice(sep, delim string, typ reflect.Type) Formatter
- func FromTemplate(tmpl *template.Template) Formatter
- func NoopFormatter() Formatter
- type Func
Constants ¶
This section is empty.
Variables ¶
var ErrUnsupported = errors.New("unsupported type")
ErrUnsupported is the error resulting if a Formatter does not support the type.
Functions ¶
Types ¶
type CompFormatter ¶
type CompFormatter struct {
// contains filtered or unexported fields
}
CompFormatter combines multiple Formatters, each handling a different type.
func (CompFormatter) Format ¶
func (cf CompFormatter) Format(i interface{}) (string, error)
Format converts the given parameter to its string representation. If none of the registered Formatters can handle the given value, an error is returned.
func (*CompFormatter) SetFormatter ¶
func (cf *CompFormatter) SetFormatter(n string, f Formatter)
SetFormatter registers the Formatter for the given type. If a Formatter already exists for the type, it is replaced.
func (*CompFormatter) SetFormatterFunc ¶
func (cf *CompFormatter) SetFormatterFunc(n string, f Func)
SetFormatterFunc registers the Func for the given type. If a Formatter already exists for the type, it is replaced.
type Formatter ¶
type Formatter interface { // Format returns a suitable string representation of i. // // Format must not modify the given parameter, even temporarily. Format(i interface{}) (string, error) }
Formatter converts the given parameter to its string representation.
func AsTab ¶
AsTab returns a new Formatter that translates tabbed columns into properly aligned text.
func FromMapKeys ¶
FromMapKeys creates a Formatter that outputs entries for the given keys. Unlike FromMap, map entries are formatted in the specified order. If a key is given multiple times, it will be rendered multiple times.
func FromMapSlice ¶
FromMapSlice creates a Formatter that formats a slice of maps.
func FromMapSliceKeys ¶
FromMapSliceKeys creates a Formatter that outputs a slice of maps.
func FromStruct ¶
FromStruct creates a new Formatter for a struct type.
func FromStructSlice ¶
FromStructSlice creates a new Formatter for a struct slice. The fields are determined by the slice's element type.
func FromTemplate ¶
FromTemplate returns a new Formatter that applies a parsed template to the input. If an error occurs executing the template, execution stops and no output is returned.
func NoopFormatter ¶
func NoopFormatter() Formatter
NoopFormatter returns a simple Formatter that always returns an empty string and no error.