Yet another flexible formatter
Reflect, render and output arbitrary data structures using plugin formatters. Supported formats include CSV, JSON, YAML, Text and Go templates.
Yaff is feature rich and a working MVP The documentation is still a bit light and increased test coverage is required.
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.
- 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.
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 |
// +-----------+--------+----------+---------+-------------+--------+
We would welcome contributions to this project. Please read our CONTRIBUTION file for further details on how you can participate or report any issues.
This software is licensed under the Apache License.