dd

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2022 License: MIT Imports: 10 Imported by: 5

README

dd test Go Reference

dd dumps Go data structures as valid syntax in Go.

  • ✅ Simple API
  • ✅ Support Go 1.16 ~ 1.18 (available generics!)
  • ✅ Customizable dump format each types
    • Available some options in df package
  • ✅ Support pretty print
    • You can use any color theme you like.

There are several libraries similar to this exist. I like them all, each one leans toward debugging purposes mainly.

In some cases, we want to use these data structures as test data. None of them output valid Go syntax, so I had to manually modify them.

dd solves this problem. Output as valid syntax, we did get also more prettry and readable form.

Synopsis

Generator purpose

Add this import line to the file you're working in:

import "github.com/Code-Hex/dd"

and just call Dump function.

data := map[string]int{
  "b": 2,
  "a": 1,
  "c": 3,
}
fmt.Println(dd.Dump(data))
// map[string]int{
//   "a": 1,
//   "b": 2,
//   "c": 3,
// }

// There are also some options
fmt.Println(dd.Dump(data, dd.WithIndent(4)))
// map[string]int{
//     "a": 1,
//     "b": 2,
//     "c": 3,
// }
Debugging purpose

Add this import line to the file you're working in:

import "github.com/Code-Hex/dd/p"

and just call p.P

func main() {
  srv := &http.Server{
    Addr:    ":8080",
    Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}),
  }
  fmt.Println("--- monokai")
  p.P(srv)
}
color.png

You can read examples/pretty/main.go. If you want to adopt a color theme of your own choice, the following links will help you: pkg.go.dev/github.com/alecthomas/chroma/styles.

Customize the format

WithDumpFunc option helps you if you want to customize the format for each type. This option works as code using Generics for 1.18 and above, otherwise it uses reflect.

Several wrapper options using this option are provided in the df package.

import "github.com/Code-Hex/dd/df"

and call Dump function with options within the package.

// json.RawMessage(`{"message":"Hello, World"}`)
fmt.Println(
  dd.Dump(
    json.RawMessage(`{"message":"Hello, World"}`),
    df.WithJSONRawMessage(),
  ),
)

// func() []byte {
//   // 00000000  48 65 6c 6c 6f 2c 20 57  6f 72 6c 64              |Hello, World|
//
//   return []byte{0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64}
// }()
fmt.Println(
  dd.Dump([]byte("Hello, World"), df.WithRichBytes()),
)

License

MIT License

Copyright (c) 2022 codehex

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Dump

func Dump(data interface{}, opts ...OptionFunc) string

Dump dumps specified data.

Types

type DumpFunc

type DumpFunc[T any] func(T, Writer)

DumpFunc is a function to dump you specified custom format.

type OptionFunc

type OptionFunc func(*options)

OptionFunc is a function for making options.

func WithDumpFunc

func WithDumpFunc[T any](f DumpFunc[T]) OptionFunc

WithDumpFunc is an option to add function for customize specified type dump string.

func WithExportedOnly

func WithExportedOnly() OptionFunc

WithExportedOnly enables to display only exported struct field. ignores unexported field.

func WithIndent

func WithIndent(indent int) OptionFunc

WithIndent adjust indent nested in any blocks. default is 2 spaces.

func WithListBreakLineSize

func WithListBreakLineSize(typ interface{}, size int) OptionFunc

WithListBreakLineSize is an option to specify the number of elements to break lines when dumped a listing (slice, array) of a given type. The number must be more than 1 otherwise treats as 1.

func WithUintFormat

func WithUintFormat(mode UintFormat) OptionFunc

WithUintFormat specify mode to display uint format. default is DecimalUint.

type UintFormat

type UintFormat int
const (
	// DecimalUint is mode to display uint as decimal format
	DecimalUint UintFormat = iota
	// BinaryUint is mode to display uint as binary format
	// The format be like 0b00000000
	BinaryUint
	// HexUint is mode to display uint as hex format
	// The format be like 0x00
	HexUint
)

type Writer

type Writer interface {
	Write(s string)
	WriteBlock(s string)
}

Writer is a writer for dump string.

Directories

Path Synopsis
cmd
internal
p

Jump to

Keyboard shortcuts

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