Skip to content

Commit 16c40e5

Browse files
Volodymyr Paprotskimastersingh24
Volodymyr Paprotski
authored andcommitted
[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 <[email protected]> Signed-off-by: Gari Singh <[email protected]>
1 parent 3069430 commit 16c40e5

File tree

2 files changed

+25
-27
lines changed

2 files changed

+25
-27
lines changed

common/viperutil/config_test.go

+10-14
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
@@ -439,7 +429,8 @@ func TestStringFromFileEnv(t *testing.T) {
439429

440430
func TestEnhancedExactUnmarshalKey(t *testing.T) {
441431
type Nested struct {
442-
Key string
432+
Key string
433+
BoolVar bool
443434
}
444435

445436
type nestedKey struct {
@@ -450,7 +441,8 @@ func TestEnhancedExactUnmarshalKey(t *testing.T) {
450441
"Top:\n" +
451442
" Nested:\n" +
452443
" Nested:\n" +
453-
" Key: BAD\n"
444+
" Key: BAD\n" +
445+
" BoolVar: true\n"
454446

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

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

common/viperutil/config_util.go

+15-13
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
@@ -321,5 +311,17 @@ func EnhancedExactUnmarshalKey(baseKey string, output interface{}) error {
321311
leafKeys := getKeysRecursively("", viper.Get, m)
322312

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

0 commit comments

Comments
 (0)