-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmeta_data.go
132 lines (110 loc) · 3 KB
/
meta_data.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package excelizex
import (
"reflect"
"strconv"
)
type metaData struct {
// 表名
sheetName string
// 存储header原始数据
headers []string
// k:columns index v:ExtHeader name
colsIndexHeaderMap map[int]string
// k:header v:Struct Field Name
headerFieldName map[string]string
// k:header v:convert name
headerMappingConverter map[string]string
// k: converter name v:converter func
converter map[string]ConvertFunc
// payload struct,the real val for run
payload any
}
func (mr *metaRaws) newMetaData() (md *metaData) {
md = &metaData{
headers: make([]string, 0),
colsIndexHeaderMap: make(map[int]string, 0),
headerFieldName: make(map[string]string),
headerMappingConverter: make(map[string]string),
converter: make(map[string]ConvertFunc),
}
for _, raw := range mr.raws {
// 加入header
if raw.part == headerPart {
md.headers = append(md.headers, raw.cellValue)
md.headerFieldName[raw.cellValue] = fieldName
}
}
return
}
func (e *metaData) addHeaderFieldName(header, fieldName string) {
e.headerFieldName[header] = fieldName
}
func (e *metaData) addHeaderConvertName(header, convertName string) {
e.headerMappingConverter[header] = convertName
}
func (e *metaData) findHeadersMap(columns []string) (exist bool) {
if len(columns) < len(e.headers) {
return
}
for index, column := range columns {
for _, h := range e.headers {
if column == h {
e.colsIndexHeaderMap[index] = column
continue
}
}
}
if len(e.colsIndexHeaderMap) == len(e.headers) {
exist = true
} else {
e.colsIndexHeaderMap = make(map[int]string, 0)
}
return
}
func (e *metaData) getHeader(columnIndex int) (header string) {
return e.colsIndexHeaderMap[columnIndex]
}
func (e *metaData) findConvertByHeader(header string) (convertName string, exist bool) {
convertName, exist = e.headerMappingConverter[header]
return
}
func (e *metaData) getHeaderFieldName(columnIndex int) (header string) {
return e.headerFieldName[e.getHeader(columnIndex)]
}
func (e *metaData) dataMapping(ptr any, columns []string) (err error) {
for index, col := range columns {
fieldName := e.getHeaderFieldName(index)
if fieldName == "" {
continue
}
field := reflect.ValueOf(ptr).Elem().FieldByName(fieldName)
// 查看该字段是否有转换器
if v, ok := e.findConvertByHeader(e.getHeader(index)); ok {
var convertValue any
if convertValue, err = e.converter[v](col); err != nil {
return
}
field.Set(reflect.ValueOf(convertValue))
continue
}
switch field.Kind() {
case reflect.Float32, reflect.Float64:
var i float64
if i, err = strconv.ParseFloat(col, 64); nil != err {
panic(i)
}
field.SetFloat(i)
case reflect.Int64, reflect.Int32, reflect.Int8, reflect.Int16, reflect.Int:
var i int64
if i, err = strconv.ParseInt(col, 10, 64); nil != err {
panic(i)
}
field.SetInt(i)
case reflect.String:
field.SetString(col)
default:
panic("cannot support other type besides int,float,string")
}
}
return
}