forked from jszwec/csvutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoder.go
223 lines (194 loc) · 4.95 KB
/
encoder.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
package csvutil
import (
"bytes"
"reflect"
)
type encField struct {
field
encodeFunc
}
type encCache struct {
types map[typeKey][]encField
buf bytes.Buffer
index []int
record []string
}
func (c *encCache) fields(k typeKey) ([]encField, error) {
encFields, ok := c.types[k]
if !ok {
fields := cachedFields(k)
encFields = make([]encField, len(fields))
for i, f := range fields {
fn, err := encodeFn(f.typ)
if err != nil {
return nil, err
}
encFields[i] = encField{
field: f,
encodeFunc: fn,
}
}
c.types[k] = encFields
}
return encFields, nil
}
func (c *encCache) reset(fieldsLen int) {
c.buf.Reset()
if fieldsLen != len(c.index) {
c.index = make([]int, fieldsLen)
c.record = make([]string, fieldsLen)
return
}
for i := range c.index {
c.index[i] = 0
c.record[i] = ""
}
}
// Encoder writes structs CSV representations to the output stream.
type Encoder struct {
// Tag defines which key in the struct field's tag to scan for names and
// options (Default: 'csv').
Tag string
w Writer
cache encCache
noHeader bool
}
// NewEncoder returns a new encoder that writes to w.
func NewEncoder(w Writer) *Encoder {
return &Encoder{
w: w,
noHeader: true,
cache: encCache{types: make(map[typeKey][]encField)},
}
}
// Encode writes the CSV encoding of v to the output stream. The provided
// argument v must be a struct.
//
// Only the exported fields will be encoded.
//
// First call to Encode will write a header. Header names can be customized by
// using tags ('csv' by default), otherwise original Field names are used.
//
// Header and fields are written in the same order as struct fields are defined.
// Embedded struct's fields are treated as if they were part of the outer struct.
// Fields that are embedded types and that are tagged are treated like any
// other field, but they have to implement Marshaler or encoding.TextMarshaler
// interfaces.
//
// Marshaler interface has the priority over encoding.TextMarshaler.
//
// Tagged fields have the priority over non tagged fields with the same name.
//
// Following the Go vibility rules if there are multiple fields with the same
// name (tagged or not tagged) on the same level and choice between them is
// ambiguous, then all these fields will be ignored.
//
// Nil values will be encoded as empty strings. Same will happen if 'omitempty'
// tag is set, and the value is a default value like 0, false or nil interface.
//
// Bool types are encoded as 'true' or 'false'.
//
// Float types are encoded using strconv.FormatFloat with precision -1 and 'G'
// format.
//
// Fields of type []byte are being encoded as base64-encoded strings.
//
// Fields can be excluded from encoding by using '-' tag option.
//
// Examples of struct tags:
//
// // Field appears as 'myName' header in CSV encoding.
// Field int `csv:"myName"`
//
// // Field appears as 'Field' header in CSV encoding.
// Field int
//
// // Field appears as 'myName' header in CSV encoding and is an empty string
// // if Field is 0.
// Field int `csv:"myName,omitempty"`
//
// // Field appears as 'Field' header in CSV encoding and is an empty string
// // if Field is 0.
// Field int `csv:",omitempty"`
//
// // Encode ignores this field.
// Field int `csv:"-"`
//
// Encode doesn't flush data. The caller is responsible for calling Flush() if
// the used Writer supports it.
func (e *Encoder) Encode(v interface{}) error {
return e.encode(reflect.ValueOf(v))
}
func (e *Encoder) encode(v reflect.Value) error {
v = indirect(v)
if v.Kind() != reflect.Struct {
return &InvalidEncodeError{v.Type()}
}
if e.noHeader {
k := typeKey{e.tag(), v.Type()}
fields, err := e.cache.fields(k)
if err != nil {
return err
}
if err := e.encodeHeader(fields); err != nil {
return err
}
e.noHeader = false
}
return e.marshal(v)
}
func (e *Encoder) encodeHeader(fields []encField) error {
e.cache.reset(len(fields))
for i, f := range fields {
e.cache.record[i] = f.tag.name
}
return e.w.Write(e.cache.record)
}
func (e *Encoder) marshal(v reflect.Value) error {
k := typeKey{e.tag(), v.Type()}
fields, err := e.cache.fields(k)
if err != nil {
return err
}
e.cache.reset(len(fields))
buf, index, record := &e.cache.buf, e.cache.index, e.cache.record
for i, f := range fields {
v, ok := walkIndex(v, f.index)
if !ok {
continue
}
n, err := f.encodeFunc(v, buf, f.tag.omitEmpty)
if err != nil {
return err
}
index[i] = n
}
out := buf.String()
for i, n := range index {
record[i], out = out[:n], out[n:]
}
return e.w.Write(record)
}
func (e *Encoder) tag() string {
if e.Tag == "" {
return defaultTag
}
return e.Tag
}
func walkIndex(v reflect.Value, index []int) (reflect.Value, bool) {
for _, i := range index {
v = walkPtr(v)
if !v.IsValid() {
return reflect.Value{}, false
}
v = v.Field(i)
}
v = walkPtr(v)
return v, v.IsValid()
}
func walkPtr(v reflect.Value) reflect.Value {
for v.Kind() == reflect.Ptr {
v = v.Elem()
}
return v
}