mapprint

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2021 License: MIT Imports: 10 Imported by: 1

README

mapprint

Actions Status Coverage Status PkgGoDev GoDoc go-report go1.15

Printf for maps and structs

go get github.com/Eun/mapprint
// prints `[14:01:08.005674] Database initialized'
mapprint.Printf("[%0H:%02m:%02s.%06ms] Database initialized", map[string]interface{}{
    "H": 14,
    "m": 1,
    "s": 8,
    "ms": 5674,
})

structs

type User struct {
    Name    string
    Balance int
}

// prints `Hello Joe, your balance is 104€!'
mapprint.Printf("Hello %Name, your balance is %Balance€!", User{
    Name:    "Joe",
    Balance: 104,
})

formating

// prints `Hello        Joe'
mapprint.Printf("Hello %10user!", map[string]string{
    "user": "Joe",
})

// prints `Hello Joe       '
mapprint.Printf("Hello %-10user!", map[string]string{
    "user": "Joe",
})

// prints `Hello    Joe    '
mapprint.Printf("Hello |-10user!", map[string]string{
    "user": "Joe",
})

// prints `Hello Joe, your balance is 0000104.00€!'
mapprint.Printf("Hello %Name, your balance is %010.2Balance€!", map[string]interface{}{
    "Name": "Joe",
    "Balance": 104,
})

// maybe something more exotic?
// prints `Hello Joe, your balance is ABAB104.00€!'
mapprint.Printf("Hello %Name, your balance is %AB10.2Balance€!", map[string]interface{}{
    "Name": "Joe",
    "Balance": 104,
})

Documentation

Overview

Package mapprint provides functionality to print a map/struct in a formated way.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Fprintf

func Fprintf(w io.Writer, format string, bindings ...interface{}) (int, error)

Fprintf formats a map/struct according to a format specifier and writes to w. It returns the number of bytes written and any write error encountered. example:

Fprintf(w, "%Key1", map[string]interface{}{
    "Key1": "Value1",
})

func Printf

func Printf(format string, bindings ...interface{}) (int, error)

Printf formats a map/struct according according to a format specifier and writes to standard output. It returns the number of bytes written and any write error encountered.

func Sprintf

func Sprintf(format string, bindings ...interface{}) string

Sprintf formats a map/struct according according to a format specifier and returns the resulting string.

Types

type KeyNotFoundFunc

type KeyNotFoundFunc func(w io.Writer, printer *Printer, prefix, key []rune, defaultPrinter PrintValueFunc) (int, error)

KeyNotFoundFunc describes the custom function that will be called if a Key was not found.

func ClearKey

func ClearKey() KeyNotFoundFunc

ClearKey returns an empty string.

func DefaultValue

func DefaultValue(defaultValue interface{}) KeyNotFoundFunc

DefaultValue returns a default value.

func KeepKey

func KeepKey() KeyNotFoundFunc

KeepKey returns the requested key.

type PrintValueFunc

type PrintValueFunc func(w io.Writer, printer *Printer, prefix, key []rune, value reflect.Value) (int, error)

PrintValueFunc describes the custom function that will be called to print a reflect.Value.

type Printer

type Printer struct {
	// KeyToken specifies how keys start
	// the Default value is % (percent sign)
	KeyToken rune

	DefaultBindings interface{}
	KeyNotFound     KeyNotFoundFunc
	PrintValue      PrintValueFunc
	// SuppressErrors if possible
	SuppressErrors bool
	// contains filtered or unexported fields
}

Printer is a object that can be used to initialize the printer with custom settings A typical example could be:

p := Printer{
    KeyToken:    '$',
    KeyNotFound: DefaultValue("Unknown"),
    DefaultBindings: map[string]interface{}{
        "Key1": "Value1"
    },
}
p.Sprintf("Key1 is $Key1")
p.Sprintf("Key2 is $Key2")

func (*Printer) Fprintf

func (printer *Printer) Fprintf(w io.Writer, format string, bindings ...interface{}) (int, error)

Fprintf formats a map/struct according to a format specifier and writes to w. It returns the number of bytes written and any write error encountered. example:

Fprintf(w, "%Key1", map[string]interface{}{
    "Key1": "Value1",
})

func (*Printer) GetKeyToken

func (printer *Printer) GetKeyToken() rune

GetKeyToken returns the rune that is actually used for the key identification.

func (*Printer) Printf

func (printer *Printer) Printf(format string, bindings ...interface{}) (int, error)

Printf formats a map/struct according according to a format specifier and writes to standard output. It returns the number of bytes written and any write error encountered.

func (*Printer) Sprintf

func (printer *Printer) Sprintf(format string, bindings ...interface{}) string

Sprintf formats a map/struct according according to a format specifier and returns the resulting string.

Jump to

Keyboard shortcuts

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