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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ | |
package zanzibar | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"reflect" | ||
|
@@ -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)) | ||
|
@@ -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]) | ||
|
@@ -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{}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: |
||
} | ||
|
||
// 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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 instringizeMapKey
? Since it passes tests, does that make the[]interface{}
case dead code if doesn't occur?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
object
is declared of typemap[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.