forked from jawher/mow.cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformatters.go
64 lines (57 loc) · 1.12 KB
/
formatters.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
package cli
import (
"fmt"
"reflect"
)
func formatterFor(t reflect.Type) func(interface{}) string {
switch t.Kind() {
case reflect.Bool:
return boolFormatter
case reflect.String:
return stringFormatter
case reflect.Int:
return intFormatter
case reflect.Slice:
switch t.Elem().Kind() {
case reflect.String:
return stringsFormatter
case reflect.Int:
return intsFormatter
default:
panic(fmt.Sprintf("No formatter for %v", t))
}
default:
panic(fmt.Sprintf("No formatter for %v", t))
}
}
func boolFormatter(v interface{}) string {
return fmt.Sprintf("%v", v)
}
func stringFormatter(v interface{}) string {
return fmt.Sprintf("%#v", v)
}
func intFormatter(v interface{}) string {
return fmt.Sprintf("%v", v)
}
func stringsFormatter(v interface{}) string {
res := "["
strings, _ := v.([]string)
for idx, s := range strings {
if idx > 0 {
res += ", "
}
res += fmt.Sprintf("%#v", s)
}
return res + "]"
}
func intsFormatter(v interface{}) string {
res := "["
ints, _ := v.([]int)
for idx, s := range ints {
if idx > 0 {
res += ", "
}
res += fmt.Sprintf("%v", s)
}
return res + "]"
}