This repository has been archived by the owner on Nov 25, 2020. It is now read-only.
forked from gocarina/gocsv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reflect.go
79 lines (71 loc) · 1.75 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
package gocsv_alt
import (
"reflect"
"strings"
"sync"
)
// --------------------------------------------------------------------------
// Reflection helpers
type structInfo struct {
Fields []fieldInfo
Zero reflect.Value
}
type fieldInfo struct {
Key string
Num int
OmitEmpty bool
}
var structMap = make(map[reflect.Type]*structInfo)
var structMapMutex sync.RWMutex
func getStructInfo(rType reflect.Type) *structInfo {
structMapMutex.RLock()
stInfo, ok := structMap[rType]
structMapMutex.RUnlock()
if ok {
return stInfo
}
fieldsCount := rType.NumField()
fieldsList := make([]fieldInfo, 0, fieldsCount)
for i := 0; i < fieldsCount; i++ {
field := rType.Field(i)
if field.PkgPath != "" {
continue
}
fieldInfo := fieldInfo{Num: i}
fieldTag := field.Tag.Get("csv")
fieldTags := strings.Split(fieldTag, ",")
for _, fieldTagEntry := range fieldTags {
if fieldTagEntry == "omitempty" {
fieldInfo.OmitEmpty = true
} else {
fieldTag = fieldTagEntry
}
}
if fieldTag == "-" {
continue
} else if fieldTag != "" {
fieldInfo.Key = fieldTag
} else {
fieldInfo.Key = field.Name
}
fieldsList = append(fieldsList, fieldInfo)
}
stInfo = &structInfo{fieldsList, reflect.New(rType).Elem()}
return stInfo
}
func getConcreteContainerInnerType(in reflect.Type) (inInnerWasPointer bool, inInnerType reflect.Type) {
inInnerType = in.Elem()
inInnerWasPointer = false
if inInnerType.Kind() == reflect.Ptr {
inInnerWasPointer = true
inInnerType = inInnerType.Elem()
}
return inInnerWasPointer, inInnerType
}
func getConcreteReflectValueAndType(in interface{}) (reflect.Value, reflect.Type) {
value := reflect.ValueOf(in)
if value.Kind() == reflect.Ptr {
value = value.Elem()
}
return value, value.Type()
}