diff --git a/config/yaml.go b/config/yaml.go index e03ccab..337bd9e 100644 --- a/config/yaml.go +++ b/config/yaml.go @@ -4,7 +4,6 @@ import ( "bytes" "sync" - "fmt" "reflect" "strings" @@ -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) @@ -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) @@ -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 } @@ -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 } diff --git a/config/yaml_test.go b/config/yaml_test.go index 1c60131..af2ff86 100644 --- a/config/yaml_test.go +++ b/config/yaml_test.go @@ -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 @@ -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) { diff --git a/util/log.go b/util/log.go index 156cbb4..1dd48ae 100644 --- a/util/log.go +++ b/util/log.go @@ -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") +} \ No newline at end of file