formatter

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: May 4, 2022 License: Apache-2.0 Imports: 8 Imported by: 2

Documentation

Overview

Package formatter provides various implementations to convert arbitrary values to different string representations such as JSON.

Index

Constants

This section is empty.

Variables

View Source
var ErrUnsupported = errors.New("unsupported type")

ErrUnsupported is the error resulting if a Formatter does not support the type.

Functions

func FormatTab

func FormatTab(tw *tabwriter.Writer, f Formatter, i interface{}) (n int, err error)

FormatTab formats the given input and translates tabbed columns into properly aligned text.

func Noop

func Noop(_ interface{}) (s string, err error)

Noop always returns an empty string and no error.

Types

type CompFormatter

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

CompFormatter combines multiple Formatters, each handling a different type.

func NewComp

func NewComp() *CompFormatter

NewComp creates and initializes a new CompFormatter.

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

func AsTab(f Formatter) Formatter

AsTab returns a new Formatter that translates tabbed columns into properly aligned text.

func FromMap

func FromMap(sep, delim string) Formatter

FromMap creates a Formatter that outputs all map entries in unspecified order.

func FromMapKeys

func FromMapKeys(sep, delim string, ks ...reflect.Value) Formatter

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

func FromMapSlice(sep, delim string) Formatter

FromMapSlice creates a Formatter that formats a slice of maps.

func FromMapSliceKeys

func FromMapSliceKeys(sep, delim string, ks ...reflect.Value) Formatter

FromMapSliceKeys creates a Formatter that outputs a slice of maps.

func FromStruct

func FromStruct(sep, delim string, typ reflect.Type) Formatter

FromStruct creates a new Formatter for a struct type.

func FromStructSlice

func FromStructSlice(sep, delim string, typ reflect.Type) Formatter

FromStructSlice creates a new Formatter for a struct slice. The fields are determined by the slice's element type.

func FromTemplate

func FromTemplate(tmpl *template.Template) Formatter

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.

type Func

type Func func(i interface{}) (string, error)

Func is an adapter to allow the use of ordinary functions as Formatters. If f is a function with the appropriate signature, Func(f) is a Formatter that calls f.

func (Func) Format

func (f Func) Format(i interface{}) (string, error)

Format returns a string by applying f to i.

Jump to

Keyboard shortcuts

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