dump

package module
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Dec 14, 2023 License: MIT Imports: 10 Imported by: 0

README

dump

Deep multi-line dump for any object

Package dump provides a functions for dumping Go values in a human-readable format.

Dumper type that can be used to write a string representation of a value to io.Writer

Log function that logs the result of dump working to stderr and return Log function with nested level prefix

Dump function that return string result of dump working

Example usage:

package main

import "github.com/d-enk/dump"

func main() {
 const multiline = `multi
line`

 val := []any{
   0, "str", false, nil,
   []any{multiline},
   struct {
    Field        any
    privateField any
   }{
    Field:        `1`,
    privateField: 1,
   },
   map[any]any{"-": multiline},
  }

 dump.Log("Title")(
  "Nested",
 )(multiline, val)
 // Output:
 // Title
 //   Nested
 //     `multi
 //      line` [
 //      0,
 //      `str`,
 //      false,
 //      nil,
 //      [
 //        `multi
 //         line`,
 //      ],
 //      {
 //        Field: `1`
 //        privateField: 1
 //      },
 //      {
 //        `-`: |
 //         `multi
 //          line`,
 //      },
 //     ]
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	NestedPadding = "  "
	LogWriter     = io.Writer(os.Stderr)
	LogChunkSize  = 1024
)

Functions

func Dump

func Dump(v ...any) string
Example
package main

import (
	"fmt"

	"github.com/d-enk/dump"
)

func main() {
	const multiline = `multi
line`

	fmt.Println(dump.Dump(
		multiline,
		[]any{
			0, "str", false, nil,
			[]any{multiline},
			map[any]any{"-": multiline},
		},
	))
}
Output:

`multi
 line` [
  0,
  `str`,
  false,
  nil,
  [
    `multi
     line`,
  ],
  {
    `-`: |
     `multi
      line`,
  },
]

func Log

func Log(v ...any) tLog
Example
package main

import (
	"os"

	"github.com/d-enk/dump"
)

func main() {
	dump.LogWriter = os.Stdout // only for test

	l := dump.Log("Title")(
		"Nested:", "some",
	)
	l("Next nested")("...")
	l(
		[]any{
			map[any]any{"key": "val"},
			[]int{1, 2, 3},
			struct {
				Field        any
				privateField any
			}{
				Field:        `1`,
				privateField: 1,
			},
		},
	)
}
Output:

`Title`
  `Nested:` `some`
    `Next nested`
      `...`
    [
      {
        `key`: `val`,
      },
      [
        1,
        2,
        3,
      ],
      {
        Field: `1`
        privateField: 1
      },
    ]

Types

type Dumper

type Dumper struct {
	// contains filtered or unexported fields
}
Example
package main

import (
	"fmt"
	"strings"

	"github.com/d-enk/dump"
)

func main() {
	builder := &strings.Builder{}
	dumper := dump.New(builder)
	dumper.WithPrefix("---").Dumpln("With prefix")
	dumper.Dumpln(map[any]any{
		"1": []any{
			false,
			nil,
			"",
		},
	})
	dumper.Dump(
		1, 2, 3,
		struct{ A4, A5, A6 int }{4, 5, 6},
	)
	fmt.Println(builder.String())
}
Output:

---`With prefix`
{
  `1`: [
    false,
    nil,
    ``,
  ],
}
1 2 3 {
  A4: 4
  A5: 5
  A6: 6
}

func New added in v0.2.0

func New(writer io.Writer) *Dumper

func (*Dumper) Dump

func (d *Dumper) Dump(v ...any)

func (*Dumper) Dumpln added in v0.2.0

func (d *Dumper) Dumpln(v ...any)

func (*Dumper) WithBuffer added in v0.3.0

func (d *Dumper) WithBuffer(chunkSize int) *Dumper

func (Dumper) WithPrefix

func (d Dumper) WithPrefix(prefix string) *Dumper

Jump to

Keyboard shortcuts

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