yaff

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2021 License: Apache-2.0 Imports: 4 Imported by: 0

README

yaff

Yet another flexible formatter

Reflect, render and output arbitrary data structures using plugin formatters. Supported formats include CSV, JSON, YAML, Text and Go templates.

Status

Yaff is feature rich and a working MVP The documentation is still a bit light and increased test coverage is required.

Status Build Status Release Coveralls License GoReportCard Go Doc Conventional Commits

Installation

go get -u https://github.com/nehemming/yaff

or clone this repo to your local machine using

git clone https://github.com/nehemming/yaff

This project requires Go 1.15 or newer and supports modules.

Key features

  • Reflects arbitrary data structures to output formatted text
  • Plug in formatter model, with built in support for csv, json, yaml, text and go templates
  • Integrates with Viper and Cobra cli application commands to bind formatting flags and configuration parameters.
  • Text formatter supports auto sizing word wrapping grid.

Getting started

Here is an example outputting an array of structures in a grid

package main

import (
	"bytes"
	"fmt"

	"github.com/nehemming/yaff"
	"github.com/nehemming/yaff/textformatter"
)

type dataRow struct {
	StringVal   string
	IntVal      int
	FloatVal    float64
	BoolVal     bool
	OnlyTrueVal bool `tabular:",trueonly"`
	hidden      int
	Embedded    embeddedData
}

type embeddedData struct {
	innerStr string
	IntTwo   int
}

func main() {
	// Get the text formatter
	formatter, err := yaff.Formatters().GetFormatter(textformatter.Text)

	if err != nil {
		fmt.Println(err)
		return
	}

	options := textformatter.NewOptions()

	// Output in grid style
	options.Style = textformatter.Grid

	// Generate some output, using io writer interface of Buffer
	var buf bytes.Buffer

	err = formatter.Format(&buf, options, []dataRow{
		{StringVal: "Hello", IntVal: 10, FloatVal: 9.8, BoolVal: true, OnlyTrueVal: true, hidden: 99,
			Embedded: embeddedData{innerStr: "hidden", IntTwo: 2}},
		{StringVal: "There", IntVal: 20, FloatVal: 3.14, BoolVal: false, OnlyTrueVal: false, hidden: 99,
			Embedded: embeddedData{innerStr: "hidden2", IntTwo: 2}},
	})

	if err != nil {
		fmt.Println(err)
	} else {
		fmt.Println(buf.String())
	}
}

// Output:
// +-----------+--------+----------+---------+-------------+--------+
// | StringVal | IntVal | FloatVal | BoolVal | OnlyTrueVal | IntTwo |
// +-----------+--------+----------+---------+-------------+--------+
// | Hello     |     10 |      9.8 |    true |        true |      2 |
// +-----------+--------+----------+---------+-------------+--------+
// | There     |     20 |     3.14 |   false |             |      2 |
// +-----------+--------+----------+---------+-------------+--------+

Contributing

We would welcome contributions to this project. Please read our CONTRIBUTION file for further details on how you can participate or report any issues.

License

FOSSA Status

This software is licensed under the Apache License.

Documentation

Overview

Package yaff yet another flexible formatter.

Example
package main

import (
	"bytes"
	"fmt"

	"github.com/nehemming/yaff"
	"github.com/nehemming/yaff/textformatter"
)

type dataRow struct {
	StringVal   string
	IntVal      int
	FloatVal    float64
	BoolVal     bool
	OnlyTrueVal bool `tabular:",trueonly"`
	hidden      int  //nolint:structcheck
	Embedded    embeddedData
}

type embeddedData struct {
	innerStr string
	IntTwo   int
}

func main() {
	// Get the text formatter
	formatter, err := yaff.Formatters().GetFormatter(textformatter.Text)
	if err != nil {
		fmt.Println(err)
		return
	}

	options := textformatter.NewOptions()

	// Output in grid style
	options.Style = textformatter.Grid

	// Generate some output, using io writer interface of Buffer
	var buf bytes.Buffer

	err = formatter.Format(&buf, options, []dataRow{
		{
			StringVal: "Hello", IntVal: 10, FloatVal: 9.8, BoolVal: true, OnlyTrueVal: true, hidden: 99,
			Embedded: embeddedData{innerStr: "hidden", IntTwo: 2},
		},
		{
			StringVal: "There", IntVal: 20, FloatVal: 3.14, BoolVal: false, OnlyTrueVal: false, hidden: 99,
			Embedded: embeddedData{innerStr: "hidden2", IntTwo: 2},
		},
	})

	if err != nil {
		fmt.Println(err)
	} else {
		fmt.Println(buf.String())
	}
}

func main() {
	main()

}
Output:

+-----------+--------+----------+---------+-------------+--------+
| StringVal | IntVal | FloatVal | BoolVal | OnlyTrueVal | IntTwo |
+-----------+--------+----------+---------+-------------+--------+
| Hello     |     10 |      9.8 |    true |        true |      2 |
+-----------+--------+----------+---------+-------------+--------+
| There     |     20 |     3.14 |   false |             |      2 |
+-----------+--------+----------+---------+-------------+--------+

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Format

type Format string

Format identifier.

type FormatOptions

type FormatOptions interface{}

FormatOptions options controlling formatting, every formatter can implement its own options.

type Formatter

type Formatter interface {

	// Format formats the supplied input data using the format options with output to the writer.
	Format(writer io.Writer, options FormatOptions, data ...interface{}) error
}

Formatter supports writing formated data to an writer.

type NewFormatter

type NewFormatter func() (Formatter, error)

NewFormatter function type to create a new formatter. Each formatter type registered will provide an implementation to creeate an instance of an associated formatter.

type Registry

type Registry interface {

	// Register adds or updates an available format.
	// The NewFormatter factory is used to create an
	// instance oif the registered formatter.
	Register(format Format, factory NewFormatter)

	// GetFormatter returns the formatter supporting the format or an error if no formatter can be found.
	GetFormatter(format Format) (Formatter, error)

	// Formats returns a slice of supported formats.
	Formats() []Format
}

Registry holds a list of available formatters. It is possible to create multiple formatters, but by default the default shared yaff.Formatters registry can be used Registry implementors must be threadsafe across all calls.

func Formatters

func Formatters() Registry

Formatters is the shared registry of formatters.

func NewRegistry

func NewRegistry() Registry

NewRegistry creates a new registry.

Directories

Path Synopsis
Package cliflags allows an application to bind parameters to control outputt formats
Package cliflags allows an application to bind parameters to control outputt formats
langpack
Package langpack for yaff cli flags.
Package langpack for yaff cli flags.
Package csvformatter is the yaff JSON formatter.
Package csvformatter is the yaff JSON formatter.
Package jsonformatter is the yaff JSON formatter.
Package jsonformatter is the yaff JSON formatter.
Package langpack for yaff.
Package langpack for yaff.
Package templateformatter is the yaff Template formatter.
Package templateformatter is the yaff Template formatter.
Package textformatter is the yaff Text formatter.
Package textformatter is the yaff Text formatter.
Package yamlformatter is the yaff YAML formatter.
Package yamlformatter is the yaff YAML formatter.

Jump to

Keyboard shortcuts

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