Skip to content

Commit 295cc28

Browse files
author
bcbrock
committed
Fixes FAB-918
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 <[email protected]>
1 parent 18a44d0 commit 295cc28

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

orderer/config/config_test.go

+15-6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"reflect"
2424
"strings"
2525
"testing"
26+
"time"
2627

2728
"github.com/spf13/viper"
2829
)
@@ -97,17 +98,25 @@ func TestEnvSlice(t *testing.T) {
9798
// a bug in the original viper implementation that is worked around in
9899
// the Load codepath for now
99100
func TestEnvInnerVar(t *testing.T) {
100-
envVar := "ORDERER_GENERAL_LISTENPORT"
101-
envVal := uint16(80)
102-
os.Setenv(envVar, fmt.Sprintf("%d", envVal))
103-
defer os.Unsetenv(envVar)
101+
envVar1 := "ORDERER_GENERAL_LISTENPORT"
102+
envVal1 := uint16(80)
103+
envVar2 := "ORDERER_KAFKA_RETRY_PERIOD"
104+
envVal2 := "42s"
105+
os.Setenv(envVar1, fmt.Sprintf("%d", envVal1))
106+
os.Setenv(envVar2, envVal2)
107+
defer os.Unsetenv(envVar1)
108+
defer os.Unsetenv(envVar2)
104109
config := Load()
105110

106111
if config == nil {
107112
t.Fatalf("Could not load config")
108113
}
109114

110-
if config.General.ListenPort != envVal {
111-
t.Fatalf("Environmental override of inner config did not work")
115+
if config.General.ListenPort != envVal1 {
116+
t.Fatalf("Environmental override of inner config test 1 did not work")
117+
}
118+
v2, _ := time.ParseDuration(envVal2)
119+
if config.Kafka.Retry.Period != v2 {
120+
t.Fatalf("Environmental override of inner config test 2 did not work")
112121
}
113122
}

orderer/config/config_util.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func getKeysRecursively(base string, v *viper.Viper, nodeKeys map[string]interfa
3131
fqKey := base + key
3232
val := v.Get(fqKey)
3333
if m, ok := val.(map[interface{}]interface{}); ok {
34-
logger.Debugf("Found map value for %s", fqKey)
34+
logger.Debugf("Found map[interface{}]interface{} value for %s", fqKey)
3535
tmp := make(map[string]interface{})
3636
for ik, iv := range m {
3737
cik, ok := ik.(string)
@@ -41,6 +41,9 @@ func getKeysRecursively(base string, v *viper.Viper, nodeKeys map[string]interfa
4141
tmp[cik] = iv
4242
}
4343
result[key] = getKeysRecursively(fqKey+".", v, tmp)
44+
} else if m, ok := val.(map[string]interface{}); ok {
45+
logger.Debugf("Found map[string]interface{} value for %s", fqKey)
46+
result[key] = getKeysRecursively(fqKey+".", v, m)
4447
} else {
4548
logger.Debugf("Found real value for %s setting to %T %v", fqKey, val, val)
4649
result[key] = val

0 commit comments

Comments
 (0)