Skip to content

Commit

Permalink
[FAB-5804] BCCSP yaml parsing in peer weakly-typed
Browse files Browse the repository at this point in the history
Fix BCCSP yaml parsing to be weakly-typed. Found this when
BCCSP:
  PKCS11:
     SoftwareVerify: true #<< would not parse bool

Change-Id: I14644000ecf59fabe9782c550cca8af61315f126
Signed-off-by: Volodymyr Paprotski <[email protected]>
Signed-off-by: Gari Singh <[email protected]>
  • Loading branch information
Volodymyr Paprotski authored and mastersingh24 committed Oct 3, 2017
1 parent 3069430 commit 16c40e5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 27 deletions.
24 changes: 10 additions & 14 deletions common/viperutil/config_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
/*
Copyright IBM Corp. 2016 All Rights Reserved.
Copyright IBM Corp. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
SPDX-License-Identifier: Apache-2.0
*/

package viperutil
Expand Down Expand Up @@ -439,7 +429,8 @@ func TestStringFromFileEnv(t *testing.T) {

func TestEnhancedExactUnmarshalKey(t *testing.T) {
type Nested struct {
Key string
Key string
BoolVar bool
}

type nestedKey struct {
Expand All @@ -450,7 +441,8 @@ func TestEnhancedExactUnmarshalKey(t *testing.T) {
"Top:\n" +
" Nested:\n" +
" Nested:\n" +
" Key: BAD\n"
" Key: BAD\n" +
" BoolVar: true\n"

envVar := "VIPERUTIL_TOP_NESTED_NESTED_KEY"
envVal := "GOOD"
Expand All @@ -477,4 +469,8 @@ func TestEnhancedExactUnmarshalKey(t *testing.T) {
t.Fatalf(`Expected: "%s", Actual: "%s"`, envVal, uconf.Nested.Key)
}

if uconf.Nested.BoolVar != true {
t.Fatalf(`Expected: "%t", Actual: "%t"`, true, uconf.Nested.BoolVar)
}

}
28 changes: 15 additions & 13 deletions common/viperutil/config_util.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
/*
Copyright IBM Corp. 2016 All Rights Reserved.
Copyright IBM Corp. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
SPDX-License-Identifier: Apache-2.0
*/

package viperutil
Expand Down Expand Up @@ -321,5 +311,17 @@ func EnhancedExactUnmarshalKey(baseKey string, output interface{}) error {
leafKeys := getKeysRecursively("", viper.Get, m)

logger.Debugf("%+v", leafKeys)
return mapstructure.Decode(leafKeys[baseKey], output)

config := &mapstructure.DecoderConfig{
Metadata: nil,
Result: output,
WeaklyTypedInput: true,
}

decoder, err := mapstructure.NewDecoder(config)
if err != nil {
return err
}

return decoder.Decode(leafKeys[baseKey])
}

0 comments on commit 16c40e5

Please sign in to comment.