Skip to content

Commit

Permalink
[config] Merge vars into data #2
Browse files Browse the repository at this point in the history
- keys inside `vars` are merged while other top level keys are merged
  inside directly, i.e. `vars: a:1  db:jack --- vars: b:1 db: marry`
would result in `var: a:1 b:1 db: marry`
  • Loading branch information
at15 committed Mar 30, 2017
1 parent 8753696 commit f3bb2f5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 27 deletions.
37 changes: 14 additions & 23 deletions config/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"sync"

"fmt"
"reflect"

"strings"
Expand Down Expand Up @@ -82,9 +81,8 @@ func (c *YAMLConfig) ParseSingleDocumentBytes(doc []byte) error {
return errors.Wrap(err, "can't render template with previous documents' vars")
}

// TODO: need special flag/tag for this logging
fmt.Printf("01-before\n%s", doc)
fmt.Printf("01-after\n%s", rendered)
log.Debugf("01-before\n%s", doc)
log.Debugf("01-after\n%s", rendered)

tmpData := make(map[string]interface{})
err = yaml.Unmarshal(rendered, &tmpData)
Expand All @@ -111,8 +109,8 @@ func (c *YAMLConfig) ParseSingleDocumentBytes(doc []byte) error {
return errors.Wrap(err, "can't render template with vars in current document")
}

fmt.Printf("02-before\n%s", doc)
fmt.Printf("02-after\n%s", rendered)
log.Debugf("02-before\n%s", doc)
log.Debugf("02-after\n%s", rendered)

tmpData = make(map[string]interface{})
err = yaml.Unmarshal(rendered, &tmpData)
Expand All @@ -125,6 +123,8 @@ func (c *YAMLConfig) ParseSingleDocumentBytes(doc []byte) error {
for k, v := range tmpData {
c.data[k] = v
}
// NOTE: vars are merged instead of overwritten like other top level keys
c.data["vars"] = c.vars
return nil
}

Expand All @@ -148,36 +148,27 @@ func searchMap(src map[string]interface{}, path []string) (interface{}, error) {
return result, errors.New("path is empty, at least provide one segment")
}
result = src
previousPath := ""
for i := 0; i < len(path); i++ {
key := path[i]
log.Debug(key)
log.Debug(result)
//m, ok := result.(map[string]interface{})
//m, ok := result.(map[interface{}]interface{})
//if !ok {
// m, ok = result.(map[string]interface{})
//}
//if !ok {
// // TODO: point out where did we go wrong
// return result, errors.Errorf("%v is not a map but %v", result, reflect.TypeOf(result))
//}

//log.Debug(key)
//log.Debug(result)
if reflect.TypeOf(result).Kind() != reflect.Map {
return nil, errors.Errorf("%v is not a map but %v", result, reflect.TypeOf(result))
return nil, errors.Errorf("%s is not a map but %s, %v", previousPath, reflect.TypeOf(result), result)
}
m, ok := result.(map[string]interface{})
if !ok {
m = cast.ToStringMap(result.(map[interface{}]interface{}))
}
// FIXED: this is a tricky problem, if you use `:` here, you create a new local variable instead update the one
// outside the loop
// outside the loop, that's all the Debug(result) for
//result, ok := m[key]
result, ok = m[key]
if !ok {
return result, errors.Errorf("key: %s does not exists in %v", key, m)
return result, errors.Errorf("key: %s does not exists in path: %s, val: %v", key, previousPath, m)
}
log.Debug(result)

//log.Debug(result)
previousPath += key
}
return result, nil
}
Expand Down
12 changes: 8 additions & 4 deletions config/yaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,14 @@ vars:

func TestYAMLConfig_Get(t *testing.T) {
assert := asst.New(t)
// TODO: reuse the samples instead of copy and paste
var sampleUsePreviousVars = `
vars:
influxdb_port: 8081
databases:
- influxdb
- kairosdb
foo: 1
---
vars:
kairosdb_port: 8080
Expand All @@ -130,15 +132,17 @@ vars:
name: {{ db }}
ssl: {{ vars.ssl }}
{% endfor %}
foo: 2
`
c := NewYAMLConfig()
err := c.ParseMultiDocumentBytes([]byte(sampleUsePreviousVars))
assert.Nil(err)
util.UseVerboseLog()
// FIXME: the data and vars should be merged into one object
log.Debug(c.data)
log.Debug(c.vars)
//assert.Equal(8081, c.Get("vars.influxdb_port"))
assert.Equal(8081, c.Get("vars.influxdb_port"))
assert.Equal(nil, c.Get("vars.that_does_not_exists"))
// NOTE: top level keys other than vars are overwritten instead of merged
assert.Equal(2, c.Get("foo"))
util.DisableVerboseLog()
}

func TestSearchMap(t *testing.T) {
Expand Down
6 changes: 6 additions & 0 deletions util/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,9 @@ func UseVerboseLog() {
Logger.Level = dlog.DebugLevel
log.Debug("enable debug logging")
}


func DisableVerboseLog() {
Logger.Level = dlog.InfoLevel
log.Info("disable debug logging")
}

0 comments on commit f3bb2f5

Please sign in to comment.