This parser loads yaml
formatted configuration from an io.ReadCloser
.
Click the Playground badge above to see the example running in the Go Playground.
package main
import (
"fmt"
"io/ioutil"
"go.krak3n.codes/gofig"
"go.krak3n.codes/gofig/parsers/yaml"
)
// Config is a struct to unpack configuration into.
type Config struct {
Foo struct {
Bar struct {
Baz string `gofig:"baz"`
} `gofig:"bar"`
} `gofig:"foo"`
Fizz struct {
Buzz map[string]string `gofig:"buzz"`
} `gofig:"fizz"`
A struct {
B map[string][]int `gofig:"b"`
} `gofig:"a"`
C struct {
D map[string]map[string][]int `gofig:"d"`
} `gofig:"c"`
}
const blob string = `
foo:
bar:
baz: bar
fizz:
buzz:
hello: world
bill: ben`
func main() {
var cfg Config
// Initialise gofig with the struct config values will be placed into
gfg, err := gofig.New(&cfg)
gofig.Must(err)
// Create a parser
parser := yaml.New()
// write some data to a config file
path, err := create()
gofig.Must(err)
// Parse in order
gofig.Must(gfg.Parse(
gofig.FromFile(parser, path),
gofig.FromString(parser, blob)))
fmt.Println(fmt.Sprintf("%+v", cfg))
}
const contents = `
a:
b:
c: [1,2,3]
c:
d:
e:
f: [1,2,3]`
func create() (string, error) {
f, err := ioutil.TempFile("", "yaml")
if err != nil {
return "", err
}
if _, err := f.Write([]byte(contents)); err != nil {
return "", err
}
return f.Name(), nil
}