2018-08-13 22:44:46 +00:00
|
|
|
package mcfg
|
|
|
|
|
2018-08-14 00:05:22 +00:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
2019-06-15 22:45:53 +00:00
|
|
|
|
|
|
|
"github.com/mediocregopher/mediocre-go-lib/mcmp"
|
2018-08-14 00:05:22 +00:00
|
|
|
)
|
2018-08-13 22:44:46 +00:00
|
|
|
|
|
|
|
// ParamValue describes a value for a parameter which has been parsed by a
|
2019-01-08 19:21:55 +00:00
|
|
|
// Source.
|
2018-08-13 22:44:46 +00:00
|
|
|
type ParamValue struct {
|
2019-01-25 22:33:36 +00:00
|
|
|
Name string
|
|
|
|
Path []string
|
2018-08-13 22:44:46 +00:00
|
|
|
Value json.RawMessage
|
|
|
|
}
|
|
|
|
|
2019-04-04 18:21:44 +00:00
|
|
|
// Source parses ParamValues out of a particular configuration source, given the
|
2019-06-15 22:45:53 +00:00
|
|
|
// Component which the Params were added to (via WithInt, WithString, etc...).
|
2019-04-04 18:21:44 +00:00
|
|
|
// CollectParams can be used to retrieve these Params.
|
2019-01-25 21:46:39 +00:00
|
|
|
//
|
2019-06-15 22:45:53 +00:00
|
|
|
// It's possible for Parsing to affect the Component itself, for example in the
|
|
|
|
// case of sub-commands.
|
2019-04-04 16:52:43 +00:00
|
|
|
//
|
2019-01-25 21:46:39 +00:00
|
|
|
// Source should not return ParamValues which were not explicitly set to a value
|
|
|
|
// by the configuration source.
|
|
|
|
//
|
|
|
|
// The returned []ParamValue may contain duplicates of the same Param's value.
|
2019-06-15 22:45:53 +00:00
|
|
|
// in which case the latter value takes precedence. It may also contain
|
2019-01-25 22:33:36 +00:00
|
|
|
// ParamValues which do not correspond to any of the passed in Params. These
|
|
|
|
// will be ignored in Populate.
|
2018-08-13 22:44:46 +00:00
|
|
|
type Source interface {
|
2019-06-15 22:45:53 +00:00
|
|
|
Parse(*mcmp.Component) ([]ParamValue, error)
|
2018-08-13 22:44:46 +00:00
|
|
|
}
|
2018-08-14 00:15:54 +00:00
|
|
|
|
2019-01-25 22:33:36 +00:00
|
|
|
// ParamValues is simply a slice of ParamValue elements, which implements Parse
|
|
|
|
// by always returning itself as-is.
|
|
|
|
type ParamValues []ParamValue
|
|
|
|
|
2019-06-15 22:45:53 +00:00
|
|
|
var _ Source = ParamValues{}
|
|
|
|
|
2019-01-25 22:33:36 +00:00
|
|
|
// Parse implements the method for the Source interface.
|
2019-06-15 22:45:53 +00:00
|
|
|
func (pvs ParamValues) Parse(*mcmp.Component) ([]ParamValue, error) {
|
|
|
|
return pvs, nil
|
2019-01-25 22:33:36 +00:00
|
|
|
}
|
|
|
|
|
2018-08-14 00:15:54 +00:00
|
|
|
// Sources combines together multiple Source instances into one. It will call
|
2019-01-08 19:21:55 +00:00
|
|
|
// Parse on each element individually. Values from later Sources take precedence
|
|
|
|
// over previous ones.
|
2018-08-14 00:15:54 +00:00
|
|
|
type Sources []Source
|
|
|
|
|
2019-06-15 22:45:53 +00:00
|
|
|
var _ Source = Sources{}
|
|
|
|
|
2018-08-14 00:15:54 +00:00
|
|
|
// Parse implements the method for the Source interface.
|
2019-06-15 22:45:53 +00:00
|
|
|
func (ss Sources) Parse(cmp *mcmp.Component) ([]ParamValue, error) {
|
2018-08-14 00:15:54 +00:00
|
|
|
var pvs []ParamValue
|
|
|
|
for _, s := range ss {
|
2019-04-04 16:52:43 +00:00
|
|
|
var innerPVs []ParamValue
|
|
|
|
var err error
|
2019-06-15 22:45:53 +00:00
|
|
|
if innerPVs, err = s.Parse(cmp); err != nil {
|
|
|
|
return nil, err
|
2018-08-14 00:15:54 +00:00
|
|
|
}
|
|
|
|
pvs = append(pvs, innerPVs...)
|
|
|
|
}
|
2019-06-15 22:45:53 +00:00
|
|
|
return pvs, nil
|
2018-08-14 00:15:54 +00:00
|
|
|
}
|