Skip to content

fsamin/go-dump

Repository files navigation

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