structformat

package module
v0.0.0-...-ddfccaa Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2022 License: MIT Imports: 5 Imported by: 0

README

Golang structural data formatter

import "gopkg.in/structformat.v0"

Go Import Go Reference

This module gives you greater control over how to format structual data instead of using Golang's default Printf formatting mechanism. It supports outputing to io.Writer interface so large data can be formatted in streaming fashion instead of returning a large string to reduce memory footprint.

Usage

To support structual formatting for your data types, implement the structformat.Formatter interface.

import "gopkg.in/structformat.v0"

type Person struct {
    FirstName string
    LastName string
    Hobbies []string
}

func (p Person) StructFormat(w structformat.Writer) (n int, err error) {
    // Describe your structual data using structformat's built-in formatters
    f := structformat.Struct("Person",
        structformat.KV("Full Name", structformat.Printf("%s %s", p.FirstName, p.LastName)))
    hobbies := structformat.Bracket()
    for _, hobby := range p.Hobbies {
        hobbies.Items = append(hobbies.Items, structformat.String(hobby))
    }
    f.Items = append(f.Items, structformat.KV("Hobbies", hobbies))
    return f.StructFormat(w)
}

func main() {
    person := &Person{
        FirstName: "Adam",
        LastName: "Brody",
        Hobbies: []string{
            "Movies",
            "Singing",
            "Surfing",
        },
    }
    // Format your structual data using structformat.FormatString() or structformat.Format()
    formatted, err := structformat.FormatString(person)
    if err != nil {
        panic(err)
    }
    fmt.Println(formatted)
}

And you get the following formatted output:

Person {
    Full Name: Adam Brody
    Hobbies: [
        Movies
        Singing
        Surfing
    ]
}

Documentation

Index

Constants

View Source
const DefaultIndent = "    "

Variables

This section is empty.

Functions

func Format

func Format(writer io.Writer, formatter Formatter, options ...FormatOption) (n int, err error)

Format a structual data and output to the provided writer.

func FormatString

func FormatString(formatter Formatter, options ...FormatOption) (s string, err error)

Format a structual data and return the formatted string.

Types

type BlockFormatter

type BlockFormatter struct {
	Open  string
	Close string
	Items []Formatter
}

func Block

func Block(items ...Formatter) *BlockFormatter

func Bracket

func Bracket(items ...Formatter) *BlockFormatter

func (*BlockFormatter) StructFormat

func (f *BlockFormatter) StructFormat(w Writer) (n int, err error)

type BoolFormatter

type BoolFormatter bool

func Bool

func Bool(value bool) BoolFormatter

Return a BoolFormatter that just outputs true or false.

func (BoolFormatter) StructFormat

func (f BoolFormatter) StructFormat(w Writer) (n int, err error)

type FormatOption

type FormatOption func(*indentWriter)

func WithIndent

func WithIndent(indent string) FormatOption

Use the provided string for each indent level

func WithIndentLevel

func WithIndentLevel(level int) FormatOption

Use the provided indent level

type Formatter

type Formatter interface {
	// Format the structual data and write to the Writer interface. Returns the number of bytes written in `n`.
	StructFormat(Writer) (n int, err error)
}

Implement this interface in your data types to support sturctual formatting.

type HexBytesFormatter

type HexBytesFormatter []byte

func HexBytes

func HexBytes(data []byte) HexBytesFormatter

Return a HexBytesFormatter that formats the bytes data using hex.Encoder.

func (HexBytesFormatter) StructFormat

func (f HexBytesFormatter) StructFormat(w Writer) (n int, err error)

type HexdumpFormatter

type HexdumpFormatter []byte

func Hexdump

func Hexdump(data []byte) HexdumpFormatter

Returns a HexdumpFormatter that format the bytes data using hex.Dumper().

func (HexdumpFormatter) StructFormat

func (f HexdumpFormatter) StructFormat(w Writer) (n int, err error)

type KVFormatter

type KVFormatter struct {
	Name            string
	Value           Formatter
	NewLineForValue bool
}

func KV

func KV(name string, value Formatter, options ...KVOption) *KVFormatter

Return a KVFormatter represents a key-value pair. Oftenly used inside the StructFormatter for form a map like structure.

func (*KVFormatter) StructFormat

func (f *KVFormatter) StructFormat(w Writer) (n int, err error)

type KVOption

type KVOption func(*KVFormatter)

func WithNewLine

func WithNewLine() KVOption

type PrintfFormatter

type PrintfFormatter struct {
	Fmt  string
	Args []interface{}
}

func Printf

func Printf(format string, args ...interface{}) *PrintfFormatter

Return a PrintfFormatter that apply Printf template to the arguments and format them as a string.

func (*PrintfFormatter) StructFormat

func (f *PrintfFormatter) StructFormat(w Writer) (n int, err error)

type StringFormatter

type StringFormatter string

func String

func String(s string) StringFormatter

Return a StringFormatter for a string. New lines will be indented according to the current indent level of the formatter.

func (StringFormatter) StructFormat

func (f StringFormatter) StructFormat(w Writer) (n int, err error)

type StructFormatter

type StructFormatter struct {
	Name  string
	Items []Formatter
}

func Struct

func Struct(name string, items ...Formatter) *StructFormatter

Return a StructFormatter that has a name and arbitrary number of items.

func (*StructFormatter) StructFormat

func (f *StructFormatter) StructFormat(w Writer) (n int, err error)

type Writer

type Writer interface {
	io.Writer

	IndentString() string
	LineNum() int
	LineOffset() int
	NonIndentLineOffset() int
	IsEmpty() bool

	// Return a new writer with indent level set to be current
	// indent level plus levelDelta. If levelDelta is 0, indent
	// is reset to empty level.
	Indent(levelDelta int) Writer
	EmptyCheck(prependIfNotEmpty string) Writer
	RemoveTrailingNewLines() Writer
}

Jump to

Keyboard shortcuts

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