forked from miaomiao3/xlsxutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoption.go
277 lines (230 loc) · 6.67 KB
/
option.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package xlsxutil
import (
"errors"
"fmt"
"math"
"reflect"
"strconv"
"strings"
errors2 "github.com/pkg/errors"
)
const (
xlsTag = "xls"
inlineKey = "inline"
precisionKey = "precision"
defaultFloatPrecision = -1
)
type xlsOption struct {
XlsName string
IsInline bool
Precision int
}
func getStructOptions(dataValue reflect.Value) (fieldNames []string, optionMap map[string]*xlsOption) {
if dataValue.Kind() == reflect.Ptr {
dataValue = dataValue.Elem()
}
if optionMap == nil {
optionMap = make(map[string]*xlsOption)
}
dataType := dataValue.Type()
for i := 0; i < dataValue.NumField(); i++ {
fieldValue := dataValue.Field(i)
fieldType := dataType.Field(i)
tag := fieldType.Tag.Get(xlsTag)
optionMap[fieldType.Name] = getOptionFromTag(tag)
switch fieldValue.Kind() {
case reflect.Ptr:
if fieldValue.Elem().Kind() == reflect.Struct {
newFieldNames, newOptionMap := getStructOptions(fieldValue.Elem())
for k, v := range newOptionMap {
optionMap[k] = v
}
fieldNames = append(fieldNames, newFieldNames...)
}
case reflect.Struct:
newFieldNames, newOptionMap := getStructOptions(fieldValue)
for k, v := range newOptionMap {
optionMap[k] = v
}
fieldNames = append(fieldNames, newFieldNames...)
default:
fieldNames = append(fieldNames, optionMap[fieldType.Name].XlsName)
}
}
return
}
func getOptionFromTag(tag string) *xlsOption {
tagStrSegs := strings.Split(tag, ",")
option := &xlsOption{
XlsName: tagStrSegs[0],
}
if len(tagStrSegs) <= 1 {
return option
}
for _, v := range tagStrSegs[1:] {
tagStrSeg := v
if inlineKey == strings.TrimSpace(tagStrSeg) {
option.IsInline = true
continue
}
segs := strings.Split(strings.TrimSpace(tagStrSeg), ":")
if precisionKey == strings.TrimSpace(segs[0]) {
option.Precision, _ = strconv.Atoi(strings.TrimSpace(segs[1]))
}
}
return option
}
type rowHandleFunc func(str string, kind reflect.Kind)
// validate data, return slice element of struct, error
func validateDataInput(data interface{}) (reflect.Type, error) {
dataType := reflect.TypeOf(data)
if dataType.Kind() != reflect.Ptr {
return nil, errInputType
}
dataType = dataType.Elem()
if dataType.Kind() != reflect.Slice {
return nil, errors2.WithStack(errInputType)
}
checkDataType := dataType.Elem() // type for check
elementType := dataType.Elem()
if checkDataType.Kind() == reflect.Ptr {
checkDataType = checkDataType.Elem()
}
if checkDataType.Kind() != reflect.Struct {
return nil, errors2.WithStack(errInputType)
}
return elementType, nil
}
func newElement(dataType reflect.Type, valueMap map[string]string, optionMap map[string]*xlsOption) *reflect.Value {
var elem reflect.Value
elem = reflect.New(dataType).Elem()
setStructValue(elem, valueMap, optionMap)
return &elem
}
func setStructValue(dataValue reflect.Value, valueMap map[string]string, optionMap map[string]*xlsOption) {
if dataValue.Kind() == reflect.Ptr {
dataValue = dataValue.Elem()
}
if !dataValue.CanAddr() {
return
}
dataType := dataValue.Type()
for i := 0; i < dataValue.NumField(); i++ {
fieldValue := dataValue.Field(i)
fieldType := dataType.Field(i)
option, ok := optionMap[fieldType.Name]
if !ok {
continue
}
fieldStr := valueMap[option.XlsName]
if fieldStr == "" && !option.IsInline {
continue
}
switch fieldValue.Kind() {
case reflect.Ptr:
if fieldValue.Elem().Kind() == reflect.Struct {
setStructValue(fieldValue.Elem(), valueMap, optionMap)
}
case reflect.Struct:
setStructValue(fieldValue, valueMap, optionMap)
case reflect.String:
fieldValue.SetString(fieldStr)
case reflect.Int,
reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
numValue, err := strconv.ParseInt(fieldStr, 10, 64)
if err != nil {
break
}
fieldValue.SetInt(numValue)
case reflect.Float32, reflect.Float64:
floatValue, err := strconv.ParseFloat(fieldStr, 64)
if err != nil {
break
}
if option.Precision > 0 {
floatValue = toFixed(floatValue, option.Precision)
}
fieldValue.SetFloat(floatValue)
}
}
}
// add header
func addHeaderRow(dataValue reflect.Value, f rowHandleFunc) (optionMap map[string]*xlsOption, err error) {
var fieldNames []string
fieldNames, optionMap = getStructOptions(dataValue)
for _, v := range fieldNames {
f(v, reflect.String)
}
return optionMap, nil
}
func addRow(dataValue reflect.Value, optionMap map[string]*xlsOption, f rowHandleFunc) (err error) {
if dataValue.Kind() == reflect.Ptr {
dataValue = dataValue.Elem()
}
if dataValue.Kind() != reflect.Struct {
err = errors.New("only support struct or struct pointer")
return
}
dataType := dataValue.Type()
num := dataValue.NumField()
for i := 0; i < num; i++ {
fieldValue := dataValue.Field(i)
fieldName := dataType.Field(i).Name
option, ok := optionMap[fieldName]
if !ok {
continue
}
switch fieldValue.Kind() {
case reflect.Ptr:
if fieldValue.Elem().Kind() == reflect.Struct {
if option.IsInline {
err = addRow(fieldValue.Elem(), optionMap, f)
if err != nil {
return err
}
}
}
case reflect.Struct:
if fieldValue.CanInterface() {
if option.IsInline {
err = addRow(fieldValue, optionMap, f)
if err != nil {
return err
}
}
}
case reflect.Interface, reflect.Map, reflect.Array, reflect.Slice, reflect.Complex64, reflect.Complex128, reflect.UnsafePointer, reflect.Chan, reflect.Func:
f("# unsupported by xlsx-util #", fieldValue.Kind())
case reflect.Float64, reflect.Float32:
precision := defaultFloatPrecision
if option != nil && option.Precision > 0 {
precision = option.Precision
}
fieldStr := ""
if fieldValue.CanInterface() {
if fieldValue.Kind() == reflect.Float32 {
fieldStr = strconv.FormatFloat(float64(fieldValue.Interface().(float32)), 'f', precision, 64)
} else {
fieldStr = strconv.FormatFloat(fieldValue.Interface().(float64), 'f', precision, 64)
}
}
f(fieldStr, fieldValue.Kind())
case reflect.Int8, reflect.Uint8, reflect.Int16, reflect.Uint16, reflect.Int32, reflect.Uint32,
reflect.Int64, reflect.Uint64, reflect.Int, reflect.Uint, reflect.String:
if fieldValue.CanInterface() {
fieldStr := fmt.Sprintf("%v", fieldValue.Interface())
f(fieldStr, fieldValue.Kind())
}
}
}
return nil
}
// https://stackoverflow.com/questions/18390266/how-can-we-truncate-float64-type-to-a-particular-precision
func round(num float64) int {
return int(num + math.Copysign(0.5, num))
}
func toFixed(num float64, precision int) float64 {
output := math.Pow(10, float64(precision))
return float64(round(num*output)) / output
}