Skip to content

Commit 851e377

Browse files
authored
Merge pull request #23 from d4rkstar/issue_106
fix: handle value formatting on config map
2 parents 44a9000 + b553f65 commit 851e377

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

config/config_map.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"fmt"
2323
"log"
2424
"os"
25+
"reflect"
2526
"strconv"
2627
"strings"
2728
)
@@ -162,11 +163,28 @@ func flatten(prefix string, inputMap map[string]interface{}, outputMap map[strin
162163
case map[string]interface{}:
163164
flatten(key, child, outputMap)
164165
default:
165-
outputMap[key] = fmt.Sprintf("%v", v)
166+
outputMap[key] = formatValue(v)
166167
}
167168
}
168169
}
169170

171+
func formatValue(v interface{}) string {
172+
switch val := v.(type) {
173+
case int, int8, int16, int32, int64:
174+
return fmt.Sprintf("%d", val)
175+
case uint, uint8, uint16, uint32, uint64:
176+
return fmt.Sprintf("%d", val)
177+
case float32, float64:
178+
return strconv.FormatFloat(reflect.ValueOf(val).Float(), 'f', -1, 64)
179+
case bool:
180+
return strconv.FormatBool(val)
181+
case string:
182+
return val
183+
default:
184+
return fmt.Sprintf("%v", val)
185+
}
186+
}
187+
170188
type configOperationFunc func(config map[string]interface{}, key string) bool
171189

172190
func visit(config map[string]interface{}, index int, keys []string, f configOperationFunc) bool {

0 commit comments

Comments
 (0)