mirror of
https://github.com/SAP/jenkins-library.git
synced 2024-12-12 10:55:20 +02:00
fix(config) conversion of small ints into string (#2274)
* fix(config) conversion of small ints into string * beautify and improve comment
This commit is contained in:
parent
6ba8b7968b
commit
690b27ed48
@ -366,12 +366,18 @@ func convertValueFromFloat(config map[string]interface{}, optionsField *reflect.
|
|||||||
switch optionsField.Type.Kind() {
|
switch optionsField.Type.Kind() {
|
||||||
case reflect.String:
|
case reflect.String:
|
||||||
val := strconv.FormatFloat(paramValue, 'f', -1, 64)
|
val := strconv.FormatFloat(paramValue, 'f', -1, 64)
|
||||||
|
// if Sprinted value and val are equal, we can be pretty sure that the result fits
|
||||||
|
// for very large numbers for example an exponential format is printed
|
||||||
|
if val == fmt.Sprint(paramValue) {
|
||||||
|
config[paramName] = val
|
||||||
|
return nil
|
||||||
|
}
|
||||||
// allow float numbers containing a decimal separator
|
// allow float numbers containing a decimal separator
|
||||||
if strings.Contains(val, ".") {
|
if strings.Contains(val, ".") {
|
||||||
config[paramName] = val
|
config[paramName] = val
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
// if no decimal separator is available we cannot be sure that the result is correct:
|
// if now no decimal separator is available we cannot be sure that the result is correct:
|
||||||
// long numbers like e.g. 73554900100200011600 will not be represented correctly after reading the yaml
|
// long numbers like e.g. 73554900100200011600 will not be represented correctly after reading the yaml
|
||||||
// thus we cannot assume that the string is correct.
|
// thus we cannot assume that the string is correct.
|
||||||
// short numbers will be handled as int anyway
|
// short numbers will be handled as int anyway
|
||||||
|
@ -385,6 +385,32 @@ bar: 42
|
|||||||
// Assert
|
// Assert
|
||||||
assert.True(t, hasFailed, "Expected checkTypes() to exit via logging framework")
|
assert.True(t, hasFailed, "Expected checkTypes() to exit via logging framework")
|
||||||
})
|
})
|
||||||
|
t.Run("Properly handle small ints", func(t *testing.T) {
|
||||||
|
// Init
|
||||||
|
hasFailed := false
|
||||||
|
|
||||||
|
exitFunc := log.Entry().Logger.ExitFunc
|
||||||
|
log.Entry().Logger.ExitFunc = func(int) {
|
||||||
|
hasFailed = true
|
||||||
|
}
|
||||||
|
defer func() { log.Entry().Logger.ExitFunc = exitFunc }()
|
||||||
|
|
||||||
|
options := struct {
|
||||||
|
Foo string `json:"foo,omitempty"`
|
||||||
|
}{}
|
||||||
|
|
||||||
|
stepConfig := map[string]interface{}{}
|
||||||
|
|
||||||
|
content := []byte("foo: 11")
|
||||||
|
err := yaml.Unmarshal(content, &stepConfig)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
// Test
|
||||||
|
stepConfig = checkTypes(stepConfig, options)
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
assert.False(t, hasFailed, "Expected checkTypes() NOT to exit via logging framework")
|
||||||
|
})
|
||||||
t.Run("Ignores nil values", func(t *testing.T) {
|
t.Run("Ignores nil values", func(t *testing.T) {
|
||||||
// Init
|
// Init
|
||||||
hasFailed := false
|
hasFailed := false
|
||||||
|
Loading…
Reference in New Issue
Block a user