dapper

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2019 License: MIT Imports: 8 Imported by: 1

README

Dapper

Build Status Code Coverage Latest Version GoDoc Go Report Card

Dapper is a pretty-printer for Go values that aims to produce the shortest possible output without ambiguity. Additionally, the output is deterministic, which allows for the generation of human-readable diffs using standard tools.

Example

This example renders a basic tree structure. Note that the output only includes type names where the value's type can not be inferred from the context.

Code
package main

import (
	"fmt"
	"github.com/dogmatiq/dapper"
)

type TreeNode struct {
	Name     string
	Value    interface{}
	Children []*TreeNode
}

type NodeValue struct{}

func main() {
	v := TreeNode{
		Name: "root",
		Children: []*TreeNode{
			{
				Name:  "branch #1",
				Value: 100,
			},
			{
				Name:  "branch #2",
				Value: NodeValue{},
			},
		},
	}

	s := dapper.Format(v)
	fmt.Println(s)
}
Output
main.TreeNode{
    Name:     "root"
    Value:    nil
    Children: {
        {
            Name:     "branch #1"
            Value:    int(100)
            Children: nil
        }
        {
            Name:     "branch #2"
            Value:    main.NodeValue{}
            Children: nil
        }
    }
}

Documentation

Overview

Package dapper is a deterministic pretty-printer with minimal output.

Index

Examples

Constants

View Source
const (
	// DefaultIndent is the default indent string used to indent nested values.
	DefaultIndent = "    "

	// DefaultRecursionMarker is the default string to displayed when recursion is
	// detected within a Go value.
	DefaultRecursionMarker = "<recursion>"
)

Variables

This section is empty.

Functions

func Format

func Format(v interface{}) string

Format returns a pretty-printed representation of v.

func Print added in v0.2.0

func Print(v interface{})

Print writes a pretty-printed representation of v to os.Stdout.

Example
type TreeNode struct {
	Name     string
	Value    interface{}
	Children []*TreeNode
}

type NodeValue struct{}

v := TreeNode{
	Name: "root",
	Children: []*TreeNode{
		{
			Name:  "branch #1",
			Value: 100,
		},
		{
			Name:  "branch #2",
			Value: NodeValue{},
		},
	},
}

Print(v)
Output:

dapper_test.TreeNode{
    Name:     "root"
    Value:    nil
    Children: {
        {
            Name:     "branch #1"
            Value:    int(100)
            Children: nil
        }
        {
            Name:     "branch #2"
            Value:    dapper_test.NodeValue{}
            Children: nil
        }
    }
}

func Write

func Write(w io.Writer, v interface{}) (int, error)

Write writes a pretty-printed representation of v to w using the default printer settings.

It returns the number of bytes written.

Types

type Printer

type Printer struct {
	// Indent is the string used to indent nested values.
	// If it is empty, DefaultIndent is used.
	Indent string

	// RecursionMarker is a string that is displayed instead of a value's
	// representation when recursion has been detected.
	// If it is empty, DefaultRecursionMarker is used.
	RecursionMarker string
}

Printer generates human-readable representations of Go values.

The output format is intended to be as minimal as possible, without being ambigious. To that end, type information is only included where it can not be reliably inferred from the structure of the value.

func (*Printer) Format

func (p *Printer) Format(v interface{}) string

Format returns a pretty-printed representation of v.

func (*Printer) Write

func (p *Printer) Write(w io.Writer, v interface{}) (int, error)

Write writes a pretty-printed representation of v to w.

It returns the number of bytes written.

Jump to

Keyboard shortcuts

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