-
Notifications
You must be signed in to change notification settings - Fork 15
/
tsv_rw.go
187 lines (178 loc) · 4.4 KB
/
tsv_rw.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
package file
import (
"bufio"
"github.com/pkg/errors"
"io"
"reflect"
"strings"
)
type tsvReader struct {
fd FileObjectModel
reader io.ReadCloser
bufReader *bufio.Reader
metadata *xsvMetadata
}
type tsvWriter struct {
fd FileObjectModel
writer io.WriteCloser
bufWriter *bufio.Writer
metadata *xsvMetadata
}
type tsvFileItemReader struct {
}
func (r *tsvFileItemReader) Open(fd FileObjectModel) (interface{}, error) {
if fd.Type != TSV {
return nil, errors.New("file type doesn't match tsvFileItemReader")
}
fs := fd.FileStore
reader, err := fs.Open(fd.FileName, fd.Encoding)
if err != nil {
return nil, err
}
bufReader := bufio.NewReader(reader)
prot := fd.ItemPrototype
tp := reflect.TypeOf(prot)
if tp.Kind() == reflect.Ptr {
tp = tp.Elem()
}
if tp.Kind() != reflect.Struct {
if er := reader.Close(); er != nil {
//
}
return nil, errors.New("the underlying type of ItemPrototype is not struct for " + fd.FileName)
}
metadata, err := getMetadata(tp)
if err != nil {
if er := reader.Close(); er != nil {
//
}
return nil, err
}
handle := &tsvReader{fd, reader, bufReader, metadata}
if fd.Header {
line, err := bufReader.ReadString('\n')
if err != nil {
if er := reader.Close(); er != nil {
//
}
return nil, err
}
sep := getFieldSeparator(fd)
fields := strings.Split(line, sep)
metadata.fileHeaders, metadata.fileHeaderMap = fields, slice2Map(fields)
}
return handle, nil
}
func (r *tsvFileItemReader) Close(handle interface{}) error {
fdr := handle.(*tsvReader)
return fdr.reader.Close()
}
func (r *tsvFileItemReader) ReadItem(handle interface{}) (interface{}, error) {
fdr := handle.(*tsvReader)
line, err := fdr.bufReader.ReadString('\n')
if err != nil && err != io.EOF {
return nil, err
} else if err == io.EOF && line == "" {
return nil, nil
}
sep := getFieldSeparator(fdr.fd)
fields := strings.Split(line, sep)
metadata := fdr.metadata
var val reflect.Value
if fdr.fd.Header {
val, err = xsvUnmarshal(fields, metadata.fileHeaderMap, metadata.structType)
} else {
val, err = xsvUnmarshalByOrder(fields, metadata.structType)
}
if err != nil {
return nil, err
}
return val.Interface(), nil
}
func getFieldSeparator(fd FileObjectModel) string {
sep := fd.FieldSeparator
if sep == "" {
sep = "\t"
}
return sep
}
func (r *tsvFileItemReader) SkipTo(handle interface{}, pos int64) error {
fdr := handle.(*tsvReader)
for i := int64(0); i < pos; i++ {
_, err := fdr.bufReader.ReadString('\n')
if err != nil {
return err
}
}
return nil
}
func (r *tsvFileItemReader) Count(fd FileObjectModel) (int64, error) {
return Count(fd)
}
type tsvFileItemWriter struct {
}
func (r *tsvFileItemWriter) Open(fd FileObjectModel) (interface{}, error) {
if fd.Type != TSV {
return nil, errors.New("file type doesn't match tsvFileItemWriter")
}
fs := fd.FileStore
writer, err := fs.Create(fd.FileName, fd.Encoding)
if err != nil {
return nil, err
}
bufWriter := bufio.NewWriter(writer)
prot := fd.ItemPrototype
tp := reflect.TypeOf(prot)
if tp.Kind() == reflect.Ptr {
tp = tp.Elem()
}
if tp.Kind() != reflect.Struct {
return nil, errors.New("the underlying type of ItemPrototype is not struct for " + fd.FileName)
}
metadata, err := getMetadata(tp)
if err != nil {
return nil, err
}
if fd.Header {
_, err = bufWriter.WriteString(strings.Join(metadata.structFields, getFieldSeparator(fd)))
if err != nil {
return nil, err
}
}
return &tsvWriter{fd, writer, bufWriter, metadata}, nil
}
func (r *tsvFileItemWriter) Close(handle interface{}) error {
fdw := handle.(*tsvWriter)
defer func() {
if err := fdw.writer.Close(); err != nil {
//
}
}()
return fdw.bufWriter.Flush()
}
func (r *tsvFileItemWriter) WriteItem(handle interface{}, item interface{}) error {
fdw := handle.(*tsvWriter)
var fields []string
var err error
if fdw.fd.Header {
fields, err = xsvMarshal(item, fdw.metadata)
} else {
fields, err = xsvMarshal(item, fdw.metadata)
}
if err != nil {
return err
}
sep := getFieldSeparator(fdw.fd)
_, err = fdw.bufWriter.WriteString(strings.Join(fields, sep))
return err
}
type tsvFileMergeSplitter struct {
}
func (r *tsvFileMergeSplitter) Merge(src []FileObjectModel, dest FileObjectModel) error {
err := Merge(src, dest)
return err
}
func (r *tsvFileMergeSplitter) Split(src FileObjectModel, dest []FileObjectModel, strategy FileSplitStrategy) error {
err := Split(src, dest, strategy)
return err
}