-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrender.go
309 lines (277 loc) · 8.55 KB
/
render.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package iss
import (
"fmt"
"reflect"
"strconv"
"strings"
)
const DefaultTagKey string = "json"
//const DefaultTagKey string = "csv"
// Иногда приходят null значение
func parseStringWithDefaultValue(fieldValue interface{}) string {
if fieldValue == nil {
return ""
}
return fieldValue.(string)
}
// setFloatField
func parseFloatWithDefaultValue(fieldValue interface{}) float64 {
if fieldValue == nil {
return 0
}
return fieldValue.(float64)
}
func parseIntWithDefaultValue(fieldValue interface{}) int {
if fieldValue == nil {
return 0
}
return int(fieldValue.(float64))
}
func parseInt64WithDefaultValue(fieldValue interface{}) int64 {
if fieldValue == nil {
return 0
}
return int64(fieldValue.(float64))
}
// Unmarshal парсинг массивов. По аналогии с csv
func Unmarshal(header []string, data [][]interface{}, destination interface{}) error {
// получим значения
sliceValPtr := reflect.ValueOf(destination)
if sliceValPtr.Kind() != reflect.Ptr {
return fmt.Errorf("must be a pointer to a slice of structs")
}
sliceVal := sliceValPtr.Elem()
if sliceVal.Kind() != reflect.Slice {
return fmt.Errorf("must be a pointer to a slice of structs")
}
structType := sliceVal.Type().Elem()
if structType.Kind() != reflect.Struct {
return fmt.Errorf("must be a pointer to a slice of structs")
}
// создадим map с названием колонок
headerMap := make(map[string]int, len(header))
for k, name := range header {
headerMap[name] = k
}
// в цикле по данным
for _, row := range data {
newValue := reflect.New(structType).Elem()
//slog.Info("Unmarshal", slog.Any("newVal", newVal))
// парсим одну строку
err := unmarshalRow(headerMap, row, newValue, DefaultTagKey)
if err != nil {
return err
}
sliceVal.Set(reflect.Append(sliceVal, newValue))
}
return nil
}
// unmarshalRow распарсим заданную строку
func unmarshalRow(headerMap map[string]int, rowData []interface{}, vv reflect.Value, tagKey string) error {
vt := vv.Type()
for i := 0; i < vv.NumField(); i++ {
typeField := vt.Field(i)
name := typeField.Tag.Get(tagKey)
if name == "" {
name = typeField.Name
}
pos, ok := headerMap[name]
if !ok {
continue
}
val := rowData[pos]
field := vv.Field(i)
//slog.Info(" unmarshalOne ", "pos", pos, slog.Any("val", val), slog.Any("field", field), "valRv.Kind()", field.Kind())
// TODO добавить setField
switch field.Kind() {
case reflect.Float64, reflect.Float32:
field.SetFloat(parseFloatWithDefaultValue(val))
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
field.SetInt(parseInt64WithDefaultValue(val))
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
//field.SetUint(i)
case reflect.String:
field.SetString(parseStringWithDefaultValue(val))
case reflect.Bool:
//b, err := strconv.ParseBool(val)
//if err != nil {
// return err
//}
//field.SetBool(b)
default:
return fmt.Errorf("cannot handle field of kind %v", field.Kind())
}
}
return nil
}
// код из проекта go-csv-tag
// https://github.com/artonge/go-csv-tag/blob/master/load.go
// Map the provided content to the destination using the header and the tags.
// @param header: the csv header to match with the struct's tags.
// @param content: the content to put in destination.
// @param destination: the destination where to put the file's content.
func UnmarshalCSV(header []string, content [][]string, destination interface{}, tagKey string) error {
//func mapToDestination(header []string, content [][]string, destination interface{}, tagKey string) error {
if destination == nil {
return fmt.Errorf("destination slice is nil")
}
if reflect.TypeOf(destination).Elem().Kind() != reflect.Slice {
return fmt.Errorf("destination is not a slice")
}
// создадим map с названием колонок
headerMap := make(map[string]int)
for i, name := range header {
headerMap[strings.TrimSpace(name)] = i
}
// Create the slice to put the values in.
sliceRv := reflect.MakeSlice(
reflect.ValueOf(destination).Elem().Type(),
len(content),
len(content),
)
for i, line := range content {
emptyStruct := sliceRv.Index(i)
for j := 0; j < emptyStruct.NumField(); j++ {
propertyTag := emptyStruct.Type().Field(j).Tag.Get(tagKey)
if propertyTag == "" {
continue
}
propertyPosition, ok := headerMap[propertyTag]
if !ok {
continue
}
err := storeValue(line[propertyPosition], emptyStruct.Field(j))
if err != nil {
return fmt.Errorf("line: %v to slice: %v:\n ==> %v", line, emptyStruct, err)
}
}
}
reflect.ValueOf(destination).Elem().Set(sliceRv)
return nil
}
func storeValue(rawValue string, valRv reflect.Value) error {
rawValue = strings.TrimSpace(rawValue)
switch valRv.Kind() {
case reflect.String:
valRv.SetString(rawValue)
case reflect.Uint32:
fallthrough
case reflect.Uint64:
fallthrough
case reflect.Uint:
value, err := strconv.ParseUint(rawValue, 10, 64)
if err != nil && rawValue != "" {
return fmt.Errorf("error parsing uint '%v':\n ==> %v", rawValue, err)
}
valRv.SetUint(value)
case reflect.Int32:
fallthrough
case reflect.Int64:
fallthrough
case reflect.Int:
i, err := toInt(rawValue)
if err != nil {
return fmt.Errorf("error parsing int '%v':\n ==> %v", rawValue, err)
}
valRv.SetInt(i)
//value, err := strconv.ParseInt(rawValue, 10, 64)
//if err != nil && rawValue != "" {
// return fmt.Errorf("error parsing int '%v':\n ==> %v", rawValue, err)
//}
//valRv.SetInt(value)
case reflect.Float32:
fallthrough
case reflect.Float64:
f, err := toFloat(rawValue)
if err != nil {
return fmt.Errorf("error parsing float '%v':\n ==> %v", rawValue, err)
}
valRv.SetFloat(f)
//value, err := strconv.ParseFloat(rawValue, 64)
//if err != nil && rawValue != "" {
// return fmt.Errorf("error parsing float '%v':\n ==> %v", rawValue, err)
//}
//valRv.SetFloat(value)
case reflect.Bool:
value, err := strconv.ParseBool(rawValue)
if err != nil && rawValue != "" {
return fmt.Errorf("error parsing bool '%v':\n ==> %v", rawValue, err)
}
valRv.SetBool(value)
}
return nil
}
// из проекта "github.com/gocarina/gocsv"
func toFloat(in interface{}) (float64, error) {
//if in == nil {
// return 0, nil
//}
inValue := reflect.ValueOf(in)
switch inValue.Kind() {
case reflect.String:
s := strings.TrimSpace(inValue.String())
if s == "" {
return 0, nil
}
s = strings.Replace(s, ",", ".", -1)
return strconv.ParseFloat(s, 64)
case reflect.Bool:
if inValue.Bool() {
return 1, nil
}
return 0, nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return float64(inValue.Int()), nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return float64(inValue.Uint()), nil
case reflect.Float32, reflect.Float64:
return inValue.Float(), nil
}
return 0, fmt.Errorf("No known conversion from " + inValue.Type().String() + " to float")
}
func toInt(in interface{}) (int64, error) {
inValue := reflect.ValueOf(in)
switch inValue.Kind() {
case reflect.String:
s := strings.TrimSpace(inValue.String())
if s == "" {
return 0, nil
}
out := strings.SplitN(s, ".", 2)
return strconv.ParseInt(out[0], 0, 64)
case reflect.Bool:
if inValue.Bool() {
return 1, nil
}
return 0, nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return inValue.Int(), nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return int64(inValue.Uint()), nil
case reflect.Float32, reflect.Float64:
return int64(inValue.Float()), nil
}
return 0, fmt.Errorf("No known conversion from " + inValue.Type().String() + " to int")
}
func toString(in interface{}) (string, error) {
inValue := reflect.ValueOf(in)
switch inValue.Kind() {
case reflect.String:
return inValue.String(), nil
case reflect.Bool:
b := inValue.Bool()
if b {
return "true", nil
}
return "false", nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return fmt.Sprintf("%v", inValue.Int()), nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return fmt.Sprintf("%v", inValue.Uint()), nil
case reflect.Float32:
return strconv.FormatFloat(inValue.Float(), byte('f'), -1, 32), nil
case reflect.Float64:
return strconv.FormatFloat(inValue.Float(), byte('f'), -1, 64), nil
}
return "", fmt.Errorf("No known conversion from " + inValue.Type().String() + " to string")
}