mcfg: shuffle some code around so there's a common way of retrieving all ParamValues out of a Cfg

This commit is contained in:
Brian Picciano 2018-08-13 18:44:46 -04:00
parent 715b6c9491
commit 8084268bbf
3 changed files with 37 additions and 35 deletions

View File

@ -122,31 +122,14 @@ func (cli SourceCLI) Parse(cfg *Cfg) ([]ParamValue, error) {
func (cli SourceCLI) cliParamVals(cfg *Cfg) (map[string]ParamValue, error) {
m := map[string]ParamValue{}
for _, param := range cfg.Params {
for _, pv := range cfg.allParamValues() {
key := cliKeyPrefix
if !cfg.IsRoot() {
key += strings.Join(cfg.Path, cliKeyJoin) + cliKeyJoin
}
key += param.Name
m[key] = ParamValue{
Param: param,
Path: cfg.Path,
}
}
for _, child := range cfg.Children {
childM, err := cli.cliParamVals(child)
if err != nil {
return nil, err
}
for key, pv := range childM {
if _, ok := m[key]; ok {
return nil, fmt.Errorf("multiple params use the same CLI arg %q", key)
if len(pv.Path) > 0 {
key += strings.Join(pv.Path, cliKeyJoin) + cliKeyJoin
}
key += pv.Param.Name
m[key] = pv
}
}
return m, nil
}

View File

@ -62,19 +62,6 @@ func (h *Hook) Also(h2 Hook) {
}
}
// ParamValue describes a value for a parameter which has been parsed by a
// Source
type ParamValue struct {
Param
Path []string // nil if root
Value json.RawMessage
}
// Source parses ParamValues out of a particular configuration source
type Source interface {
Parse(*Cfg) ([]ParamValue, error)
}
// Cfg describes a set of configuration parameters and run-time behaviors.
// Parameters are defined using the Param* methods, and run-time behaviors by
// the Hook fields on this struct.

32
mcfg/source.go Normal file
View File

@ -0,0 +1,32 @@
package mcfg
import "encoding/json"
// ParamValue describes a value for a parameter which has been parsed by a
// Source
type ParamValue struct {
Param
Path []string // nil if root
Value json.RawMessage
}
func (cfg *Cfg) allParamValues() []ParamValue {
pvs := make([]ParamValue, 0, len(cfg.Params))
for _, param := range cfg.Params {
pvs = append(pvs, ParamValue{
Param: param,
Path: cfg.Path,
})
}
for _, child := range cfg.Children {
pvs = append(pvs, child.allParamValues()...)
}
return pvs
}
// Source parses ParamValues out of a particular configuration source. The
// returned []ParamValue may contain duplicates of the same Param's value.
type Source interface {
Parse(*Cfg) ([]ParamValue, error)
}