Categorygithub.com/fsamin/go-dump
modulepackage
1.8.0
Repository: https://github.com/fsamin/go-dump.git
Documentation: pkg.go.dev

# 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:

KEYValue
T.A23
T.Bfoo 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:

KEYValue
MYSTRUCT_Avalue A
MYSTRUCT_B_INSIDEBvalue 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 :)

# Functions

Dump displays the passed parameter properties to standard out such as complete types and all pointer addresses used to indirect to the final value.
Fdump formats and displays the passed arguments to io.Writer w.
MustSdump is a helper that wraps a call to a function returning (string, error) and panics if the error is non-nil.
NewDefaultEncoder instanciate a go-dump encoder.
NewEncoder instanciate a go-dump encoder over the writer.
NoFormatter doesn't do anything, so to be sure to avoid keys formatting, use only this formatter.
Sdump returns a string with the passed arguments formatted exactly the same as Dump.
ToMap dumps argument as a map[string]interface{}.
ToStringMap formats the argument as a map[string]string.
WithDefaultFormatter is the default formatter.
WithDefaultLowerCaseFormatter formats keys in lowercase and apply default formatting.
WithDefaultUpperCaseFormatter formats keys in uppercase and apply default formatting.
WithLowerCaseFormatter formats keys in lowercase.

# Structs

Encoder ensures all options to dump an object.

# Type aliases

KeyFormatterFunc is a type for key formatting.