Debian Codegraph @

writer.go

raw symbols SCIP
1 package csv
2
3 import (
4 encoding "encoding/csv"
5 "io"
6 )
7
8 type Writer interface {
9 Header() []string // Answer the header of the stream.
10 Blank() Record // Provide a blank record compatible with the stream.
11 Write(r Record) error // Write a single record into the underying stream.
12 Error() error // Return the final error.
13 Close(err error) error // Close the writer with the specified error.
14 }
15
16 // A constructor for a writer using the specified header.
17 // By convention, passing a nil to the Builder returns a Writer
18 // which will release any underlying resources held by the builder
19 // when Close(error) is called.
20 type WriterBuilder func(header []string) Writer
21
22 type writer struct {
23 header []string
24 builder RecordBuilder
25 encoder *encoding.Writer
26 closer io.Closer
27 err error
28 }
29
30 // Answer a Writer for the CSV stream constrained by specified header, using the specified encoding writer
31 func WithCsvWriter(w *encoding.Writer, c io.Closer) WriterBuilder {
32 return func(header []string) Writer {
33 result := &writer{
34 header: header,
35 builder: NewRecordBuilder(header),
36 encoder: w,
37 closer: c,
38 }
39 result.err = result.encoder.Write(header)
40 return result
41 }
42 }
43
44 // Answer a Writer for the CSV stream constrained by the specified header, using the specified io writer.
45 func WithIoWriter(w io.WriteCloser) WriterBuilder {
46 return WithCsvWriter(encoding.NewWriter(w), w)
47 }
48
49 // Answer a Writer for the CSV stream constrained by the specified header, using the specified io writer and delimiter.
50 func WithIoWriterAndDelimiter(w io.WriteCloser, delimiter rune) WriterBuilder {
51 writer := encoding.NewWriter(w)
52 writer.Comma = delimiter
53 return WithCsvWriter(writer, w)
54 }
55
56 // Answer the header that constrains the output stream
57 func (w *writer) Header() []string {
58 return w.header
59 }
60
61 // Answer a blank record for the output stream
62 func (w *writer) Blank() Record {
63 return w.builder(make([]string, len(w.header), len(w.header)))
64 }
65
66 // Write a record into the underlying stream.
67 func (w *writer) Write(r Record) error {
68 if w.err != nil {
69 return w.err
70 }
71 h := r.Header()
72 var d []string
73 if len(h) > 0 && len(w.header) == len(h) && &h[0] == &w.header[0] {
74 // optimisation to avoid copying or iterating over slice in default case
75 d = r.AsSlice()
76 } else {
77 // fallback in case where the stream and the record have a different header
78 d := make([]string, len(w.header), len(w.header))
79 for i, k := range w.header {
80 d[i] = r.Get(k)
81 }
82 }
83 return w.encoder.Write(d)
84 }
85
86 func (w *writer) Error() error {
87 return w.err
88 }
89
90 // Close the stream and propagate an error
91 func (w *writer) Close(err error) error {
92 w.encoder.Flush()
93 if err == nil {
94 err = w.encoder.Error()
95 }
96 w.err = err
97 if w.closer != nil {
98 return w.closer.Close()
99 } else {
100 return nil
101 }
102 }