dump

package module
v1.8.0 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2022 License: Apache-2.0 Imports: 9 Imported by: 29

README

Go-Dump

Go-Dump is a package which helps you to dump a struct to SdtOut, any io.Writer, or a map[string]string.

GoDoc

Sample usage

type T struct {
    A int
    B string
}

a := T{23, "foo bar"}

dump.Fdump(out, a)

Will print

T.A: 23
T.B: foo bar

Usage with a map

type T struct {
    A int
    B string
}

a := T{23, "foo bar"}

m, _ := dump.ToMap(a)

Will return such a map:

KEY Value
T.A 23
T.B foo bar

Formatting keys

    dump.ToMap(a, dump.WithDefaultLowerCaseFormatter())

Using go-dump to manage environment variables and using spf13/viper

    
    type MyStruct struct {
        A string
        B struct {
            InsideB string
        }
    }

    var myStruct MyStruct
    myStruct.A = "value A"
    myStruct.B.InsideB = "value B"
    

    dumper := dump.NewDefaultEncoder()
    dumper.DisableTypePrefix = true
    dumper.Separator = "_"
    dumper.Prefix = "MYSTRUCT"
    dumper.Formatters = []dump.KeyFormatterFunc{dump.WithDefaultUpperCaseFormatter()}

    envs, _ := dumper.ToStringMap(&myStruct) // envs is the map of the dumped MyStruct 

Will return such a map:

KEY Value
MYSTRUCT_A value A
MYSTRUCT_B_INSIDEB value B

The environement variables can be handled by viper spf13/viper.

    ...
    for k := range envs {
        viper.BindEnv(dumper.ViperKey(k), k)
    }
    
    ...

    viperSettings := viper.AllSettings()
    for k, v := range viperSettings {
        fmt.Println(k, v)
    }
    ...

More examples

See unit tests for more examples.

Dependencies

Go-Dump needs Go >= 1.8

No external dependencies :)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Dump

func Dump(i interface{}, formatters ...KeyFormatterFunc) error

Dump displays the passed parameter properties to standard out such as complete types and all pointer addresses used to indirect to the final value. See Fdump if you would prefer dumping to an arbitrary io.Writer or Sdump to get the formatted result as a string.

func Fdump

func Fdump(w io.Writer, i interface{}, formatters ...KeyFormatterFunc) error

Fdump formats and displays the passed arguments to io.Writer w. It formats exactly the same as Dump.

func MustSdump

func MustSdump(i interface{}, formatters ...KeyFormatterFunc) string

MustSdump is a helper that wraps a call to a function returning (string, error) and panics if the error is non-nil.

func Sdump

func Sdump(i interface{}, formatters ...KeyFormatterFunc) (string, error)

Sdump returns a string with the passed arguments formatted exactly the same as Dump.

func ToMap

func ToMap(i interface{}, formatters ...KeyFormatterFunc) (map[string]interface{}, error)

ToMap dumps argument as a map[string]interface{}

func ToStringMap

func ToStringMap(i interface{}, formatters ...KeyFormatterFunc) (map[string]string, error)

ToStringMap formats the argument as a map[string]string. It formats exactly the same as Dump.

Types

type Encoder

type Encoder struct {
	Formatters  []KeyFormatterFunc
	ExtraFields struct {
		Len            bool
		Type           bool
		DetailedStruct bool
		DetailedMap    bool
		DetailedArray  bool
		DeepJSON       bool
		UseJSONTag     bool
	}
	ArrayJSONNotation bool
	Separator         string
	DisableTypePrefix bool
	Prefix            string
	// contains filtered or unexported fields
}

Encoder ensures all options to dump an object

func NewDefaultEncoder

func NewDefaultEncoder() *Encoder

NewDefaultEncoder instanciate a go-dump encoder

func NewEncoder added in v1.0.5

func NewEncoder(w io.Writer) *Encoder

NewEncoder instanciate a go-dump encoder over the writer

func (*Encoder) Fdump

func (e *Encoder) Fdump(i interface{}) (err error)

Fdump formats and displays the passed arguments to io.Writer w. It formats exactly the same as Dump.

func (*Encoder) Sdump

func (e *Encoder) Sdump(i interface{}) (string, error)

Sdump returns a string with the passed arguments formatted exactly the same as Dump.

func (*Encoder) ToMap

func (e *Encoder) ToMap(i interface{}) (res map[string]interface{}, err error)

ToMap dumps argument as a map[string]interface{}

func (*Encoder) ToStringMap

func (e *Encoder) ToStringMap(i interface{}) (res map[string]string, err error)

ToStringMap formats the argument as a map[string]string. It formats exactly the same as Dump.

func (*Encoder) ViperKey added in v1.0.6

func (e *Encoder) ViperKey(s string) string

type KeyFormatterFunc

type KeyFormatterFunc func(s string, level int) string

KeyFormatterFunc is a type for key formatting

func NoFormatter

func NoFormatter() KeyFormatterFunc

NoFormatter doesn't do anything, so to be sure to avoid keys formatting, use only this formatter

func WithDefaultFormatter

func WithDefaultFormatter() KeyFormatterFunc

WithDefaultFormatter is the default formatter

func WithDefaultLowerCaseFormatter

func WithDefaultLowerCaseFormatter() KeyFormatterFunc

WithDefaultLowerCaseFormatter formats keys in lowercase and apply default formatting

func WithDefaultUpperCaseFormatter added in v1.0.5

func WithDefaultUpperCaseFormatter() KeyFormatterFunc

WithDefaultUpperCaseFormatter formats keys in uppercase and apply default formatting

func WithLowerCaseFormatter

func WithLowerCaseFormatter() KeyFormatterFunc

WithLowerCaseFormatter formats keys in lowercase

Jump to

Keyboard shortcuts

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