Documentation ¶
Index ¶
- Variables
- func Format(w io.Writer, v interface{}, config *Config) error
- func FormatBytes(v interface{}, config *Config) ([]byte, error)
- func FormatString(v interface{}, config *Config) (string, error)
- func FormatterNames() []string
- func RegisterFormatFunc(name string, f FormatFunc)
- func RegisterFormatter(name string, f Formatter)
- type Config
- type FormatFunc
- type Formatter
- type FormatterMap
- type TemplateConfig
Constants ¶
This section is empty.
Variables ¶
var DefaultFormatters = FormatterMap{ "json": FormatFunc(func(v interface{}, config *Config) ([]byte, error) { return json.MarshalIndent(v, "", " ") }), "yaml": FormatFunc(func(v interface{}, config *Config) ([]byte, error) { return yaml.Marshal(v) }), "gostring": FormatFunc(func(v interface{}, config *Config) ([]byte, error) { return []byte(fmt.Sprintf("%#v", v)), nil }), "gotemplate": FormatFunc(func(v interface{}, config *Config) ([]byte, error) { var buf bytes.Buffer if err := formatTemplate(&buf, v, config); err != nil { return nil, err } return buf.Bytes(), nil }), }
DefaultFormatters is a map of built-in formatters which can be extended to make more formatters globally available. These are used as a fallback if the user does not provide a custom map as part of the config to Format* funcs.
Functions ¶
func Format ¶
Format formats v using the given config and writes the result to w. Returns any error that may occur during formatting. On errors nothing is written to w.
func FormatBytes ¶
FormatBytes formats v using the given config and returns the formatted bytes. Returns any error that may occur during formatting.
func FormatString ¶
FormatString formats v using the given config and returns the formatted string. Returns any error that may occur during formatting.
func FormatterNames ¶
func FormatterNames() []string
FormatterNames returns a sorted slice of globally registered formatter names. This is useful to present allowed values in command line flags.
func RegisterFormatFunc ¶
func RegisterFormatFunc(name string, f FormatFunc)
RegisterFormatFunc globally registers a FormatFunc. Panics if a formatter with the same name already exists.
func RegisterFormatter ¶
RegisterFormatter globally registers a Formatter. Panics if a formatter with the same name already exists.
Types ¶
type Config ¶
type Config struct { // Format must contain the name of the formatter that should be used. Format string // Formatters can be set to configure user-defined formatters. If empty, // the built-in formatters are used. Formatters FormatterMap // Template configures the template for template-based formatters. Template string // TemplateConfig is optional configuration for the underlying template // struct for template-based formatter. // See: https://golang.org/pkg/text/template/#Template TemplateConfig TemplateConfig // TemplateItems configures the behaviour of template based formatters. If // true the template applies only to the items of a slice that should be // formatted. This allows for omission of the `range` loop in templates. // This config only applies to slices. Has no effect on non-slice types. // // @FIXME(mohmann): should this be part of TemplateConfig instead? TemplateItems bool // TrailingNewline ensures that the formatted output always ends with a // trailing newline character if it is not empty. Does not add a newline if // the formatted output is already terminated by one. TrailingNewline bool // JSONPointer can be fill with a jsonpointer expression to select a nested // field within an object before passing it on to the formatter. If empty // the original object is passed as is. // See RFC: https://datatracker.ietf.org/doc/html/rfc6901 JSONPointer string }
Config configures the behaviour of the output formatter.
type FormatFunc ¶
FormatFunc is a func that implements the Formatter interface.
type Formatter ¶
type Formatter interface { // Format takes v and returns the formatted bytes. The Formatter may change // behaviour depending on the passed in config. Format(v interface{}, config *Config) ([]byte, error) }
Formatter can format values.
type FormatterMap ¶
FormatterMap maps a user-defined name to a formatter.
func (FormatterMap) DeepCopy ¶
func (m FormatterMap) DeepCopy() FormatterMap
DeepCopy copies m. Returns a new FormatterMap which contains all entries from m.
func (FormatterMap) Names ¶
func (m FormatterMap) Names() []string
Names returns a sorted slice of formatter names. This is useful to present allowed values in command line flags.
func (FormatterMap) RegisterFormatFunc ¶
func (m FormatterMap) RegisterFormatFunc(name string, f FormatFunc)
RegisterFormatFunc registers a FormatFunc. Panics if a formatter with the same name already exists.
func (FormatterMap) RegisterFormatter ¶
func (m FormatterMap) RegisterFormatter(name string, f Formatter)
RegisterFormatter registers a Formatter. Panics if a formatter with the same name already exists.
type TemplateConfig ¶
type TemplateConfig struct { // Funcs configures additional template funcs. // See: https://golang.org/pkg/text/template/#Template.Funcs Funcs template.FuncMap // Options configures additional template options. // See: https://golang.org/pkg/text/template/#Template.Option Options []string }
TemplateConfig is optional configuration for the underlying template struct for template-based formatter. See: https://golang.org/pkg/text/template/#Template