2018-01-11 22:19:25 +00:00
|
|
|
// Package mcfg provides a simple foundation for complex service/binary
|
|
|
|
// configuration, initialization, and destruction
|
|
|
|
package mcfg
|
|
|
|
|
|
|
|
import (
|
2019-02-05 20:18:17 +00:00
|
|
|
"context"
|
2019-01-25 22:33:36 +00:00
|
|
|
"crypto/md5"
|
|
|
|
"encoding/hex"
|
2018-01-11 22:19:25 +00:00
|
|
|
"encoding/json"
|
2019-01-25 22:33:36 +00:00
|
|
|
"fmt"
|
2019-01-08 19:21:55 +00:00
|
|
|
"sort"
|
|
|
|
|
|
|
|
"github.com/mediocregopher/mediocre-go-lib/mctx"
|
2019-01-25 03:02:04 +00:00
|
|
|
"github.com/mediocregopher/mediocre-go-lib/merr"
|
2018-01-11 22:19:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// TODO Sources:
|
|
|
|
// - JSON file
|
|
|
|
// - YAML file
|
|
|
|
|
2019-01-08 19:21:55 +00:00
|
|
|
func sortParams(params []Param) {
|
|
|
|
sort.Slice(params, func(i, j int) bool {
|
|
|
|
a, b := params[i], params[j]
|
2019-02-05 20:18:17 +00:00
|
|
|
aPath, bPath := mctx.Path(a.Context), mctx.Path(b.Context)
|
2019-01-08 19:21:55 +00:00
|
|
|
for {
|
|
|
|
switch {
|
|
|
|
case len(aPath) == 0 && len(bPath) == 0:
|
|
|
|
return a.Name < b.Name
|
|
|
|
case len(aPath) == 0 && len(bPath) > 0:
|
|
|
|
return false
|
|
|
|
case len(aPath) > 0 && len(bPath) == 0:
|
|
|
|
return true
|
|
|
|
case aPath[0] != bPath[0]:
|
|
|
|
return aPath[0] < bPath[0]
|
|
|
|
default:
|
|
|
|
aPath, bPath = aPath[1:], bPath[1:]
|
|
|
|
}
|
2018-01-11 22:19:25 +00:00
|
|
|
}
|
2019-01-08 19:21:55 +00:00
|
|
|
})
|
2018-01-11 22:19:25 +00:00
|
|
|
}
|
|
|
|
|
2019-01-08 19:21:55 +00:00
|
|
|
// returns all Params gathered by recursively retrieving them from this Context
|
|
|
|
// and its children. Returned Params are sorted according to their Path and
|
|
|
|
// Name.
|
2019-02-05 20:18:17 +00:00
|
|
|
func collectParams(ctx context.Context) []Param {
|
2019-01-08 19:21:55 +00:00
|
|
|
var params []Param
|
2018-01-11 22:19:25 +00:00
|
|
|
|
2019-02-05 20:18:17 +00:00
|
|
|
var visit func(context.Context)
|
|
|
|
visit = func(ctx context.Context) {
|
|
|
|
for _, param := range getLocalParams(ctx) {
|
2019-01-08 19:21:55 +00:00
|
|
|
params = append(params, param)
|
|
|
|
}
|
2018-05-28 03:10:57 +00:00
|
|
|
|
2019-01-08 19:21:55 +00:00
|
|
|
for _, childCtx := range mctx.Children(ctx) {
|
|
|
|
visit(childCtx)
|
|
|
|
}
|
2018-05-28 03:10:57 +00:00
|
|
|
}
|
2019-01-08 19:21:55 +00:00
|
|
|
visit(ctx)
|
2018-05-28 03:10:57 +00:00
|
|
|
|
2019-01-08 19:21:55 +00:00
|
|
|
sortParams(params)
|
|
|
|
return params
|
2018-05-28 03:10:57 +00:00
|
|
|
}
|
|
|
|
|
2019-01-25 22:33:36 +00:00
|
|
|
func paramHash(path []string, name string) string {
|
|
|
|
h := md5.New()
|
|
|
|
for _, pathEl := range path {
|
|
|
|
fmt.Fprintf(h, "pathEl:%q\n", pathEl)
|
|
|
|
}
|
|
|
|
fmt.Fprintf(h, "name:%q\n", name)
|
|
|
|
hStr := hex.EncodeToString(h.Sum(nil))
|
|
|
|
// we add the displayName to it to make debugging easier
|
|
|
|
return paramFullName(path, name) + "/" + hStr
|
|
|
|
}
|
|
|
|
|
2019-01-08 19:21:55 +00:00
|
|
|
func populate(params []Param, src Source) error {
|
2019-01-11 22:44:53 +00:00
|
|
|
if src == nil {
|
2019-01-25 22:33:36 +00:00
|
|
|
src = ParamValues(nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// map Params to their hash, so we can match them to their ParamValues
|
|
|
|
// later. There should not be any duplicates here.
|
|
|
|
pM := map[string]Param{}
|
|
|
|
for _, p := range params {
|
2019-02-05 20:18:17 +00:00
|
|
|
path := mctx.Path(p.Context)
|
|
|
|
hash := paramHash(path, p.Name)
|
2019-01-25 22:33:36 +00:00
|
|
|
if _, ok := pM[hash]; ok {
|
2019-02-05 20:18:17 +00:00
|
|
|
panic("duplicate Param: " + paramFullName(path, p.Name))
|
2019-01-25 22:33:36 +00:00
|
|
|
}
|
|
|
|
pM[hash] = p
|
2019-01-11 22:44:53 +00:00
|
|
|
}
|
|
|
|
|
2019-01-08 19:21:55 +00:00
|
|
|
pvs, err := src.Parse(params)
|
2018-08-14 01:02:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-01-11 22:19:25 +00:00
|
|
|
}
|
|
|
|
|
2018-08-14 00:05:22 +00:00
|
|
|
// dedupe the ParamValues based on their hashes, with the last ParamValue
|
2019-01-25 22:33:36 +00:00
|
|
|
// taking precedence. Also filter out those with no corresponding Param.
|
2018-01-11 22:19:25 +00:00
|
|
|
pvM := map[string]ParamValue{}
|
|
|
|
for _, pv := range pvs {
|
2019-01-25 22:33:36 +00:00
|
|
|
hash := paramHash(pv.Path, pv.Name)
|
|
|
|
if _, ok := pM[hash]; !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
pvM[hash] = pv
|
2018-01-11 22:19:25 +00:00
|
|
|
}
|
|
|
|
|
2018-08-14 00:05:22 +00:00
|
|
|
// check for required params
|
2019-01-25 22:33:36 +00:00
|
|
|
for hash, p := range pM {
|
|
|
|
if !p.Required {
|
2018-08-14 00:05:22 +00:00
|
|
|
continue
|
2019-01-25 22:33:36 +00:00
|
|
|
} else if _, ok := pvM[hash]; !ok {
|
|
|
|
return merr.New("required parameter is not set",
|
2019-02-05 20:18:17 +00:00
|
|
|
"param", paramFullName(mctx.Path(p.Context), p.Name))
|
2018-01-11 22:19:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-25 22:33:36 +00:00
|
|
|
// do the actual populating
|
|
|
|
for hash, pv := range pvM {
|
|
|
|
// at this point, all ParamValues in pvM have a corresponding pM Param
|
|
|
|
p := pM[hash]
|
|
|
|
if err := json.Unmarshal(pv.Value, p.Into); err != nil {
|
2018-01-11 22:19:25 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2018-02-15 22:46:32 +00:00
|
|
|
|
2019-01-08 19:21:55 +00:00
|
|
|
return nil
|
2018-05-28 02:39:56 +00:00
|
|
|
}
|
|
|
|
|
2019-01-08 19:21:55 +00:00
|
|
|
// Populate uses the Source to populate the values of all Params which were
|
2019-02-05 20:18:17 +00:00
|
|
|
// added to the given Context, and all of its children. Populate may be called
|
|
|
|
// multiple times with the same Context, each time will only affect the values
|
|
|
|
// of the Params which were provided by the respective Source.
|
2019-01-11 22:44:53 +00:00
|
|
|
//
|
|
|
|
// Source may be nil to indicate that no configuration is provided. Only default
|
|
|
|
// values will be used, and if any paramaters are required this will error.
|
2019-02-05 20:18:17 +00:00
|
|
|
func Populate(ctx context.Context, src Source) error {
|
2019-01-08 19:21:55 +00:00
|
|
|
return populate(collectParams(ctx), src)
|
2018-01-11 22:19:25 +00:00
|
|
|
}
|