Generalize Prettify function

This commit is contained in:
Alan Bernstein 2016-12-12 22:10:07 -06:00
parent d08ffd20e2
commit da3bb99f0a

View file

@ -1,8 +1,6 @@
package bench
import (
"time"
)
import "time"
// wrapper type to force human-readable JSON output
type PrettyDuration time.Duration
@ -17,10 +15,19 @@ func Prettify(m map[string]interface{}) map[string]interface{} {
newmap := make(map[string]interface{})
for k, v := range m {
switch v.(type) {
case map[string]interface{}:
newmap[k] = Prettify(v.(map[string]interface{}))
case []time.Duration:
newslice := make([]PrettyDuration, len(v.([]time.Duration)))
slice := v.([]time.Duration)
for n, e := range slice {
newslice[n] = PrettyDuration(e)
}
newmap[k] = newslice
case time.Duration:
newmap[k] = PrettyDuration(v.(time.Duration))
default:
newmap[k] = Prettify(v.(map[string]interface{}))
newmap[k] = v
}
}
return newmap