-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreflect.go
92 lines (85 loc) · 2.31 KB
/
reflect.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package reporter
import (
"fmt"
"reflect"
"strings"
"time"
)
type reflection struct {
Type reflect.Type
Kind reflect.Kind
Value reflect.Value
}
func createReflection(v interface{}) *reflection {
refVal := reflect.ValueOf(v)
if refVal.Kind() == reflect.Ptr || refVal.Kind() == reflect.Interface {
refVal = refVal.Elem() // get the Value contained in an pointer or interface
}
var t reflect.Type
if refVal.IsValid() {
t = refVal.Type()
}
return &reflection{
Type: t,
Kind: refVal.Kind(),
Value: refVal,
}
}
func (ref *reflection) getStructFieldAtIdx(idx int) *reflection {
structField := ref.Value.Field(idx)
return createReflection(structField.Interface())
}
func (ref *reflection) tryCommonTypes() string {
if ref.Type.Name() == reflect.TypeOf(time.Time{}).Name() {
return reflectTimeToString(&ref.Value)
}
return ""
}
func reflectTimeToString(structField *reflect.Value) string {
formatMethod := structField.MethodByName("Format")
if !formatMethod.IsZero() {
args := []reflect.Value{reflect.ValueOf(timeFormatString)}
timeVal := formatMethod.Call(args)
if len(timeVal) > 0 {
return timeVal[0].String()
}
}
return ""
}
func (ref *reflection) getUnderlyingData() string {
switch ref.Kind {
case reflect.Struct:
return strings.Join(MarshalCSV(ref.Value.Interface()), ",")
case reflect.Slice:
strVal := ""
sliceRef := createReflection(ref.Value.Interface())
if sliceRef.Value.Len() > 0 {
curRef := createReflection(sliceRef.Value.Index(0).Interface())
strVal = curRef.getUnderlyingData()
for i := 1; i < sliceRef.Value.Len(); i++ {
curRef = createReflection(sliceRef.Value.Index(i).Interface())
strVal += fmt.Sprintf(",%v", curRef.getUnderlyingData())
}
}
return strVal
case reflect.Map:
iter := ref.Value.MapRange()
strVal := ""
for iter.Next() {
keyRef := createReflection(iter.Key().Interface())
valRef := createReflection(iter.Value().Interface())
strVal += fmt.Sprintf(",%v:%v", keyRef.getUnderlyingData(), valRef.getUnderlyingData())
}
if string(strVal[0]) == "," {
strVal = strVal[1:]
}
return strVal
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return fmt.Sprint(ref.Value.Interface())
default:
if !ref.Value.IsValid() {
return ""
}
return fmt.Sprintf("%v", ref.Value.Interface())
}
}