Skip to content

Commit 27b1648

Browse files
author
Volodymyr Paprotski
committed
[FAB-5804] BCCSP yaml parsing in peer weakly-typed
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 <vpaprots@ca.ibm.com>
1 parent a657db2 commit 27b1648

File tree

2 files changed

+25
-27
lines changed

2 files changed

+25
-27
lines changed

common/viperutil/config_test.go

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,7 @@
11
/*
2-
Copyright IBM Corp. 2016 All Rights Reserved.
2+
Copyright IBM Corp. All Rights Reserved.
33
4-
Licensed under the Apache License, Version 2.0 (the "License");
5-
you may not use this file except in compliance with the License.
6-
You may obtain a copy of the License at
7-
8-
http://www.apache.org/licenses/LICENSE-2.0
9-
10-
Unless required by applicable law or agreed to in writing, software
11-
distributed under the License is distributed on an "AS IS" BASIS,
12-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
See the License for the specific language governing permissions and
14-
limitations under the License.
4+
SPDX-License-Identifier: Apache-2.0
155
*/
166

177
package viperutil
@@ -440,7 +430,8 @@ func TestStringFromFileEnv(t *testing.T) {
440430

441431
func TestEnhancedExactUnmarshalKey(t *testing.T) {
442432
type Nested struct {
443-
Key string
433+
Key string
434+
BoolVar bool
444435
}
445436

446437
type nestedKey struct {
@@ -451,7 +442,8 @@ func TestEnhancedExactUnmarshalKey(t *testing.T) {
451442
"Top:\n" +
452443
" Nested:\n" +
453444
" Nested:\n" +
454-
" Key: BAD\n"
445+
" Key: BAD\n" +
446+
" BoolVar: true\n"
455447

456448
envVar := "VIPERUTIL_TOP_NESTED_NESTED_KEY"
457449
envVal := "GOOD"
@@ -478,4 +470,8 @@ func TestEnhancedExactUnmarshalKey(t *testing.T) {
478470
t.Fatalf(`Expected: "%s", Actual: "%s"`, envVal, uconf.Nested.Key)
479471
}
480472

473+
if uconf.Nested.BoolVar != true {
474+
t.Fatalf(`Expected: "%t", Actual: "%t"`, true, uconf.Nested.BoolVar)
475+
}
476+
481477
}

common/viperutil/config_util.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,7 @@
11
/*
2-
Copyright IBM Corp. 2016 All Rights Reserved.
2+
Copyright IBM Corp. All Rights Reserved.
33
4-
Licensed under the Apache License, Version 2.0 (the "License");
5-
you may not use this file except in compliance with the License.
6-
You may obtain a copy of the License at
7-
8-
http://www.apache.org/licenses/LICENSE-2.0
9-
10-
Unless required by applicable law or agreed to in writing, software
11-
distributed under the License is distributed on an "AS IS" BASIS,
12-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
See the License for the specific language governing permissions and
14-
limitations under the License.
4+
SPDX-License-Identifier: Apache-2.0
155
*/
166

177
package viperutil
@@ -323,5 +313,17 @@ func EnhancedExactUnmarshalKey(baseKey string, output interface{}) error {
323313
leafKeys := getKeysRecursively("", viper.Get, m)
324314

325315
logger.Debugf("%+v", leafKeys)
326-
return mapstructure.Decode(leafKeys[baseKey], output)
316+
317+
config := &mapstructure.DecoderConfig{
318+
Metadata: nil,
319+
Result: output,
320+
WeaklyTypedInput: true,
321+
}
322+
323+
decoder, err := mapstructure.NewDecoder(config)
324+
if err != nil {
325+
return err
326+
}
327+
328+
return decoder.Decode(leafKeys[baseKey])
327329
}

0 commit comments

Comments
 (0)