outfmt

package module
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2024 License: Apache-2.0 Imports: 5 Imported by: 0

README

outfmt

outfmt is a library which can take in any generic data and print it out. Either in a table view, json or yaml. Outfmt is very practical in CLI usecases.

examples

some examples can be found under the _examples directory

output, err := outfmt.Format($MY_GENERIC_OBJECT, &outfmt.Config{
    Format: outfmt.OutputFormatTable,
})
if err != nil {
    fmt.Fprintf(os.Stderr, "could not format data: %s", err.Error())
    return
}

registering types.

when wanting to output structs as a table the struct firstly needs to be registed. The registration process also defined the fields which will be printed and the names they will get. Printing happens on a whitelist and not a blacklist like the json and yaml printers.


type Data struct {
    Id     string
    Name   string
    Active bool
}

func init() {
    outfmt.Register(Data{}, &outfmt.Spec{
        "default": {
            {"ID", "Id"},
            {"NAME", "Name"},
        },
        "wide": {
            {"ID", "Id"},
            {"NAME", "Name"},
            {"ACTIVE", "Active"},
        },
    })
}

// output
-------------------------------------
ID      NAME  
12323   cool  
534324  cool  
1gerfs  cool 

Now, when formatting the struct as a table outfmt will only print the Id and Names field of the struct. The outfmt.Spec consists of a map, this allows us to create certain conditions for what fields outfmt will print. If not being told otherwise outfmt will choose the default condition.

// output
-------------------------------------
ID      NAME  ACTIVE  
12323   cool  true    
534324  cool  false   
1gerfs  cool  true 
changing the condition

when specifying the Condition output format we must pass the name of the condition in the AdditionalField property of the outfmt.Config

formats

  • JSON
  • YAML
  • Table
  • Field
  • Condition

field

When wanting to only print one field pass the path of the field you want to print using the AdditionalField property on config.

type Names struct {
    First string
    Last string
}

type Person struct {
    Names Names
    Age int
}

output, err := outfmt.Format($MY_GENERIC_OBJECT, &outfmt.Config{
    Format: outfmt.OutputFormatField,
    AdditionalField: "Names.First"
})
if err != nil {
    fmt.Fprintf(os.Stderr, "could not format data: %s", err.Error())
    return
}

// output
-------------------------------------
John
Dick

here only the persons first name will be printed. AddtionalField can also be a comma seperated list of fields. When using a comma seperated list this is the output we get.

// output
-------------------------------------
John Deere
Dick McDonald

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Format

func Format(data any, config *Config) ([]byte, error)

func Register

func Register(of any, spec *Spec)

Types

type Config

type Config struct {
	Format OutputFormat

	/// only used for OutputFormatField
	AdditionalField string
}

type OutputFormat

type OutputFormat int64
const (
	OutputFormatJSON OutputFormat = iota
	OutputFormatYAML
	OutputFormatTable
	OutputFormatField
	OutputFormatCondition
)

type Spec

type Spec map[string][]SpecField

type SpecField

type SpecField struct {
	Key   string
	Field string
}

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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