/
human_readable_parser.go
73 lines (70 loc) · 1.89 KB
/
human_readable_parser.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package parser
import (
"fmt"
"reflect"
"strconv"
"strings"
)
func toHumanReadable(value reflect.Value, insertion int) string {
kind := value.Kind()
switch kind {
case reflect.Struct:
var output string
for i := 0; i < value.NumField(); i++ {
if value.Type().Field(i).Tag.Get("human_readable") == "-" {
continue
}
fieldValue := toHumanReadable(value.Field(i), insertion+1)
if strings.TrimSpace(fieldValue) == "" {
continue
}
output += strings.Repeat(" ", insertion)
output += value.Type().Field(i).Name + ": "
output += fieldValue
output += "\n"
}
return "\n" + strings.Repeat(" ", insertion) + strings.TrimSpace(output) + "\n"
case reflect.Slice:
if value.IsNil() {
return ""
}
output := "[" + strconv.Itoa(value.Len()) + "] "
for i := 0; i < value.Len(); i++ {
output += toHumanReadable(value.Index(i), insertion+1)
output += " "
}
output += "\n"
return output
case reflect.Map:
output := "(" + strconv.Itoa(value.Len()) + ") \n"
for _, key := range value.MapKeys() {
output += strings.Repeat(" ", insertion)
output += toHumanReadable(key, insertion+1) + ": "
output += toHumanReadable(value.MapIndex(key), insertion+1)
output += "\n"
}
return "\n" + strings.Repeat(" ", insertion) + strings.TrimSpace(output) + "\n"
case reflect.String:
return value.String()
case reflect.Int:
return strconv.Itoa(int(value.Int()))
case reflect.Uint:
return strconv.Itoa(int(value.Uint()))
case reflect.Uint64:
return strconv.Itoa(int(value.Uint()))
case reflect.Float64:
return strconv.FormatFloat(value.Float(), 'f', -1, 64)
case reflect.Ptr:
if value.IsNil() {
return ""
}
return toHumanReadable(reflect.Indirect(value), insertion)
case reflect.Interface:
return toHumanReadable(reflect.ValueOf(value.Interface()), insertion)
default:
if !value.IsValid() {
return ""
}
return fmt.Sprint(value.Interface())
}
}