/
ffyaml.go
38 lines (31 loc) · 1.01 KB
/
ffyaml.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Package ffyaml provides a YAML config file parser.
package ffyaml
import (
"errors"
"io"
"github.com/peterbourgon/ff/v4/internal/ffdata"
"gopkg.in/yaml.v2"
)
// Parse is a helper function that uses a default parser.
func Parse(r io.Reader, set func(name, value string) error) error {
return (&Parser{}).Parse(r, set)
}
// Parser collects parameters for the YAML config file parser.
type Parser struct {
// Delimiter is used when concatenating nested node keys into a flag name.
// The default delimiter is ".".
Delimiter string
}
// Parse a YAML document from the provided io.Reader, using the provided set
// function to set flag values. Flag names are derived from the node names and
// their key/value pairs.
func (p Parser) Parse(r io.Reader, set func(name, value string) error) error {
if p.Delimiter == "" {
p.Delimiter = "."
}
var m map[string]interface{}
if err := yaml.NewDecoder(r).Decode(&m); err != nil && !errors.Is(err, io.EOF) {
return err
}
return ffdata.TraverseMap(m, p.Delimiter, set)
}