-
Notifications
You must be signed in to change notification settings - Fork 8
/
writer.go
155 lines (137 loc) · 3.66 KB
/
writer.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
// Copyright 2014 Jens Rantil. All rights reserved. Use of this source code is
// governed by a BSD-style license that can be found in the LICENSE file.
package csv
import (
"bufio"
"io"
"strings"
)
// A Writer writes records to a CSV encoded file.
//
// Can be created by calling either NewWriter or using NewDialectWriter.
type Writer struct {
opts Dialect
w *bufio.Writer
}
// Create a writer that conforms to RFC 4180 and behaves identical as a
// encoding/csv.Reader.
//
// See `Default*` constants for default dialect used.
func NewWriter(w io.Writer) Writer {
return NewDialectWriter(w, Dialect{})
}
// Create a custom CSV writer.
func NewDialectWriter(w io.Writer, opts Dialect) Writer {
opts.setDefaults()
return Writer{
opts: opts,
w: bufio.NewWriter(w),
}
}
// Error reports any error that has occurred during a previous Write or Flush.
func (w Writer) Error() error {
_, err := w.w.Write(nil)
return err
}
// Flush writes any buffered data to the underlying io.Writer.
// To check if an error occurred during the Flush, call Error.
func (w Writer) Flush() {
w.w.Flush()
}
// Helper function that ditches the first return value of w.w.WriteString().
// Simplifies code.
func (w Writer) writeString(s string) error {
_, err := w.w.WriteString(s)
return err
}
func (w Writer) writeDelimiter() error {
return w.writeRune(w.opts.Delimiter)
}
func (w Writer) fieldNeedsQuote(field string) bool {
switch w.opts.Quoting {
case QuoteNone:
return false
case QuoteAll:
return true
case QuoteNonNumeric:
return !isNumeric(field)
case QuoteNonNumericNonEmpty:
return !(isNumeric(field) || isEmpty(field))
case QuoteMinimal:
// TODO: Can be improved by making a single search with trie.
// See https://docs.python.org/2/library/csv.html#csv.QUOTE_MINIMAL for info on this.
return strings.Contains(field, w.opts.LineTerminator) || strings.ContainsRune(field, w.opts.Delimiter) || strings.ContainsRune(field, w.opts.QuoteChar)
}
panic("Unexpected quoting.")
}
func (w Writer) writeRune(r rune) error {
_, err := w.w.WriteRune(r)
return err
}
func (w Writer) writeEscapeChar(r rune) error {
switch w.opts.DoubleQuote {
case DoDoubleQuote:
return w.writeRune(r)
case NoDoubleQuote:
return w.writeRune(w.opts.EscapeChar)
}
panic("Unrecognized double quote type.")
}
func (w Writer) writeQuotedRune(r rune) error {
switch r {
case w.opts.EscapeChar:
if err := w.writeEscapeChar(r); err != nil {
return err
}
case w.opts.QuoteChar:
if err := w.writeEscapeChar(r); err != nil {
return err
}
}
return w.writeRune(r)
}
func (w Writer) writeQuoted(field string) error {
if err := w.writeRune(w.opts.QuoteChar); err != nil {
return err
}
for _, r := range field {
if err := w.writeQuotedRune(r); err != nil {
return err
}
}
return w.writeRune(w.opts.QuoteChar)
}
func (w Writer) writeField(field string) error {
if w.fieldNeedsQuote(field) {
return w.writeQuoted(field)
}
return w.writeString(field)
}
func (w Writer) writeNewline() error {
return w.writeString(w.opts.LineTerminator)
}
// Writer writes a single CSV record to w along with any necessary quoting.
// A record is a slice of strings with each string being one field.
func (w Writer) Write(record []string) (err error) {
for n, field := range record {
if n > 0 {
if err = w.writeDelimiter(); err != nil {
return
}
}
if err = w.writeField(field); err != nil {
return
}
}
err = w.writeNewline()
return
}
// WriteAll writes multiple CSV records to w using Write and then calls Flush.
func (w Writer) WriteAll(records [][]string) (err error) {
for _, record := range records {
if err := w.Write(record); err != nil {
return err
}
}
return w.w.Flush()
}