This repository has been archived by the owner on Aug 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
csvparser.go
207 lines (194 loc) · 4.6 KB
/
csvparser.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
package dataset
import (
"encoding/csv"
"errors"
"io"
"strconv"
"strings"
)
var (
ErrFailedToParseCSVorTSVFile error = errors.New("failed to parse file content")
ErrIncompatibleSchema error = errors.New("schema is not compatible with csv")
ErrDuplicatiedNameFields error = errors.New("failed to parse, name-duplicated fields")
)
type DatasetCSVParser struct {
reader *csv.Reader
firstline []string
headers []string
schema *Schema
name string
}
func NewCSVParser(r io.Reader, n string, seperator rune) *DatasetCSVParser {
r2 := csv.NewReader(r)
r2.Comma = seperator
obj := &DatasetCSVParser{
reader: r2,
name: n,
}
return obj
}
func (p *DatasetCSVParser) Init() error {
headers, err := p.reader.Read()
if err != nil {
return ErrFailedToParseCSVorTSVFile
}
p.headers = headers
p.firstline, err = p.reader.Read()
if err != nil {
return ErrFailedToParseCSVorTSVFile
}
return nil
}
func (p *DatasetCSVParser) validateLine(line []string) bool {
return len(p.headers) == len(line)
}
func (p *DatasetCSVParser) GuessSchema(sid SceneID) error {
if !p.validateLine(p.firstline) {
return ErrFailedToParseCSVorTSVFile
}
schemafields := []*SchemaField{}
haslat, haslng := false, false
for k, h := range p.headers {
if h == "lat" {
haslat = true
}
if h == "lng" {
haslng = true
}
if h != "lng" && h != "lat" && strings.TrimSpace(h) != "" {
t := ValueFromStringOrNumber(p.firstline[k]).Type()
field, _ := NewSchemaField().NewID().Name(h).Type(t).Build()
schemafields = append(schemafields, field)
}
}
if haslat && haslng {
field, _ := NewSchemaField().NewID().Name("location").Type(ValueTypeLatLng).Build()
schemafields = append(schemafields, field)
}
schema, err := NewSchema().
NewID().
Scene(sid).
Name(p.name).
Source("file:///" + p.name).
Fields(schemafields).
Build()
if err != nil {
return err
}
p.schema = schema
return nil
}
func (p *DatasetCSVParser) ReadAll() (*Schema, []*Dataset, error) {
if p.schema == nil {
return nil, nil, errors.New("schema is not generated yet")
}
var fields []*Field
schemafieldmap := make(map[string]FieldID)
for _, f := range p.schema.Fields() {
if _, ok := schemafieldmap[f.Name()]; !ok {
schemafieldmap[f.Name()] = f.ID()
} else {
return nil, nil, ErrDuplicatiedNameFields
}
}
datasets := []*Dataset{}
i := 0
for {
var line []string
var err error
if i == 0 {
// process first line
line = p.firstline
} else {
line, err = p.reader.Read()
if err == io.EOF {
break
}
if err != nil {
return nil, nil, err
}
}
if !p.validateLine(line) {
return nil, nil, ErrFailedToParseCSVorTSVFile
}
fields, err = p.getFields(line, schemafieldmap)
if err != nil {
return nil, nil, err
}
ds, err := New().NewID().
Fields(fields).
Scene(p.schema.Scene()).Schema(p.schema.ID()).Build()
if err != nil {
return nil, nil, err
}
datasets = append(datasets, ds)
i++
}
return p.schema, datasets, nil
}
func (p *DatasetCSVParser) getFields(line []string, sfm map[string]FieldID) ([]*Field, error) {
fields := []*Field{}
var lat, lng *float64
for i, record := range line {
value := ValueFromStringOrNumber(record)
if p.headers[i] == "lng" {
value, err := strconv.ParseFloat(record, 64)
if err != nil {
return nil, ErrFailedToParseCSVorTSVFile
}
lng = &value
}
if p.headers[i] == "lat" {
value, err := strconv.ParseFloat(record, 64)
if err != nil {
return nil, ErrFailedToParseCSVorTSVFile
}
lat = &value
}
if p.headers[i] != "lat" && p.headers[i] != "lng" {
fields = append(fields, NewField(sfm[p.headers[i]], value, ""))
}
}
if lat != nil && lng != nil {
latlng := LatLng{Lat: *lat, Lng: *lng}
fields = append(fields, NewField(sfm["location"], ValueTypeLatLng.ValueFrom(latlng), ""))
}
return append([]*Field{}, fields...), nil
}
func (p *DatasetCSVParser) CheckCompatible(s *Schema) error {
fieldsmap := make(map[string]*SchemaField)
for _, f := range s.Fields() {
fieldsmap[f.Name()] = f
}
haslat, haslng := false, false
for i, h := range p.headers {
if h != "lat" && h != "lng" {
if fieldsmap[h] == nil {
return ErrIncompatibleSchema
}
t := fieldsmap[h].Type()
v := ValueFromStringOrNumber(p.firstline[i])
if v.Type() != t {
return ErrIncompatibleSchema
}
}
if h == "lat" {
haslat = true
}
if h == "lng" {
haslng = true
}
}
// check for location fields
if haslat && haslng {
if fieldsmap["location"] == nil {
return ErrIncompatibleSchema
}
} else {
if fieldsmap["location"] != nil {
return ErrIncompatibleSchema
}
}
p.schema = s
return nil
}