-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsvdata.go
More file actions
355 lines (330 loc) · 8.69 KB
/
csvdata.go
File metadata and controls
355 lines (330 loc) · 8.69 KB
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package csvdata
// csvdata complements the csv package by allowing you to map a custom structure to
// the columns of data in a CSV file. The struct needs to be annotated so that each
// field can match a column in the data
//
// type Person struct {
// FirstName string `field:"First Name"`
// Second_Name string
// Age int
// }
//
// The name of the column can be inferred from the field name; any underscores in the
// name are converted to spaces when comparing. Otherwise, you must provide a tag
// 'field' with the name of the column.
//
// r := csv.NewReader(os.Stdin)
// p := new (Person)
// rs,_ := NewReaderIter(r,p)
// for rs.Get() {
// fmt.Println(p.FirstName,p.Second_Name,p.Age)
// }
// if rs.Error != nil {
// fmt.Println("error",rs.Error)
// }
import (
"errors"
"io"
// "os"
"fmt"
"reflect"
"strconv"
"strings"
"time"
)
// The data source is any object that has a Read method which can
// return a row as a slice of strings. This matches csv.Reader in particular.
type Reader interface {
Read() ([]string, error)
}
// Custom data types can be implemented by implementing Value; these
// methods must be defined on a pointer receiver.
// The interface is also used by flag package for a similar purpose.
type Value interface {
String() string
Set(string) bool
}
// ReadIter encapsulates an iterator over a Reader source that fills a
// pointer to a user struct with data.
type ReadIter struct {
Reader Reader
Headers []string
Error error
Line, Column int
fields []reflect.Value
kinds []int
tags []int
}
const (
none_k = iota
string_k
int_k
float_k
uint_k
value_k
)
func StrToInt64(s string) int64 {
i, err := strconv.ParseInt(s, 10, 0)
if err != nil {
// handle error
}
return i
}
func mapType(aHeader []string, v reflect.Value) (this *ReadIter, err error) {
this = new(ReadIter)
this.Line = 1
// t := v.Type()
st := v.Type() // reflect.TypeOf(v).Elem()
//sv := h.Elem()
//nf := st.NumField()
//this.kinds = make([]int, nf)
//this.tags = make([]int, nf)
//this.fields = make([]reflect.Value, nf)
for i := 0; i < st.NumField(); i++ {
f := st.Field(i) //field
val := v.Field(i) // field value
// ADD BY HZM
if val.Kind() == reflect.Struct {
var lTime time.Time
//fmt.Println(reflect.TypeOf(lTime), val.Type(), val.Type().ConvertibleTo(reflect.TypeOf(lTime)))
// 非时间的结构体
if !val.Type().ConvertibleTo(reflect.TypeOf(lTime)) {
//fmt.Println("time.Time")
lParentReadIter, _ := mapType(aHeader, val)
//fmt.Println("lParentReadIter", len(lParentReadIter.fields), len(lParentReadIter.kinds))
this.fields = append(this.fields, lParentReadIter.fields...)
this.kinds = append(this.kinds, lParentReadIter.kinds...)
this.tags = append(this.tags, lParentReadIter.tags...)
//fmt.Println(len(this.fields), len(this.kinds))
continue
}
/*
switch val.Interface().(type) {
case time.Time:
fmt.Println("time.Time")
default:
lParentReadIter, _ := mapType(aHeader, val)
//fmt.Println("lParentReadIter", len(lParentReadIter.fields), len(lParentReadIter.kinds))
this.fields = append(this.fields, lParentReadIter.fields...)
this.kinds = append(this.kinds, lParentReadIter.kinds...)
this.tags = append(this.tags, lParentReadIter.tags...)
fmt.Println(len(this.fields), len(this.kinds))
continue
}
*/
}
// get the corresponding field name and look it up in the headers
tag := f.Tag.Get("field")
if len(tag) == 0 {
tag = f.Name
if strings.Contains(tag, "_") {
tag = strings.Replace(tag, "_", " ", -1)
}
}
// 遍历对比
itag := -1
for k, h := range aHeader {
fmt.Println(tag, h)
if strings.EqualFold(h, tag) {
itag = k
break
}
}
fmt.Println(f, val, itag)
// 判断是否有该Field
if itag == -1 {
continue
/*
err = errors.New("cannot find this field " + tag)
this = nil
return
*/
}
kind := none_k
Kind := f.Type.Kind()
// this is necessary because Kind can't tell distinguish between a primitive type
// and a type derived from it. We're looking for a Value interface defined on
// the pointer to this value
_, ok := val.Addr().Interface().(Value)
if ok {
val = val.Addr()
kind = value_k
} else {
switch Kind {
case reflect.Int, reflect.Int16, reflect.Int8, reflect.Int32, reflect.Int64:
kind = int_k
case reflect.Uint, reflect.Uint16, reflect.Uint8, reflect.Uint32, reflect.Uint64:
kind = uint_k
case reflect.Float32, reflect.Float64:
kind = float_k
case reflect.String:
kind = string_k
default:
kind = value_k
_, ok := val.Interface().(Value)
if !ok {
err = errors.New("cannot convert this type ")
this = nil
return
}
}
}
this.fields = append(this.fields, val)
this.kinds = append(this.kinds, kind)
this.tags = append(this.tags, itag)
// this.kinds[i] = kind
// this.tags[i] = itag
// this.fields[i] = val
fmt.Println(val, kind, itag)
}
fmt.Println(len(this.fields), len(this.kinds))
return this, err
}
// Creates a new iterator from a Reader source and a user-defined struct.
func NewReadIter(rdr Reader, ps interface{}) (this *ReadIter, err error) {
lCsvHeaders, err := rdr.Read()
// Remove BOM
if len(lCsvHeaders) > 0 {
lCsvHeaders[0] = strings.Trim(lCsvHeaders[0], "\xef\xbb\xbf")
}
this, _ = mapType(lCsvHeaders, reflect.ValueOf(ps).Elem())
this.Reader = rdr
if err != nil {
this = nil
return
}
//fmt.Println(len(this.fields), len(this.kinds))
/*
st := reflect.TypeOf(ps).Elem()
sv := reflect.ValueOf(ps).Elem()
nf := st.NumField()
this.kinds = make([]int, nf)
this.tags = make([]int, nf)
this.fields = make([]reflect.Value, nf)
fmt.Println(this.fields, this.tags, this.kinds)
for i := 0; i < nf; i++ {
f := st.Field(i) //field
// ADD BY HZM
if f.Kind() == reflect.Struct {
lParentStruct := f.Type()
for _, lFld := range lParentStruct.NumField() {
col.FieldName = fmt.Sprintf("%v.%v", t.Field(i).Name, lFld.FieldName)
this.fields = append(this.fields)
}
continue
}
fmt.Println(f)
val := sv.Field(i) // field value
// get the corresponding field name and look it up in the headers
tag := f.Tag.Get("field")
if len(tag) == 0 {
tag = f.Name
if strings.Contains(tag, "_") {
tag = strings.Replace(tag, "_", " ", -1)
}
}
// 遍历对比
itag := -1
for k, h := range this.Headers {
if strings.EqualFold(h, tag) {
itag = k
break
}
}
// 判断是否有该Field
if itag == -1 {
continue
/*
err = errors.New("cannot find this field " + tag)
this = nil
return
*/
/* }
kind := none_k
Kind := f.Type.Kind()
// this is necessary because Kind can't tell distinguish between a primitive type
// and a type derived from it. We're looking for a Value interface defined on
// the pointer to this value
_, ok := val.Addr().Interface().(Value)
if ok {
val = val.Addr()
kind = value_k
} else {
switch Kind {
case reflect.Int, reflect.Int16, reflect.Int8, reflect.Int32, reflect.Int64:
kind = int_k
case reflect.Uint, reflect.Uint16, reflect.Uint8, reflect.Uint32, reflect.Uint64:
kind = uint_k
case reflect.Float32, reflect.Float64:
kind = float_k
case reflect.String:
kind = string_k
default:
kind = value_k
_, ok := val.Interface().(Value)
if !ok {
err = errors.New("cannot convert this type ")
this = nil
return
}
}
}
this.kinds[i] = kind
this.tags[i] = itag
this.fields[i] = val
}
*/
return
}
// The Get method reads the next row. If there was an error or EOF, it
// will return false. Client code must then check that ReadIter.Error is
// not nil to distinguish between normal EOF and specific errors.
func (this *ReadIter) Get() bool {
row, err := this.Reader.Read()
this.Line = this.Line + 1
if err != nil {
if err != io.EOF {
this.Error = err
}
return false
}
var ival int64
var fval float64
var uval uint64
var v Value
var ok bool
for fi, ci := range this.tags {
vals := row[ci] // string at column ci of current row
f := this.fields[fi]
switch this.kinds[fi] {
case string_k:
f.SetString(vals)
case int_k:
// HZM 空白Int字段
if vals == "" {
vals = "0"
}
ival, err = strconv.ParseInt(vals, 10, 0)
f.SetInt(ival)
case uint_k:
uval, err = strconv.ParseUint(vals, 10, 0)
f.SetUint(uval)
case float_k:
fval, err = strconv.ParseFloat(vals, 0)
f.SetFloat(fval)
case value_k:
v, ok = f.Interface().(Value)
if !ok {
err = errors.New("Not a Value object")
break
}
v.Set(vals)
}
if err != nil {
this.Column = ci + 1
this.Error = err
return false
}
}
return true
}