config: github.com/gookit/config/toml Index | Examples | Files

package toml

import "github.com/gookit/config/toml"

Package toml is driver use TOML format content as config source

Usage please see example:

Code:

config.WithOptions(config.ParseEnv)

// add Decoder and Encoder
config.AddDriver(Driver)

err := config.LoadFiles("testdata/toml_base.toml")
if err != nil {
    panic(err)
}

// fmt.Printf("config data: \n %#v\n", Data())

// load more files
err = config.LoadFiles("testdata/toml_other.toml")
// can also
// config.LoadFiles("testdata/toml_base.toml", "testdata/toml_other.toml")
if err != nil {
    panic(err)
}

// load from string
_ = config.LoadSources(config.Toml, []byte(tomlStr))

// fmt.Printf("config data: \n %#v\n", Data())
fmt.Print("get config example:\n")

name := config.String("name")
fmt.Printf("get string\n - val: %v\n", name)

arr1 := config.Strings("arr1")
fmt.Printf("get array\n - val: %#v\n", arr1)

val0 := config.String("arr1.0")
fmt.Printf("get sub-value by path 'arr.index'\n - val: %v\n", val0)

map1 := config.StringMap("map1")
fmt.Printf("get map\n - val: %#v\n", map1)

val0 = config.String("map1.name")
fmt.Printf("get sub-value by path 'map.key'\n - val: %v\n", val0)

// can parse env name(ParseEnv: true)
fmt.Printf("get env 'envKey' val: %s\n", config.String("envKey", ""))
fmt.Printf("get env 'envKey1' val: %s\n", config.String("envKey1", ""))

// Out:
// get config example:
// get string
// - val: app2
// get array
// - val: []string{"alpha", "omega"}
// get sub-value by path 'arr.index'
// - val: alpha
// get map
// - val: map[string]string{"name":"inhere", "org":"GitHub"}
// get sub-value by path 'map.key'
// - val: inhere
// get env 'envKey' val: /bin/zsh
// get env 'envKey1' val: defValue

Index

Examples

Package Files

toml.go

Variables

var Decoder config.Decoder = func(blob []byte, ptr interface{}) (err error) {
    _, err = toml.Decode(string(blob), ptr)

    return
}

Decoder the toml content decoder

var Driver = &tomlDriver{config.Toml}

Driver for toml

var Encoder config.Encoder = func(ptr interface{}) (out []byte, err error) {
    buf := new(bytes.Buffer)

    err = toml.NewEncoder(buf).Encode(ptr)
    if err != nil {
        return
    }

    return buf.Bytes(), nil
}

Encoder the toml content encoder

Package toml imports 3 packages (graph). Updated 2019-06-12. Refresh now. Tools for package owners.