Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix static config inspection #496

Merged
merged 1 commit into from Oct 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 27 additions & 4 deletions runtime/static_config.go
Expand Up @@ -21,6 +21,7 @@
package zanzibar

import (
"fmt"
"io/ioutil"
"os"
"reflect"
Expand Down Expand Up @@ -232,7 +233,7 @@ func (conf *StaticConfig) MustGetStruct(key string, ptr interface{}) {
}

if v, contains := conf.configValues[key]; contains {
if value, ok := v.(map[interface{}]interface{}); ok {
if value, ok := v.(map[string]interface{}); ok {
err := mapstructure.Decode(value, ptr)
if err != nil {
panic(errors.Errorf("Decoding key (%s) failed", key))
Expand Down Expand Up @@ -340,7 +341,7 @@ func (conf *StaticConfig) initializeConfigValues() {
}

func (conf *StaticConfig) collectConfigMaps() []map[string]interface{} {
var maps = []map[string]interface{}{}
var maps = make([]map[string]interface{}, len(conf.configOptions))

for i := 0; i < len(conf.configOptions); i++ {
fileObject := conf.parseFile(conf.configOptions[i])
Expand Down Expand Up @@ -389,10 +390,32 @@ func (conf *StaticConfig) parseFile(
))
}

var object map[string]interface{}
var object map[interface{}]interface{}
err := yaml.Unmarshal(bytes, &object)
if err != nil {
panic(err)
}
return object
return stringizeMapKey(object).(map[string]interface{})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this type assertion fail if you take the []interface{} case in stringizeMapKey? Since it passes tests, does that make the []interface{} case dead code if doesn't occur?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

object is declared of type map[interface{}]interface{}, so the conversion should not fail. The []interface{} case is for when the value is a slice of maps where the keys are also interface{} and need to be stringized.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: stringizeMapKey ---> stringifyMapKey

}

// yaml.Unmarshal deserializes nested maps to map[interface{}]interface{}, which is
// unsupported type for json.Marshal, we need to convert the map key to string
// so that the object can be serialized by both yaml and json marshalers.
func stringizeMapKey(in interface{}) interface{} {
switch v := in.(type) {
case map[interface{}]interface{}:
ret := make(map[string]interface{}, len(v))
for key, value := range v {
ret[fmt.Sprintf("%v", key)] = stringizeMapKey(value)
}
return ret
case []interface{}:
ret := make([]interface{}, len(v))
for i, value := range v {
ret[i] = stringizeMapKey(value)
}
return ret
default:
return v
}
}
2 changes: 1 addition & 1 deletion runtime/static_config_test.go
Expand Up @@ -818,7 +818,7 @@ func TestInspectFromFile(t *testing.T) {
"bool": true,
"float": float64(1.2),
"int": int(1),
"struct": map[interface{}]interface{}{
"struct": map[string]interface{}{
"Field": "a",
"Foo": int(4),
},
Expand Down