formatter

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2020 License: Apache-2.0 Imports: 8 Imported by: 5

Documentation

Overview

Package formatter implements “replacement fields” surrounded by curly braces {} format strings.

Index

Examples

Constants

View Source
const (
	DefaultPlaceholder    = "p"
	DefaultLeftDelimiter  = "{"
	DefaultRightDelimiter = "}"
)

These constants define default values used by formatter.

Variables

This section is empty.

Functions

func Format

func Format(message string, arguments ...interface{}) (string, error)

Format formats string.

Example (AutomaticPlaceholder)
package main

import (
	"fmt"

	"gitlab.com/tymonx/go-formatter/formatter"
)

func main() {
	formatted, err := formatter.Format("Automatic placeholder {p}:{p}:{p}():", "dir/file", 1, "func1")

	if err != nil {
		panic(err)
	}

	fmt.Println(formatted)
}
Output:

Automatic placeholder dir/file:1:func1():
Example (BackgroundBrightColors)
package main

import (
	"fmt"

	"gitlab.com/tymonx/go-formatter/formatter"
)

func main() {
	formatted, err := formatter.Format("With background bright colors {Cyan | Bright | Background}cyan{Normal}")

	if err != nil {
		panic(err)
	}

	fmt.Println(formatted)
}
Example (BackgroundColors)
package main

import (
	"fmt"

	"gitlab.com/tymonx/go-formatter/formatter"
)

func main() {
	formatted, err := formatter.Format("With background colors {Yellow | Background}yellow{Normal}")

	if err != nil {
		panic(err)
	}

	fmt.Println(formatted)
}
Example (BackgroundRGB)
package main

import (
	"fmt"

	"gitlab.com/tymonx/go-formatter/formatter"
)

func main() {
	formatted, err := formatter.Format("With background RGB {RGB 255 165 0 | Background}funky{Normal}")

	if err != nil {
		panic(err)
	}

	fmt.Println(formatted)
}
Example (BrightColors)
package main

import (
	"fmt"

	"gitlab.com/tymonx/go-formatter/formatter"
)

func main() {
	formatted, err := formatter.Format("With bright colors {Magenta | Bright}magenta{Normal}")

	if err != nil {
		panic(err)
	}

	fmt.Println(formatted)
}
Example (Colors)
package main

import (
	"fmt"

	"gitlab.com/tymonx/go-formatter/formatter"
)

func main() {
	formatted, err := formatter.Format("With colors {Red}red{Normal} {Green}green{Normal} {Blue}blue{Normal}")

	if err != nil {
		panic(err)
	}

	fmt.Println(formatted)
}
Example (MixedPlaceholders)
package main

import (
	"fmt"

	"gitlab.com/tymonx/go-formatter/formatter"
)

func main() {
	objectPointer := &struct {
		X int
		Y int
		Z int
	}{
		X: 2,
		Z: 6,
		Y: 3,
	}

	formatted, err := formatter.Format("Mixed placeholders {.X}.{p}.{.Y}.{.Z} {p1} {p0}", objectPointer, "b", "c", nil)

	if err != nil {
		panic(err)
	}

	fmt.Println(formatted)
}
Output:

Mixed placeholders 2.{2 3 6}.3.6 b {2 3 6} c <nil>
Example (NamedPlaceholders)
package main

import (
	"fmt"

	"gitlab.com/tymonx/go-formatter/formatter"
)

func main() {
	formatted, err := formatter.Format("Named placeholders {file}:{line}:{function}():", formatter.Named{
		"line":     3,
		"function": "func1",
		"file":     "dir/file",
	})

	if err != nil {
		panic(err)
	}

	fmt.Println(formatted)
}
Output:

Named placeholders dir/file:3:func1():
Example (ObjectAutomaticPlaceholders)
package main

import (
	"fmt"

	"gitlab.com/tymonx/go-formatter/formatter"
)

func main() {
	object1 := struct {
		X int
	}{
		X: 1,
	}

	object2 := struct {
		Message string
	}{
		Message: "msg",
	}

	formatted, err := formatter.Format("{p.X} {p.Message}", object1, object2)

	if err != nil {
		panic(err)
	}

	fmt.Println(formatted)
}
Output:

1 msg
Example (ObjectPlaceholders)
package main

import (
	"fmt"

	"gitlab.com/tymonx/go-formatter/formatter"
)

func main() {
	object := struct {
		Line     int
		Function string
		File     string
	}{
		Line:     4,
		Function: "func1",
		File:     "dir/file",
	}

	formatted, err := formatter.Format("Object placeholders {.File}:{.Line}:{.Function}():", object)

	if err != nil {
		panic(err)
	}

	fmt.Println(formatted)
}
Output:

Object placeholders dir/file:4:func1():
Example (ObjectPointerPlaceholders)
package main

import (
	"fmt"

	"gitlab.com/tymonx/go-formatter/formatter"
)

func main() {
	objectPointer := &struct {
		X int
		Y int
		Z int
	}{
		X: 4,
		Z: 3,
		Y: 1,
	}

	formatted, err := formatter.Format("Object placeholders {.X}.{.Y}.{.Z}", objectPointer)

	if err != nil {
		panic(err)
	}

	fmt.Println(formatted)
}
Output:

Object placeholders 4.1.3
Example (ObjectPositionalPlaceholders)
package main

import (
	"fmt"

	"gitlab.com/tymonx/go-formatter/formatter"
)

func main() {
	object1 := struct {
		X int
	}{
		X: 1,
	}

	object2 := struct {
		Y int
	}{
		Y: 2,
	}

	formatted, err := formatter.Format("{p1.Y} {p0.X}", object1, object2)

	if err != nil {
		panic(err)
	}

	fmt.Println(formatted)
}
Output:

2 1
Example (PositionalPlaceholders)
package main

import (
	"fmt"

	"gitlab.com/tymonx/go-formatter/formatter"
)

func main() {
	formatted, err := formatter.Format("Positional placeholders {p1}:{p0}:{p2}():", 2, "dir/file", "func1")

	if err != nil {
		panic(err)
	}

	fmt.Println(formatted)
}
Output:

Positional placeholders dir/file:2:func1():
Example (Rgb)
package main

import (
	"fmt"

	"gitlab.com/tymonx/go-formatter/formatter"
)

func main() {
	formatted, err := formatter.Format("With RGB {RGB 255 165 0}funky{Normal}")

	if err != nil {
		panic(err)
	}

	fmt.Println(formatted)
}
Example (SetDelimiters)
package main

import (
	"fmt"

	"gitlab.com/tymonx/go-formatter/formatter"
)

func main() {
	formatted, err := formatter.New().SetDelimiters("<", ">").Format("Custom delimiters <p1> <p0>", "4", 3)

	if err != nil {
		panic(err)
	}

	fmt.Println(formatted)
}
Output:

Custom delimiters 3 4
Example (SetFunctions)
package main

import (
	"fmt"

	"gitlab.com/tymonx/go-formatter/formatter"
)

func main() {
	functions := formatter.Functions{
		"str": func() string {
			return "text"
		},
		"number": func() int {
			return 3
		},
		"boolean": func() bool {
			return true
		},
		"floating": func() float64 {
			return 4.5
		},
	}

	formatted, err := formatter.New().SetFunctions(functions).Format("Custom functions {str} {p} {number} {boolean} {floating}", 5)

	if err != nil {
		panic(err)
	}

	fmt.Println(formatted)
}
Output:

Custom functions text 5 3 true 4.5
Example (SetPlaceholder)
package main

import (
	"fmt"

	"gitlab.com/tymonx/go-formatter/formatter"
)

func main() {
	formatted, err := formatter.New().SetPlaceholder("arg").Format("Custom placeholder {arg1} {arg0}", "2", 3)

	if err != nil {
		panic(err)
	}

	fmt.Println(formatted)
}
Output:

Custom placeholder 3 2
Example (WithArguments)
formatted, err := formatter.Format("With arguments", 3, nil, 4.5, true, "arg1", []byte{}, Error("error"))

if err != nil {
	panic(err)
}

fmt.Println(formatted)
Output:

With arguments 3 <nil> 4.5 true arg1 [] error
Example (WithoutArguments)
package main

import (
	"fmt"

	"gitlab.com/tymonx/go-formatter/formatter"
)

func main() {
	formatted, err := formatter.Format("Without arguments")

	if err != nil {
		panic(err)
	}

	fmt.Println(formatted)
}
Output:

Without arguments

func FormatWriter

func FormatWriter(writer io.Writer, message string, arguments ...interface{}) error

FormatWriter formats string to writer.

Example
package main

import (
	"bytes"
	"fmt"

	"gitlab.com/tymonx/go-formatter/formatter"
)

func main() {
	buffer := new(bytes.Buffer)

	if err := formatter.FormatWriter(buffer, "Writer {p2}", 3, "foo", "bar"); err != nil {
		panic(err)
	}

	fmt.Println(buffer.String())
}
Output:

Writer bar 3 foo

func MustFormat added in v1.1.0

func MustFormat(message string, arguments ...interface{}) string

MustFormat is like Format but panics if provided message cannot be formatted. It simplifies safe initialization of global variables holding formatted strings.

Example
fmt.Println(formatter.MustFormat("With arguments", 3, nil, false, 4.5, "text", []byte{}, Error("error")))
Output:

With arguments 3 <nil> false 4.5 text [] error

Types

type Formatter

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

Formatter defines a formatter object that formats string using “replacement fields” surrounded by curly braces {}.

func New

func New() *Formatter

New creates a new formatter object.

func (*Formatter) AddFunction

func (f *Formatter) AddFunction(name string, function interface{}) *Formatter

AddFunction adds template function used by formatter.

func (*Formatter) AddFunctions

func (f *Formatter) AddFunctions(functions Functions) *Formatter

AddFunctions adds template functions used by formatter.

func (*Formatter) Format

func (f *Formatter) Format(message string, arguments ...interface{}) (string, error)

Format formats string.

func (*Formatter) FormatWriter

func (f *Formatter) FormatWriter(writer io.Writer, message string, arguments ...interface{}) error

FormatWriter formats string to writer.

func (*Formatter) GetDelimiters

func (f *Formatter) GetDelimiters() (left, right string)

GetDelimiters returns delimiters used by formatter. Default is {}.

func (*Formatter) GetFunction

func (f *Formatter) GetFunction(name string) interface{}

GetFunction returns template function used by formatter.

func (*Formatter) GetFunctions

func (f *Formatter) GetFunctions() Functions

GetFunctions returns template functions used by formatter.

func (*Formatter) GetLeftDelimiter

func (f *Formatter) GetLeftDelimiter() string

GetLeftDelimiter returns left delimiter used by formatter. Default is {.

func (*Formatter) GetPlaceholder

func (f *Formatter) GetPlaceholder() string

GetPlaceholder returns placeholder string prefix used for automatic and positional placeholders to format string. Default is p.

func (*Formatter) GetRightDelimiter

func (f *Formatter) GetRightDelimiter() string

GetRightDelimiter returns right delimiter used by formatter. Default is }.

func (*Formatter) MustFormat added in v1.1.0

func (f *Formatter) MustFormat(message string, arguments ...interface{}) string

MustFormat is like Format but panics if provided message cannot be formatted. It simplifies safe initialization of global variables holding formatted strings.

func (*Formatter) RemoveFunction

func (f *Formatter) RemoveFunction(name string) *Formatter

RemoveFunction removes template function used by formatter.

func (*Formatter) RemoveFunctions

func (f *Formatter) RemoveFunctions(names []string) *Formatter

RemoveFunctions removes template functions used by formatter.

func (*Formatter) Reset

func (f *Formatter) Reset() *Formatter

Reset resets formatter to default state.

func (*Formatter) ResetDelimiters

func (f *Formatter) ResetDelimiters() *Formatter

ResetDelimiters resets delimiters used by formatter to default values.

func (*Formatter) ResetFunctions

func (f *Formatter) ResetFunctions() *Formatter

ResetFunctions resets template functions used by formatter.

func (*Formatter) ResetLeftDelimiter

func (f *Formatter) ResetLeftDelimiter() *Formatter

ResetLeftDelimiter resets left delimiter used by formatter to default value.

func (*Formatter) ResetPlaceholder

func (f *Formatter) ResetPlaceholder() *Formatter

ResetPlaceholder resets placeholder to default value.

func (*Formatter) ResetRightDelimiter

func (f *Formatter) ResetRightDelimiter() *Formatter

ResetRightDelimiter resets right delimiter used by formatter to default value.

func (*Formatter) SetDelimiters

func (f *Formatter) SetDelimiters(left, right string) *Formatter

SetDelimiters sets delimiters used by formatter. Default is {}.

func (*Formatter) SetFunctions

func (f *Formatter) SetFunctions(functions Functions) *Formatter

SetFunctions sets template functions used by formatter.

func (*Formatter) SetLeftDelimiter

func (f *Formatter) SetLeftDelimiter(delimiter string) *Formatter

SetLeftDelimiter sets left delimiter used by formatter. Default is {.

func (*Formatter) SetPlaceholder

func (f *Formatter) SetPlaceholder(placeholder string) *Formatter

SetPlaceholder sets placeholder string prefix used for automatic and positional placeholders to format string. Default is p.

func (*Formatter) SetRightDelimiter

func (f *Formatter) SetRightDelimiter(delimiter string) *Formatter

SetRightDelimiter sets right delimiter used by formatter. Default is }.

type Functions

type Functions map[string]interface{}

Functions defines a map of template functions.

type Named

type Named map[string]interface{}

Named defines named arguments.

Jump to

Keyboard shortcuts

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