Skip to content

Commit

Permalink
Fixes FAB-918
Browse files Browse the repository at this point in the history
When processing environment variable overrides, Viper may pass back objects
that satisfy the map[string]interface{} type, which does not match the
map[interface{}]interface{} type. This change fixes the issue.

I don't fully understand why the current code does not work, but the modified
code does seem to work and there is a precedent in the Viper code at viper.go,
line 358.

Change-Id: Ibdf2ac43632a897113096de293c7340c09627aea
Signed-off-by: Bishop Brock <bcbrock@us.ibm.com>
  • Loading branch information
bcbrock committed Nov 1, 2016
1 parent 18a44d0 commit 295cc28
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
21 changes: 15 additions & 6 deletions orderer/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"reflect"
"strings"
"testing"
"time"

"github.com/spf13/viper"
)
Expand Down Expand Up @@ -97,17 +98,25 @@ func TestEnvSlice(t *testing.T) {
// a bug in the original viper implementation that is worked around in
// the Load codepath for now
func TestEnvInnerVar(t *testing.T) {
envVar := "ORDERER_GENERAL_LISTENPORT"
envVal := uint16(80)
os.Setenv(envVar, fmt.Sprintf("%d", envVal))
defer os.Unsetenv(envVar)
envVar1 := "ORDERER_GENERAL_LISTENPORT"
envVal1 := uint16(80)
envVar2 := "ORDERER_KAFKA_RETRY_PERIOD"
envVal2 := "42s"
os.Setenv(envVar1, fmt.Sprintf("%d", envVal1))
os.Setenv(envVar2, envVal2)
defer os.Unsetenv(envVar1)
defer os.Unsetenv(envVar2)
config := Load()

if config == nil {
t.Fatalf("Could not load config")
}

if config.General.ListenPort != envVal {
t.Fatalf("Environmental override of inner config did not work")
if config.General.ListenPort != envVal1 {
t.Fatalf("Environmental override of inner config test 1 did not work")
}
v2, _ := time.ParseDuration(envVal2)
if config.Kafka.Retry.Period != v2 {
t.Fatalf("Environmental override of inner config test 2 did not work")
}
}
5 changes: 4 additions & 1 deletion orderer/config/config_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func getKeysRecursively(base string, v *viper.Viper, nodeKeys map[string]interfa
fqKey := base + key
val := v.Get(fqKey)
if m, ok := val.(map[interface{}]interface{}); ok {
logger.Debugf("Found map value for %s", fqKey)
logger.Debugf("Found map[interface{}]interface{} value for %s", fqKey)
tmp := make(map[string]interface{})
for ik, iv := range m {
cik, ok := ik.(string)
Expand All @@ -41,6 +41,9 @@ func getKeysRecursively(base string, v *viper.Viper, nodeKeys map[string]interfa
tmp[cik] = iv
}
result[key] = getKeysRecursively(fqKey+".", v, tmp)
} else if m, ok := val.(map[string]interface{}); ok {
logger.Debugf("Found map[string]interface{} value for %s", fqKey)
result[key] = getKeysRecursively(fqKey+".", v, m)
} else {
logger.Debugf("Found real value for %s setting to %T %v", fqKey, val, val)
result[key] = val
Expand Down

0 comments on commit 295cc28

Please sign in to comment.